1> 普通类
// 主构造函数2 class Person constructor(name:String,age:Int){ // 次构造函数3 constructor(name:String,age:Int,sex:Boolean):this(name,age){ this.name = name; this.age = age; this.sex = sex; println("In second Constructor!"); } // 初始化方法1 init{ println("In init!"); } private var name:String = name; private var age:Int = age; private var sex:Boolean? = null; fun getName():String{ return name; } fun setName(name:String){ this.name = name; } fun getAge():Int{ return age; } fun setAge(age:Int){ this.age = age; } fun getSex():Boolean?{ return sex; } fun setSex(sex:Boolean){ this.sex = sex;; } } fun main(args: Array<String>) { var person1 = Person("A",15,false); // println(person1.name); // println(person1.age); // println(person1.sex); person1.setName("AAA"); println(person1.getName()); }2> 数据类
data class Student(var name:String,var age:Int); fun main(args: Array<String>) { var stu1 = Student("A",15); println(stu1.name); println(stu1.component1()); stu1.name = "AAA"; println(stu1.name); println(stu1.component1()); }