Kotlin's Learning Log (3) class construcor,interface,abstract class

Keywords: Java

If there are any mistakes, thank you for pointing out.
/**
 * Created by Gray on 2017/5/28.
 */
/*

    The keyword of a class in kotlin is class
        It consists of class name, class header (type parameter, main constructor, etc.) and class. If class has no body, curly brackets can be omitted.
                clasa A

         There may be a primary constructor or one or more auxiliary constructors in a class
               1. Principal constructor
                class A constructor(s:String)

                If there is no comment or visible modifier
                class A(s:String)

                There can be no code in the main constructor. The initialized code is placed in Inti.
                The parameters of the principal function can be used in it.
                class A(s:String){
                    init{
                         val s1 = s
                     }
                }

                Usually we can initialize them in the following ways
                class A(val s:String)

                If there are annotations or visible modifiers (more on that later)
                class A public @Inject constructor(s:String){}


                2. Second-order constructor
                    class A{
                        constructor(parent:A){
                            parent.child.add(this)
                        }
                    }

                    If a class has a principal constructor, then this auxiliary constructor is used
                    Delegate directly or indirectly to the principal constructor through another
                    class A{
                        constructor(i:Int,parent:A):this(i){
                            parent.child.add(this)
                        }
                    }

                    If a non-abstract class does not declare any constructors, it will have the primary constructor generated without parameters.
                    If you don't want your class to be a public constructor, declare an invisible constructor
                    class A private constructor(){}

                    If the parameters of the principal constructor have default values, then a constructor without parameters will be generated for you.

                    How to create instances of classes
                    val A = A()
                    val A = A("Hello")


        The parent class of all classes in Kotlin is Any. If your class does not inherit any classes, it will inherit Any by default.
            Any is different from Object in java. There are only equals(),hashCode(),toString() in Any.

            To declare an explicit supertype, we put the type after the colon in the class header
            open class A(a:Int)
            class B(a:Int):A(a)


            If the class does not have a primary constructor, each auxiliary constructor must initialize the base type using the super keyword, or delegate it to another constructor

            Everything in Kotlin is final, so you need to add open to the class so that it can be inherited.

            If some methods of the parent class need to be overridden, they need to be written as open, and override on the override method.
            The above rules are also common in attributes.
 */

/**
 * Created by Gray on 2017/5/28.
 */

class Person1{
    var name:String? = null
    init {
        name=""
    }
}

class Person2 constructor(name:String){
    init {
        println(name)
    }
}

class Person3(var name:String,var age:Int = 17){
    init {
        println("name:$name,age:$age")
    }
}

class Person4{
    constructor(name:String){
        println(name)
    }
    constructor(name:String,firstname:String,lastname:String){}
    constructor(name:String,firstname:String,lastname:String,age:Int){}
}

//Call the main constructor, overload the self-constructor
class Person5 constructor(name:String){
    constructor(name:String,firstname:String,lastname:String):this(name)
    constructor(name:String,firstname:String,lastname:String,age:Int):this(name)
}

class Person6 private constructor(name:String){
    constructor(name:String,firstname:String,lastname:String):this(name)
    constructor(name:String,firstname:String,lastname:String,age:Int):this(name,firstname,lastname)
}


open class A(a:Int){
}

class B(b:Int):A(b){}


open class View{
    init {

    }
}

class MyView: View {
    constructor()
}


open class Animal{
    open fun eat(){
        println("Animal eat")
    }
    fun sleep(){}
}

class People():Animal(){
    override fun eat() {
        super.eat()
    }
}
fun main(args:Array<String>){
    var people = People()
    people.eat()
}



/**
 * Created by Gray on 2017/5/29.
 */
//Interface cannot store state
interface Family {
    val id:Int
    fun name(name:String){}
    fun isFamily(){}
}

class MyFamily(override val id: Int = 0):Family{
    override fun name(name: String) {
        super.name(name)
    }

    override fun isFamily() {
        //<Family> Specifies which method is in which interface
        super<Family>.isFamily()
    }
}

//abstract class
abstract class Base{
    abstract fun f()
}

Posted by Arbitus on Sun, 16 Dec 2018 10:57:04 -0800