• Java和Kotlin的Field在继承中的不同表现


    Kotlin是一个宣称与Java兼容性较好的语言,但在接触后发现一些技术还是有“概念上”的冲突,本文就记录下两者对象的Field(中文的说法有字段、域、属性、成员变量,下文若出现这些表达,指的都是这个东西)在继承中的不同表现。

    Java中Field在继承中的表现

    首先来看一段简单的程序:

    public class FieldInheritDemo {
        public static void main(String[] args) {
            Child child = new Child();
            Parent parent = child;
            System.out.println(child.a + "-" + parent.a);// => 8-1
            child.a = 88;
            System.out.println(child.a + "-" + parent.a);// => 88-1
            parent.a = 11;
            System.out.println(child.a + "-" + parent.a);// => 88-11
    
            System.out.println(child.b + "-" + parent.b);// => 9-2
            child.b = 99;
            System.out.println(child.b + "-" + parent.b);// => 99-2
    //        parent.b = 22; //error: 不能对final变量赋值
            parent.printParent();// => 11-2-11-2-88-99
            child.printChild();// => 88-99-88-99-11-2
        }
    }
    
    class Parent {
        public int a = 1;
        public final int b = 2;
    
        public void printParent() {
            System.out.println(a + "-" + b + "-" + this.a + "-" + this.b + "-" + ((Child) this).a + "-" + ((Child) this).b);
        }
    }
    
    class Child extends Parent {
        public int a = 8;
        public int b = 9;
    
        public void printChild() {
            System.out.println(a + "-" + b + "-" + this.a + "-" + this.b + "-" + super.a + "-" + super.b);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    从输出结果来看,Java的域有“遮蔽”的现象,但是没有“覆盖”或“重写”的现象。具体引用的是父类的域还是子类的域取决于变量的类型,而非对象的实际类型。this虽然是动态变量,但是在Parent中它仍然是this

    Kotlin中Field在继承中的表现

    同样来看一段和上面相似的程序:

    fun main(args: Array<String>) {
        val child: Child = Child()
        val parent: Parent = child
    
        println("${child.a}-${parent.a}")// => 8-8
        child.a = 88
        println("${child.a}-${parent.a}")// => 88-88
        parent.a = 11
        println("${child.a}-${parent.a}")// => 11-11
    
        println("${child.b}-${parent.b}")// => 9-9
        child.b = 99;
        println("${child.b}-${parent.b}")// => 99-99
    //    parent.b = 22; //error: 不能对val变量赋值
    
        parent.printParent()// => 11-99-11-99-11-99
        child.printChild()// => 11-99-11-99-1-2
    }
    
    open class Parent {
        open var a: Int = 1
        open val b: Int = 2
    
        fun printParent() {
            println("$a-$b-${this.a}-${this.b}-${(this as Child).a}-${(this as Child).b}")
        }
    }
    
    class Child : Parent() {
        override var a: Int = 8
        override var b: Int = 9;
    
        fun printChild() {
            println("$a-$b-${this.a}-${this.b}-${super.a}-${super.b}")
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    Kotlin中的输出结果来看,“遮蔽”、“覆盖”现象都存在,跟方法一样,其实只要看字节码就可以发现对Field的读写都是调方法,比如child.a = 88这行,字节码中就包含INVOKEVIRTUAL Parent.setA (I)V

    但是,Kotlin中有两个需要注意的点:

    1. super的行为还是和Java类似,并非Parent.setA之类的过程调用。
    2. openval同时修饰一个域的时候,这个域可能会变,例如上面parent.b,我们没法对其赋值,但是它的值却一直在变。(没错,不可变的值看上去变了。。。我很不喜欢这点设计,用的时候当心)
  • 相关阅读:
    会议OA项目之我的审批&&签字功能
    【笔记】Ningx(9)HTTPS
    大数据之Hudi数据湖_版本兼容与Maven安装配置_解决Hudi与Hadoop3.0的兼容问题_编译hudi源码---大数据之Hudi数据湖工作笔记0002
    A1034 Head of a Gang(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【DFS+图的遍历】
    微信小程序通过npm引入tdesign包进行构建的时候报错
    你必须要知道Mybatis中的OGNL表达式
    K最邻近法KNN分类算法(单点分类预测)
    电脑文件一团乱?试试这个高效率的管理软件
    (附源码)ssm失物招领平台 毕业设计 271621
    CSS 的快乐:画一个可爱的三只小鸟 Button
  • 原文地址:https://blog.csdn.net/zssrxt/article/details/132650180