kotlin abstract class, sealed class, interface,

1. abstract class

(1) concept

From a plurality of classes with the same characteristics of an abstract modification using the abstract class to the class as a subclass of abstract template, thereby avoiding arbitrary subclass design. The design is an abstract class template pattern design pattern.

(2) features

  • Abstract class, abstract properties, do not need to open abstract method modifier, abstract and open modifier can not coexist.

  • An abstract class can not be instantiated, but the method has a structure, subclasses constructor is used.

  • An abstract class may contain: properties (abstract or non-abstract attribute property), a method (or a method of non-abstract methods abstract), constructor, initialization block, nested classes (interfaces, enumeration) 5 members.

  • Contains abstract members (including direct defines an abstract members, or inherit an abstract superclass but not fully realized the parent class contains abstract members, or implements an interface but does not fully implement abstract interface contains members of all three cases) only It can be defined as an abstract class.
  • abstract local variables can not be modified.

(3) members of the abstract (abstract methods, abstract properties)

Abstract modification using the abstract class in which methods and properties.

  • Abstract methods can not have a method body.

  • Abstract attribute initialization is not required.

(4) defines an abstract class

Here is the definition of the abstract class method

/**
 * 定义一个抽象类
 */
abstract class AbstractClass {
    //抽象属性,不能初始化
    abstract var abstractParam : String
    //抽象方法,不能有方法体
    abstract fun abstractFun()
    //普通属性,子类也可以重写,但是需要open修饰
    var commonParam : Int = 1
    //普通方法,子类也可以重写,但是需要open修饰
    fun commonFun() {
        //方法执行体
    }
    constructor() {
        //构造器,也可以在定义类时定义主构造器,抽象类的构造器是给子类用的,抽象类本身不能实例化
    }
}

The following is an example of a subclass inherits the abstract classes

fun main(args: Array<String>) {
    val fruit1 = Orange("橙子")
    val fruit2 = Apple()
    fruit1.eat()
    fruit2.eat()
}

/**
 * 水果抽象类
 */
abstract class Fruit {
    fun eat() {
        println("吃水果:$fruitName ,获取的水果重量${getFruitWeight()}")
    }
    abstract val fruitName : String
    abstract fun getFruitWeight() : Int
}

/**
 * 橘子子类
 */
class Orange(override val fruitName: String) : Fruit() {
    override fun getFruitWeight(): Int {
        return 10
    }
}

/**
 * 苹果子类
 */
class Apple : Fruit() {
    override fun getFruitWeight(): Int {
        return 20
    }

    override val fruitName: String
        get() = "苹果"

}

2. sealed class (special abstract class)

(1) concept

Seal is a special class abstract class, designed to be subclassed. The use of the sealed modifier modified.

(2) features

Subclass sealed class is fixed.

For the following reasons:

  • Subclasses must be sealed with a class file in the class with the seal.
  • In other files can not be sealed class subclass (subclass sealed class but may be other types of inherited).

(3) define a sealed class

/**
 * 密封类
 */
sealed class SealedClass {
    abstract fun sealedFun()
}

/**
 * 密封类子类1
 */
class SubClass1 : SealedClass() {
    override fun sealedFun() {
        
    }
}

/**
 * 密封类子类2(使用open修饰,可以在其他文件派生子类)
 */
open class SubClass2 : SealedClass() {
    override fun sealedFun() {
        
    }
}

3. Interface

(1) concept

Interface defines the system to interact with the outside window or specification, which defines the service (methods, properties) to achieve must provide to the outside world.

(2) features

  • Modifier: public | internal | private or omitted (i.e., a public omitted);
  • Interface can inherit interfaces, not inherited class;
  • Abstract interface / non-abstract methods defined;
  • Interface can define abstract properties / non-abstract properties, but the interface is not behind the abstract properties fee field (filed), and therefore need to provide them with get-set methods.
  • kotlin interface non-abstract members (methods, properties) can use public | private two access rights modification (java automatically modified to provide a public interface member, if you specify access rights can only be public), members can only use public abstract modification;
  • Interface not contain constructors and initialization block, but the interface may include: a method (abstract method / non-abstract methods), properties (abstract properties / attributes non-abstract), nested classes (interfaces or nested, nested enumeration);
  • kotlin interface abstracts the abstract keyword can be omitted member (abstract members abstract class can not be omitted);
  • Interfaces can not be instantiated, but can be used to declare variables to be assigned by the variable Any (upward transition, since the subclass object interface must Any subclass);
  • Abstract implementation class that implements the interface can only be a member of the public interface modification.

(3) defines the interface

fun main(args: Array<String>) {
    val person: Human = People("男", 5, 10)
    person.speak()
    (person as? Child)?.howOldAreYou()//向下转型为Child类型
    println("年级:${(person as? Student)?.grade}")//向下转型为Student类型
    val any : Any = person//person可以直接赋给Any
}

/**
 * 定义一个人类接口
 */
interface Human {
    //抽象属性,可省略abstract关键字,var type : String,只能使用public修饰
    abstract var type: String
    //非抽象属性,可以使用public|private访问权限符修饰
    val sex: String
        get() = type

    //非抽象方法,可以使用public|private访问权限符修饰
    fun speak() {
        if (canSpeak()) println("我是${sex}的") else println("不会说话")
    }

    //抽象方法,可省略abstract关键字,fun canSpeak() : Boolean
    abstract fun canSpeak(): Boolean
}

/**
 * 定义一个学生接口
 */
interface Student {
    val grade: Int
}

/**
 * 定义一个小孩抽象类
 */
abstract class Child {
    //抽象类的抽象属性,abstract不能省略
    abstract val age: Int
    //抽象类的费抽象方法(有方法体)
    fun howOldAreYou() {
        println("年龄:$age")
    }
}

/**
 * 定义Person类,继承于Child抽象类,实现了Student接口、Human接口,类后面的抽象类只能有一个,接口可以有多个,摆放顺序随意
 */
class People(override var type: String, override val grade: Int, override val age: Int) : Child(), Student, Human {
    override fun canSpeak(): Boolean {
        return true
    }
}

Guess you like

Origin www.cnblogs.com/nicolas2019/p/10940504.html