Kotlin class:
class Invoice {
}
, ( , .) , . : , .
class Empty
Kotlin (primary constructor) (secondary constructors). , ( ).
class Person constructor(firstName: String) {
}
, constructor :
class Person(firstName: String) {
}
. (initializers blocks), init:
class Customer(name: String) {
init {
logger.info("Customer initialized with value ${name}")
}
}
, . :
class Customer(name: String) {
val customerKey = name.toUpperCase()
}
, , Kotlin :
class Person(val firstName: String, val lastName: String, var age: Int) {
// ...
}
, , (var) (val).
, constructor , .
class Customer public @Inject constructor(name: String) { ... }
, . " ".
(secondary constructors), constructor:
class Person {
constructor(parent: Person) {
parent.children.add(this)
}
}
() , ( () ()) .
class Person(val name: String) {
constructor(name: String, parent: Person) : this(name) {
parent.children.add(this)
}
}
( ), . public. public , :
class DontCreateMe private constructor () {
}
: JVM, , . , Jackson JPA, Kotlin, .
class Customer(val customerName: String = "")
, :
val invoice = Invoice()
val customer = Customer("Joe Smith")
, Kotlin new.
:
Koltin Any
. , - :
class Example // Implicitly inherits from Any
Any
java.lang.Object
. , : equals()
, hashCode()
, toString()
. , c Java .
, :
open class Base(p: Int)
class Derived(p: Int) : Base(p)
, ( ) , .
, super , . , .
class MyView : View {
constructor(ctx: Context) : super(ctx) {
}
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
}
}
open final Java: . , Kotlin final, Effective Java, Item 17: Design and document for inheritance or else prohibit it.
, Kotlin. , Java, Kotlin , , :
open class Base {
open fun v() {}
fun nv() {}
}
class Derived() : Base() {
override fun v() {}
}
Derived.v()
override. , . Base.nv()
open, , override . final (, open), open .
, override open, . , final:
open class AnotherDerived() : Base() {
final override fun v() {}
}
( final) - , .
, :
Kotlin : , (, ). , ( ), , super. , super<Base>
:
open class A {
open fun f() { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } // interface members are 'open' by default
fun b() { print("b") }
}
class C() : A(), B {
// The compiler requires f() to be overridden:
override fun f() {
super<A>.f() // call to A.f()
super<B>.f() // call to B.f()
}
}
A
B
. a()
b()
, C
.
f()
, C
, f()
C
.
abstract. . , open - .
- open
open class Base {
open fun f() {}
}
abstract class Derived : Base() {
override abstract fun f()
}
Kotlin, Java C#, . , (.: "package-level functions").
, - , , , Java/C# ( ).
, , , . , - , enum : enum , enum , , , (sealed class), .
, sealed , sealed
. , .
sealed class Expr {
class Const(val number: Double) : Expr()
class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
}
, , sealed ( ), , sealed .
sealed , when
. , , else
.
fun eval(expr: Expr): Double = when(expr) {
is Expr.Const -> expr.number
is Expr.Sum -> eval(expr.e1) + eval(expr.e2)
Expr.NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}
![]() | 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 -> - _. |
|
![]() |