Kotlin-2.1-类和继承

本文详细介绍了Kotlin中类的定义与使用方法,包括构造器、类成员、继承及抽象类等内容。同时,还讲解了如何重载方法与属性、调用超类实现以及伴侣对象的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kotlin-2.1-Classes and Inheritance

标签:Kotlin


翻译自官方文档:Classes and Inheritance-如有遗漏和错误,欢迎指正!

本节介绍了Kotlin中类和继承的内容。类中包含首要构造器和次要构造器,还有类成员:属性(Java中成员变量)和函数(Java中方法)。继承主要讲解的是如何重载父类的方法和属性以及如何调用父类的实现。最后归纳总结重载的原则。

1-类和继承

1-类

在Kotlin中类使用关键字class声明:
Classes in Kotlin are declared using the keyword class:

class Invoice {
}

类声明由类名类头类主体三部分组成,用大括号包裹。其中类头(class header)指明了类的类型参数和首要构造器等内容。类头和类主体都是可选的,如果类没有主体,大括号可以被省略:

class Empty

1-构造器/构造函数

在Kotlin中的类可以有一个首要构造函数(primary constructor)和一个或者多个次要构造函数(secondary constructors)。首要构造函数是类头的一部分:它跟在类名的后面并拥有可选的类型参数,如下:

class Person constructor(firstName: String) {
}

如果首要构造器没有任何注解或者可见性修饰符,该constructor关键字可以被省略,如下:

class Person(firstName: String) {
}

可见性修饰符:类似于java中的public,private等修饰符。

首要构造器不包含任何代码。初始化代码被放置在initializer blocks初始化代码块中, 该代码块以关键字init作为前缀。

在一个实例初始化的期间,初始化代码块以它们在类主体中出现的相同顺序,顺序执行。它们可以和属性初始化纵横交错:

class InitOrderDemo(name: String) {
//属性初始化
    val firstProperty = "First property: $name".also(::println)

//第一段初始化代码块
    init {
        println("First initializer block that prints ${name}")
    }
//属性初始化
    val secondProperty = "Second property: ${name.length}".also(::println)
//第二段初始化代码块
    init {
        println("Second initializer block that prints ${name.length}")
    }
}

fun main(args: Array<String>) {
    InitOrderDemo("hello")
}

注意:首要构造器的参数可以被初始化代码块所使用。它们也可以被在类主体中声明的属性初始化所使用:

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) { ... }

更多细节,参考Visibility Modifiers(可见性修饰符)。

2-次要构造器

类也可以声明次要构造器,次要构造器以constructor作为前缀:

class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}

如果类有首要构造器,每个次要构造器都需要去代理首要构造器,直接代理或间接通过另一个次要构造器去代理。代理同类的另一个构造器,需要使用关键字this

class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

注意:在初始化代码块的代码高效地成为首要构造器的一部分。首要构造器的代理会作为次要构造器的第一行语句执行,所以在所有初始化代码块的代码都在次要构造器之前执行。甚至,如果类没有首要构造器,代理都会隐式地发生,初始化代码块依旧会执行:

class Constructors {
    init {
        println("Init block")
    }

    constructor(i: Int) { //次要构造器,在没有首要构造器时,依然先执行了init代码块内容。
        println("Constructor")
    }
}

fun main(args: Array<String>) {
    Constructors(1)
}

执行结果:

Init block
Constructor

如果,一个非抽象类没有声明任何构造器(包括首要和次要),类仍然会生成一个没有参数的首要构造器。该构造器的可见性是public、如果你不想你的类有一个public构造器,你需要声明一个空的首要构造器,并使用非默认可见性修饰符

class DontCreateMe private constructor () {
}

注意: 在JVM中,如果首要构造器的所有参数都有默认数值,编译器将会生成一个额外的无参数的构造器,该构造器会使用默认数值。这样使得更容易在Kotlin中使用库,例如Jackson或者JPA(通过无参数构造器创建类的实例)

class Customer(val customerName: String = "")

3-创建类的实例

为了创建类的实例,我们将如调用普通函数一样调用构造器:

val invoice = Invoice() //构造器,和普通函数一样

val customer = Customer("Joe Smith")

注意Kotlin中不使用关键字new

嵌套类、内部类、匿名内部类的创建在Nested classes嵌套类中进行介绍。

4-类成员

类可以包含:

  • 构造器和初始化代码块
  • 函数
  • 属性
  • 嵌套和内部类
  • 对象声明

2-继承

在Kotlin中所有类都有一个共同的超类Any, 该超类是默认的,不需要超类的声明:

class Example // 隐式继承自Any

Any不是java.lang.Object;特别地,它除了equals(), hashCode() and toString()没有其他任何成员。请参考Java interoperability(Java互通性)章节。

为了声明一个显式的超类,我们将类型放置在类头后的冒号后面:

open class Base(p: Int)

class Derived(p: Int) : Base(p)

如果类(子类)有首要构造器,这个基本类型(超类)必须在这里被初始化,通过使用首要构造器的参数。

如果类(子类)没有首要构造器,每个次要构造器必须使用关键字super初始化基本类型(超类),或者代理给另一个完成该任务的构造器。注意,在这种情况下,不同的次要构造器可以调用基本类型(超类)的不同的构造器:

//没有首要构造器,只有次要构造器
class MyView : View {
    constructor(ctx: Context) : super(ctx) //MyView的次要构造器初始化父类,通过使用View的super(ctx)构造器。

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) //MyView使用了View的另一种构造器
}

open注解在类中是与java中的final相反的内容:open允许其他类继承自该类。默认的,在Kotlin中所有的类都是final,这对应于Effective Java, Item 17: Design and document for inheritance or else prohibit it.(继承的设计和文档,否则禁止它.-不知道如何翻译,很不通顺。)

补充:本人亲测,在Kotlin中所有类默认都是无法被继承的,因为默认是final的。只有用open修饰的类才可以被继承。

1-重载方法

正如我们之前提到的,我们坚持在Kotlin中让事情都是显式的。不像Java,Kotlin对于覆盖的成员(这些成员需要有open修饰符)需要显式的注释:

open class Base {
    open fun v() {} //open修饰
    fun nv() {}
}
class Derived() : Base() {
    override fun v() {} //显式覆盖
}

override 注解需要修饰 Derived.v(). 如果该注解遗漏,编译器就会报错。如果一个函数没有open注解,类似Base.nv(), 在子类中声明相同签名的方法就是非法的,无论是否有overide。在一个final类中(即,一个没有open注解的类), 禁止拥有open的成员(open修饰符无效).

overide标记的成员其本身就是open的,即,该成员可以在子类被重载。如果你想禁止“再重载”(重载父类的方法继续被子类重载),可以使用final关键字:

open class AnotherDerived() : Base() {
    final override fun v() {} //不会继续被子类重载。
}

2-重载属性

重载属性和重载方法的工作方式类似;在超类声明的属性在派生类中被再次声明,必须附加上override修饰符,此外两个属性必须要有兼容的类型。每个声明的属性可以被具有初始化值得属性重载,或者可以被具有getter方法的属性重载。

open class Foo {
    open val x: Int get() { ... }
}

class Bar1 : Foo() {
    override val x: Int = ...
}

你也可以使用var属性重载val的属性,反之不可以。因为val属性本质具有getter方法,将其重载为var属性,会在派生类中额外声明一个setter即可。

注意,你可以使用override关键字作为首要构造器中属性声明的一部分。

interface Foo {
    val count: Int
}

class Bar1(override val count: Int) : Foo //属性声明中进行重载

class Bar2 : Foo {
    override var count: Int = 0
}

3-调用超类的实现

在派生类中的代码可以调用其超类的函数和属性访问器的实现,通过使用super关键字来达到这效果:

open class Foo {
    open fun f() { println("Foo.f()") }
    open val x: Int get() = 1
}

class Bar : Foo() {
    override fun f() { 
        super.f() //超类中的函数
        println("Bar.f()") 
    }

    override val x: Int get() = super.x + 1 //属性在超类中的getter
}

在内部类中,访问外部类的超类需要使用super关键字,并配合外部类的名字: super@Outer例如(super@Bar):

class Bar : Foo() {
    override fun f() { /* ... */ }
    override val x: Int get() = 0

    inner class Baz {
        fun g() {
            super@Bar.f() // 调用外部类Bar的超类Foo的f()的实现
            println(super@Bar.x) // 使用外部类Bar的超类Foo的x的getter
        }
    }
}

4-重载原则

Kotlin中, 继被下列规则所规范: 如果一个类继承了很多直接超类的同名成员,必须要显式提供自己的实现(比如类实现了两个接口,里面都有同名方法function(),则一定要显式提供实现). 为了表示调用的超类的实现具体是哪个超类的,我们使用super<超类名>来指明:

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 {
    //f()必须要被重载:
    override fun f() {
        super<A>.f() // 调用A的f()
        super<B>.f() // 调用B的f()
    }
}

a()b() 两个方法是没有争议的,不需要重载。方法f()需要提供实现来消除模棱两可的境地。

3-抽象类-Abstract

一个类和其本身的成员可以用abstract声明。抽象成员不能有具体实现。此外,我们不需要用open来注释抽象类或者抽象方法——这是默认的。

我们可以用抽象成员去重载非抽象的open成员:

open class Base {
    open fun f() {}
}

abstract class Derived : Base() {
    override abstract fun f() //用抽象方法重载了非抽象方法
}

4-伴侣对象

Kotlin中,不像Java或者C#,类没有静态方法。在大多是情况下,建议用包级别的函数。

如果你需要写一个函数(类似static方法),该函数不需要类的实例就可以调用,你可以在类中给该成员添加object声明。

甚至更特别的情况,如果你在类中声明一个companion对象。你可以和Java/C#一样通过类名作为修饰符就可以调用其所有成员。


翻译自官方文档Classes and Inheritance
下面是英文原文

2-Classes and Inheritance(原文)

1-Classes

Classes in Kotlin are declared using the keyword class:

class Invoice {
}

The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.) and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.

class Empty

1-Constructors

A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).

class Person constructor(firstName: String) {
}

If the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted:

class Person(firstName: String) {
}

The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword.

During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers:

//sampleStart
class InitOrderDemo(name: String) {
    val firstProperty = "First property: $name".also(::println)

    init {
        println("First initializer block that prints ${name}")
    }

    val secondProperty = "Second property: ${name.length}".also(::println)

    init {
        println("Second initializer block that prints ${name.length}")
    }
}
//sampleEnd

fun main(args: Array<String>) {
    InitOrderDemo("hello")
}

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

class Customer(name: String) {
    val customerKey = name.toUpperCase()
}

In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax:

class Person(val firstName: String, val lastName: String, var age: Int) {
    // ...
}

Much the same way as regular properties, the properties declared in the primary constructor can be mutable (var) or read-only (val).

If the constructor has annotations or visibility modifiers, the constructor keyword is required, and the modifiers go before it:

class Customer public @Inject constructor(name: String) { ... }

For more details, see Visibility Modifiers.

2-Secondary Constructors

The class can also declare secondary constructors, which are prefixed with constructor:

class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

Note that code in initializer blocks effectively becomes part of the primary constructor. Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks is executed before the secondary constructor body. Even if the class has no primary constructor, the delegation still happens implicitly, and the initializer blocks are still executed:

//sampleStart
class Constructors {
    init {
        println("Init block")
    }

    constructor(i: Int) {
        println("Constructor")
    }
}
//sampleEnd

fun main(args: Array<String>) {
    Constructors(1)
}

If a non-abstract class does not declare any constructors (primary or secondary), it will have a generated primary constructor with no arguments. The visibility of the constructor will be public. If you do not want your class to have a public constructor, you need to declare an empty primary constructor with non-default visibility:

class DontCreateMe private constructor () {
}

NOTE: On the JVM, if all of the parameters of the primary constructor have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors.

class Customer(val customerName: String = "")

3-Creating instances of classes

To create an instance of a class, we call the constructor as if it were a regular function:

val invoice = Invoice()

val customer = Customer("Joe Smith")

Note that Kotlin does not have a new keyword.

Creating instances of nested, inner and anonymous inner classes is described in Nested classes.

4-Class Members

Classes can contain:

  • Constructors and initializer blocks
  • Functions
  • Properties
  • Nested and Inner Classes
  • Object Declarations

2-Inheritance

All classes in Kotlin have a common superclass Any, that is a default super for a class with no supertypes declared:

class Example // Implicitly inherits from Any

Any is not java.lang.Object; in particular, it does not have any members other than equals(), hashCode() and toString(). Please consult the Java interoperability section for more details.

To declare an explicit supertype, we place the type after a colon in the class header:

open class Base(p: Int)

class Derived(p: Int) : Base(p)

If the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.

If the class has no primary constructor, then each secondary constructor has to initialize the base type using the super keyword, or to delegate to another constructor which does that. Note that in this case different secondary constructors can call different constructors of the base type:

class MyView : View {
    constructor(ctx: Context) : super(ctx)

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}

The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.

1-Overriding Methods

As we mentioned before, we stick to making things explicit in Kotlin. And unlike Java, Kotlin requires explicit annotations for overridable members (we call them open) and for overrides:

open class Base {
    open fun v() {}
    fun nv() {}
}
class Derived() : Base() {
    override fun v() {}
}

The override annotation is required for Derived.v(). If it were missing, the compiler would complain. If there is no open annotation on a function, like Base.nv(), declaring a method with the same signature in a subclass is illegal, either with override or without it. In a final class (e.g. a class with no open annotation), open members are prohibited.

A member marked override is itself open, i.e. it may be overridden in subclasses. If you want to prohibit re-overriding, use final:

open class AnotherDerived() : Base() {
    final override fun v() {}
}

2-Overriding Properties

Overriding properties works in a similar way to overriding methods; properties declared on a superclass that are then redeclared on a derived class must be prefaced with override, and they must have a compatible type. Each declared property can be overridden by a property with an initializer or by a property with a getter method.

open class Foo {
    open val x: Int get() { ... }
}

class Bar1 : Foo() {
    override val x: Int = ...
}

You can also override a val property with a varproperty, but not vice versa. This is allowed because a val property essentially declares a getter method, and overriding it as a var additionally declares a setter method in the derived class.

Note that you can use the override keyword as part of the property declaration in a primary constructor.

interface Foo {
    val count: Int
}

class Bar1(override val count: Int) : Foo

class Bar2 : Foo {
    override var count: Int = 0
}

3-Calling the superclass implementation

Code in a derived class can call its superclass functions and property accessors implementations using the super keyword:

open class Foo {
    open fun f() { println("Foo.f()") }
    open val x: Int get() = 1
}

class Bar : Foo() {
    override fun f() { 
        super.f()
        println("Bar.f()") 
    }

    override val x: Int get() = super.x + 1
}

Inside an inner class, accessing the superclass of the outer class is done with the super keyword qualified with the outer class name: super@Outer:

class Bar : Foo() {
    override fun f() { /* ... */ }
    override val x: Int get() = 0

    inner class Baz {
        fun g() {
            super@Bar.f() // Calls Foo's implementation of f()
            println(super@Bar.x) // Uses Foo's implementation of x's getter
        }
    }
}

4-Overriding Rules

In Kotlin, implementation inheritance is regulated by the following rule: if a class inherits many implementations of the same member from its immediate superclasses, it must override this member and provide its own implementation (perhaps, using one of the inherited ones). To denote the supertype from which the inherited implementation is taken, we use super qualified by the supertype name in angle brackets, e.g. 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()
    }
}

It’s fine to inherit from both A and B, and we have no problems with a() and b() since C inherits only one implementation of each of these functions. But for f() we have two implementations inherited by C, and thus we have to override f() in C and provide our own implementation that eliminates the ambiguity.

3-Abstract Classes

A class and some of its members may be declared abstract. An abstract member does not have an implementation in its class. Note that we do not need to annotate an abstract class or function with open – it goes without saying.

We can override a non-abstract open member with an abstract one

open class Base {
    open fun f() {}
}

abstract class Derived : Base() {
    override abstract fun f()
}

4-Companion Objects

In Kotlin, unlike Java or C#, classes do not have static methods. In most cases, it’s recommended to simply use package-level functions instead.

If you need to write a function that can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class.

Even more specifically, if you declare a companion object inside your class, you’ll be able to call its members with the same syntax as calling static methods in Java/C#, using only the class name as a qualifier.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值