Kotlin Class
Kotlin Basic Class
class Invoice { }
Empty
class Empty
Constructor
class ConstExample(str : String) { var str : String = str var goal : Int = 0 init { this.str = "Taroshi" } constructor(goal : Int, str: String) : this(str){ this.goal = goal } } val jiro : ConstExample = ConstExample("Jiro") val sabu : ConstExample = ConstExample(3, "Saburo") fun makeSure() { jiro.str = "Jirobo" println(sabu.goal) }
Inheritance
To allow to inherit class, we need to add open
open class Base(p: Int)
Derived
class Derived(p: Int) : Base(p)
abstract
Of course, Kotlin has abstract class
// Abstract abstract class Abst { abstract fun f() } class BAbst : Abst() { override fun f() { } }
Kotlin Basic Interface
Difference between Java and Kotlin interface is that Kotlin interface may have implementation.
interface B { fun f() { print("Bf") } // Can write actual codes fun b() { print("Bb")} }
Those two interface has actual code(implementation).
Override Rule
// Overriding Rules open class A { open fun f() { print("Af")} fun a() {print("Aa")} } interface B { fun f() { print("Bf") } // Can write actual codes fun b() { print("Bb")} } class C() : A(), B { override fun f() { super<A>.f() super<B>.f() } override fun b() { super.b() } }
We can choose interface or class override using super<T>