Kotlin Kotlin] [Object Oriented (class | member variables | member methods | package | inheritance | polymorphism)



I. Create a simple class (integer field)



Defined categories:


① the definition of class: keyword defines class is class ;

② defined fields: in parentheses after the class, using var field name: field type can define a field;


/**
 * 定义 Rect 类 : 定义类的关键字是 class
 *
 * 类字段 : 该类对象有两个属性 , 宽 ( width ) 和 高 ( height ) , 类型都是 int
 *
 */
class Rect (var width:Int , var height : Int)

/**
 * 函数入口
 */
fun main(args:Array<String>){
    //创建矩形
    var rect = Rect(10, 8);

    //使用字符串模板打印 Rect 对象字段
    println("Rect width : ${rect.width} , height : ${rect.height}")
}

Results of the :

Rect width : 10 , height : 8


II. Create a Simple (+ integer field string field)



Defined categories:


① the definition of class: keyword defines class is class ;

② defined fields: in parentheses after the class, using var field name: field type can define a field;


/**
 * 定义学生类
 *
 * 定义字段 : 其含有一个字符串字段 ,
 */
class Student ( var name:String , var age:Int )

fun main(args:Array<String>) {

    //创建 Student 对象
    var student = Student("Tom", 18)

    //使用字符串模板打印 Student 对象字段
    println("student name : ${student.name} , age : ${student.age}")

}

Results of the :

student name : Tom , age : 18


III. The method defined in class



Class method definitions: Use the keyword fun, defined in the class method, this method can access the member variables of an object;

/**
 * 定义学生类
 *
 * 定义字段 : 其含有一个字符串字段 ,
 */
class Student ( var name:String , var age:Int ){

    fun talk(){
        println("Student ${name} is talking")
    }

}

fun main(args:Array<String>) {

    //创建 Student 对象
    var student = Student("Tom", 18)

    //使用字符串模板打印 Student 对象字段
    println("student name : ${student.name} , age : ${student.age}")

    //调用类中的方法
    student.talk()

}

Results of the :

student name : Tom , age : 18
Student Tom is talking


IV. Package



Package effect: hide the details of the properties and methods implemented inside the object, only the public interface open to the public;


Such as: Student, a package name and age fields, methods and talk;



V. Inheritance



1 parent:. Open keywords:


① Open Inheritance: Only use open keyword before the class, the class is only open to have a class that inherits the class;

② method overrides allowed: allowed rewriting method, only open function can overridden by subclasses;


/**
 * 定义父类
 *
 * open 关键字作用 : 打开继承 , 只有打开了该类 , 才能有类继承该类
 *                 允许方法重写 , 只有 open 的函数 , 才能被子类重写
 */
open class Father {

    var familyName:String = "Trump"

    fun name(){
        println("My family name is ${familyName}")
    }

    //允许方法被重写
    open fun say(){
        println("I'm father. family : ${familyName}")
    }

}

2 subclasses: class inherits and overrides methods;

/**
 * 子类代码
 *
 * 继承 : 使用 冒号 ":" , 表示继承
 */
class Son : Father() {

    //重写父类方法
    override fun say(){
        println("I'm son. family : ${familyName}")
    }
}

3. Test Inheritance:

/**
 * 测试继承
 */
fun main() {

    //创建子类对象
    var son = Son();

    //调用子类对象继承的父类的方法
    son.name();

    //调用子类对象重写的父类方法
    son.say();
}

4. The results:

My family name is Trump
I'm son. family : Trump


VI. Polymorphism and abstract classes



1 defines an abstract class: only in the abstract class to define abstract method (unlike Java);

/**
 * 抽象类 : 只有抽象类中才能定义抽象方法 ( 与 Java 不同 )
 */
abstract class Human ( var name:String , var age:Int ) {

    /**
     * 定义抽象方法 , 类必须是抽象类
     */
    abstract fun say();

}

2 for Type 1:

/**
 * 定义 Man 类 , 继承抽象类 Human
 */
class Man(name : String, age : Int) : Human(name, age) {

    /**
     * 实现的 Human 抽象类 的 say 抽象方法
     */
    override fun say() {
       println("Man : name : ${name} , age : ${age}")
    }
}

3 implementation class 2:

/**
 * 定义 Women 类 , 继承抽象类 Human
 */
class Woman(name : String, age : Int) : Human(name, age) {

    /**
     * 实现的 Human 抽象类 的 say 抽象方法
     */
    override fun say() {
        println("Women : name : ${name} , age : ${age}")
    }
}

4. Polymorphic introduced: Man and Women method calls say, different operations performed polymorphism is reflected;

/**
 * 测试抽象类与多态
 */
fun main() {

    // Man 和 Women 调用 say 方法 , 执行的不同的操作
    //  这就是多态

    var tom = Man("Tom", 18);

    //Man : name : Tom , age : 18
    tom.say();

    var jerry = Woman("Jerry", 19);

    //Women : name : Jerry , age : 19
    jerry.say();

}
Published 307 original articles · won praise 1043 · Views 1.7 million +

Guess you like

Origin blog.csdn.net/han1202012/article/details/104527342