kotlin 类继承的演示


   open class Person(name: String, age: Int) {
        var currentName = name;
        var age = age;

        init {
            "init call ${currentName} name:${name}"
        }

        fun hello() {
            println("hello method call  my name is:${this.currentName} ,i'm ${this.age} years old");
        }
    }

    class Zhang(name: String, age: Int) : Person(name, age) {

    }

    class Luo : Person {
        var interect: String? = null;

        constructor(name: String) : super(name, 0);
        constructor(name: String, birthday: Long, age: Int) : super(name, age);
        constructor(name: String, birthday: Long, interect: String, age: Int) : super(name, age) {
            this.interect = interect;

        }

        init {
            println("luo init call this.interect  ${this.interect}");
        }
    }

    class Li : Person {
        constructor(deathTime:Long, age: Int=1,name: String = "i am zhang") : super(name, age) {
            println(" Li constructor ${name}");
        }
    }

    class SuperMan : Person {
        fun productSuerMan(): SuperMan {
            return SuperMan();
        }

        private constructor() : super("qssq", 1000);
    }



    var person = Li(100);
    var person3 = Li(100, 1);
    var person2 = Li(100, 1, "hello");
    person.hello();
    person3.hello();
    person2.hello();
    val obj = TestStatic.getInstanceXXX()
    TestStatic.main()
    println("hello obj${obj}")

由于有默认值,应该是不能进行重载的,另外 没有默认值的参数必须放前面,否则报错
无论什么构造 在构造完成之后 都会走init方法,因此init里面 可以拿构造方法的值。

你可能感兴趣的:(kotlin 类继承的演示)