Kotlin Inheritance
In Kotlin, all classes inherit from the Any class. It is the superclass of all classes, and is the default superclass for classes without explicit supertype declarations:
class Example // 从 Any 隐式继承
Any provides three functions by default:
equals() hashCode() toString()
Note: Any is not java.lang.Object.
If a class is to be inherited, it can be modified with the open keyword.
open class Base(p: Int) // 定义基类 class Derived(p: Int) : Base(p)
Constructors
Subclass with a primary constructor
If the subclass has a primary constructor, the base class must be initialized immediately in the primary constructor.
open class Person(var name : String, var age : Int){// 基类
}
class Student(name : String, age : Int, var no : String, var score : Int) : Person(name, age) {
}
// 测试
fun main(args: Array<String>) {
val s = Student("Example", 18, "S12346", 89)
println("学生名: ${s.name}")
println("年龄: ${s.age}")
println("学生号: ${s.no}")
println("成绩: ${s.score}")
}
Output:
学生名: Example 年龄: 18 学生号: S12346 成绩: 89
Subclass without a primary constructor
If the subclass has no primary constructor, the base class must be initialized in each secondary constructor using the super keyword, or it must delegate to another constructor. When initializing the base class, different constructors of the base class can be called.
class Student : Person {
constructor(ctx: Context) : super(ctx) {
}
constructor(ctx: Context, attrs: AttributeSet) : super(ctx,attrs) {
}
}
Example
/**用户基类**/
open class Person(name:String){
/**次级构造函数**/
constructor(name:String,age:Int):this(name){
//初始化
println("-------基类次级构造函数---------")
}
}
/**子类继承 Person 类**/
class Student:Person{
/**次级构造函数**/
constructor(name:String,age:Int,no:String,score:Int):super(name,age){
println("-------继承类次级构造函数---------")
println("学生名: ${name}")
println("年龄: ${age}")
println("学生号: ${no}")
println("成绩: ${score}")
}
}
fun main(args: Array<String>) {
var s = Student("Example", 18, "S12345", 89)
}
Output:
-------基类次级构造函数--------- -------继承类次级构造函数--------- 学生名: Example 年龄: 18 学生号: S12345 成绩: 89
Overriding
In a base class, when a function is declared with fun, it is modified as final by default and cannot be overridden by subclasses. If you want to allow subclasses to override the function, you must manually add open to modify it. Subclasses override methods using the override keyword:
/**用户基类**/
open class Person{
open fun study(){ // 允许子类重写
println("我毕业了")
}
}
/**子类继承 Person 类**/
class Student : Person() {
override fun study(){ // 重写方法
println("我在读大学")
}
}
fun main(args: Array<String>) {
val s = Student()
s.study();
}
The output is:
I am in college.
If there are multiple identical methods (inherited from or implemented from other classes, such as classes A and B), the method must be overridden, and the super generic can be used to selectively call the implementation of the parent class.
open class A {
open fun f () { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } //接口的成员变量默认是 open 的
fun b() { print("b") }
}
class C() : A() , B{
override fun f() {
super<A>.f()//调用 A.f()
super<B>.f()//调用 B.f()
}
}
fun main(args: Array<String>) {
val c = C()
c.f();
}
C inherits from a() or b(). C can not only inherit functions from A or B, but C can also inherit functions common to A() and B(). At this point, the function has only one implementation in C. To eliminate ambiguity, the function must call the implementations of this function in A() and B(), and provide its own implementation.
The output is:
AB
Property Overriding
Property overriding uses the override keyword. Properties must have compatible types. Each declared property can be overridden by an initializer or a getter method:
open class Foo {
open val x: Int get { …… }
}
class Bar1 : Foo() {
override val x: Int = ……
}
You can override a val property with a var property, but not vice versa. Because a val property itself defines a getter method, overriding it as a var property additionally declares a setter method in the derived class.
You can use the override keyword in the primary constructor as part of the property declaration:
interface Foo {
val count: Int
}
class Bar1(override val count: Int) : Foo
class Bar2 : Foo {
override var count: Int = 0
}
Other Extensions