Код: Выделить всё
private static Book currentBook;
public static Book get() {
if(currentBook == null) {
currentBook = new Book();
}
return currentBook;
}
public static void set(Book book) {
if(currentBook != null) {
throw IllegealStateException()
}
currentBook = book
}
Код: Выделить всё
companion object {
var currentBook: Book? = null
get(): Book? {
if (field == null) {
field = Book()
}
return field
}
set(value) {
if(field != null) {
throw IllegalStateException()
}
field = value
}
}
Есть ли правильный способ для статического свойства currentBook, которое имеет необнуляемый тип Book?
Подробнее здесь: https://stackoverflow.com/questions/793 ... ialization
Мобильная версия