Kotlin : (mutable) (read-only) var val .
public class Address {
public var name: String = ...
public var street: String = ...
public var city: String = ...
public var state: String? = ...
public var zip: String = ...
}
, , ( Java):
fun copyAddress(address: Address): Address {
val result = Address() // `new`
result.name = address.name //
result.street = address.street
// ...
return result
}
var <propertyName>: <PropertyType> [= <property_initializer>]
[<getter>]
[<setter>]
property_initializer, . , .
:
var allByDefault: Int? // : ,
var initialized = 1 // Int,
: -, val
var
, -, :
val simple: Int? // Int, ,
val inferredType = 1 // Int
, , . , :
val isEmpty: Boolean
get() = this.size == 0
:
var stringRepresentation: String
get() = this.toString()
set(value) {
setDataFromString(value) //
}
, - value
, .
, , :
var setterVisibility: String = "abc"
private set // private
var setterWithAnnotation: Any? = null
@Inject set // Inject
Kotlin . .. , Java, , .. get set. , , backing field. , , , backing field. Kotlin backing field, field
:
var counter = 0
set(value) {
if (value >= 0) field = value // backing field
}
field
.
Backing field , . , field
.
, backing field:
val isEmpty: Boolean
get() = this.size == 0
- , " backing field", backing property:
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null) {
_table = HashMap() // (.: "Type parameters are inferred")
}
return _table ?: throw AssertionError("Set to null by another thread")
}
, Java, , .
, , . const
. :
object
String
:
const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"
@Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
, , non-null , . , . , , (.: "setup method") -, onCreate
Android. non-null , null .
, , lateinit
:
public class MyTest {
lateinit var subject: TestSubject
@SetUp fun setup() {
subject = TestSubject()
}
@Test fun test() {
subject.method() // , null
}
}
var
, ( ). , . non-null .
lateinit
, , , , .
( ) backing field. , . , , . :
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |