Kotlin learn quick start (3) - class inherits the interface

class

Reference links

Class Definition Format

Using the class keyword to define the following format:

class T{
    //属性
    //构造函数
    //函数
    //内部类
}

Java Bean class

java bean class

//java bean类
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class java bean (Kotlin implemented)

//kotlin写法,get和set方法默认实现
class Student {
    /注意,这里的var代表着变量的数值之后可以被修改,也可以使用只读val
    //?是可为空的写法,后面会提到
    //这里其实包含了主构造方法,不过因为主构造方法为空,所以省略了
    var name: String? = null
    var age: Int = 0

    //这几个constructor是次级构造方法
    constructor() {}

    constructor(name: String) {
        this.name = name
    }

    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
    }
    
}

//下面是没有省略主构造方法的
//注意,因为把默认的主构造方法写了出来,所以,不允许出现无参数的次构造方法
class Student public constructor() {
    //注意,这里的var代表着变量的数值之后可以被修改,也可以使用只读val
    //?是可为空的写法,后面会提到
    //这里其实包含了主构造方法,不过因为主构造方法为空,所以省略了
    var name: String? = null
    var age: Int = 0

    //这几个constructor是次级构造方法
    constructor(name: String) : this() {
        this.name = name
    }

    constructor(name: String, age: Int)  : this(){
        this.name = name
        this.age = age
    }
}

The main function calls

//主函数调用
fun main(args: Array<String>){
    //声明类不需要new关键字
    val student = Student("star",12)
    //,使用对象.属性名调用,而不是使用get或set
    println(student.age)//获得属性
    student.name = "stars"//修改属性
}

Main constructor (Kotlin)

There may be primary and secondary constructor constructor kotlin class, tectonic those above method is the use of Java Bean classes kotlin implemented (above the main structure of the method is actually omitted)

/*下面三种方法都是声明了一个Student类
*Student包含了一个构造方法(两个参数),还有两个成员变量以及成员变量对应的get和set方法
*/

//原始方式,使用主构造方法
class Student public constructor(name: String,age: Int) {
    //注意,这里的var代表着变量的数值之后可以被修改,也可以使用只读val
    var name = name
    var age = age
}

//简洁写法,如果主构造方法只有默认的修饰符(public,默认的修饰符可省略),可以把constructor省略
class Student(name: String,age: Int) {
    var name = name
    var age = age
}

//更简洁写法
class Student(var name: String,var age: Int) {
}

//使用Student类
fun main(args: Array<String>) {
    //声明类不需要new关键字
    val student = Student("star",12)
    println(student.name)
    println(student.age)
    student.age = 19 //修改内容
    println(student.age)
}

init (initialization block)

If we want to perform an initialization operation in the main construction method, we need to write code in the code block inside init, such as

//更简洁写法
class Student(var name: String,var age: Int) {
    init{
        println("这里是初始化操作")
    }
}

Note that, the initialization part of the main block constructor

Initialization will be executed before the constructor times, even without the write main construction methods, such as:

class Student{
    var name: String? = null
    var age: Int = 0
    
    init{
        println("这里是初始化操作")
    }
    
    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
    }
}

Primary / secondary construction method used in combination

Constructor class defines the primary, secondary constructor must call the primary constructor directly or indirectly;

class Student() {
    var name: String? = null
    var age: Int = 0

    //这几个constructor是次级构造方法,,这里的this()就是当前的主构造方法
    constructor(name: String) : this() {
        this.name = name
    }

    constructor(name: String, age: Int)  : this(){
        this.name = name
        this.age = age
    }
}
class Student public constructor() {
    var name: String? = null
    var age: Int = 0

    //这几个constructor是次级构造方法,,这里的this()就是当前的主构造方法
    //这里用专业术语说,是次级构造方法需要委托给主构造方法
    constructor(name: String) : this() {
        this.name = name
    }

    constructor(name: String, age: Int)  : this(){
        this.name = name
        this.age = age
    }
}

Associated methods (static methods)

class Student{
    ...
    companion object {
        @JvmStatic
        //下面定义一些方法
        fun sayHello() {
            println("hello")
        }
    }
}

get / set methods to modify

After reading the above, we all know kotlin default to help us achieve the get and set methods, Val modified variable is read-only, so the variable no setter method

format:

var <propertyName>[: <PropertyType>] [= <property_initializer>]
    [<getter>]
    [<setter>]
//表达式写法
get() = ...
//花括号写法
get(){
    ...
    return xx
}   
class Student() {
    var name: String = ""
    var age: Int = 0
    //这里使用val
    val isNameEmpty: Boolean
        get() = name.length==0
 
    //使用var就得赋值
    //val isNameEmpty: Boolean
    //    get() = name.length==0
    
    constructor(name: String) : this() {
        this.name = name
    }

    constructor(name: String, age: Int)  : this(){
        this.name = name
        this.age = age
    }
}

If you want to refer to the current field (property value) and get in the set, was filed using keywords instead of content

class Student() {
    var name: String = ""
        //如果当前的name为"",则返回小红作为姓名
        //这里的filed就是name,类型也与name一样
        get() {
            return if(field.length==0) "小红" else field
        }
    var age: Int = 0

    constructor(name: String) : this() {
        this.name = name
    }

    constructor(name: String, age: Int)  : this(){
        this.name = name
        this.age = age
    }
}

setter and getter method, like the previous method, which is also using the field instead of the current value, but there is a parameter setter, default value, you can change the name

set(value){
    filed = vaule
}

Nested classes and inner class

And the difference between inner nested classes are classes, nested classes can not reference the outer class attributes and methods, and inner class

//Nested为嵌套类
class Outer {
    private val bar: Int = 1
    class Nested {
        //这里因为是嵌套类,无法引用Outer中的bar
        fun foo() = 2
    }
}

val demo = Outer.Nested().foo() // == 2

Inner classes, use the inner keyword

class Outer {
    private val bar: Int = 1
    inner class Inner {
        //这里可以引用bar
        fun foo() = bar
    }
}

val demo = Outer().Inner().foo() // == 1

Inheritance and Interfaces

inherit

kotlin all classes are inherited Any, note, the Any not java.lang.Object

kotlin the default class is not inherited, the need for openkeyword modified class, need to subclass replication, but also have the parent class using openthe modified method

open class Person{
    var name: String = ""
    var age: Int =0
    
    constructor(){}
    constructor(name: String, age: Int){
        this.name = name
        this.age = age
    }

    open fun hello() {
        println("hello this is person")
    }
}

class Student: Person {
    constructor() : super(){}
    constructor(name: String,age: Int) :super(name,age){}

    override fun hello() {
        println("hello this is student")
    }
}

interface

Implementation of the interface is to use :, the interface is the interface declaration keyword, note that the interface method can be implemented in kotlin

interface Print {
    fun print()
    fun say(){
        println("sayhello")
    }
}

Student class inherits Person Print and implement interfaces:

class Student: Person,Print {
    override fun print() {
        //复写接口里的方法
    }

    constructor(name: String,age: Int) :super(name,age){}

    override fun hello() {

    }
}

Data Class

Introduction

kotlin provides a data class, special class data storage, data modification using the keyword class

The official data on class specifications:

  • Primary constructor needs to have at least one parameter;
  • All the main parameters to be marked as a constructor or var val;
  • Data can not be abstract class, open, or internal seal;
  • (Before 1.1) data can only achieve interfaces.

There are two main classes of data the following features:

  • Automatically parse
  • Direct Copy

Data class definitions

data class Person(var name: String,var age: Int){
}

Automatically parse

Automatic sequential parse, according to the attribute class attribute

val person = Person("star",19)
//val括号里可以根据需要选择不同参数,注意顺序,不能跳过一个不需要的属性
val(name,age) = person
println("$name, $age years of age") // 输出 "star, 19 years of age"

copy

val person = Person("star",19)
val person1 = person.copy()
//复制并修改部分属性
val person2 = person.copy(age =23)

to sum up

Personally I feel, if a class has only one constructor, you can define a class constructor contains only a master can, using the most concise way.

If you need to have a different argument constructor (or Java Bean), the secondary structure using the method

If it is used as a data class using the class definition data

Guess you like

Origin www.cnblogs.com/kexing/p/11262112.html