• 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,我们没法对其赋值,但是它的值却一直在变。(没错,不可变的值看上去变了。。。我很不喜欢这点设计,用的时候当心)
  • 相关阅读:
    第十三届蓝桥杯c++b组-积木画
    Rhodamine/Cy3/Cy3.5/Cy5/FITC荧光素标记羧甲基纤维素CMC/羧甲基壳聚糖/羧甲基凝胶多糖
    身份证实名认证接口的三种方式、C#实名认证接口
    设计原则之【单一职责原则】
    SpringBoot篇---第三篇
    【使用Cpolar将Tomcat网页传输到公共互联网上】
    新闻api接口,新闻资讯,社交媒体,体育赛事,全国热门带正文新闻查询API接口
    Maven概述
    开源|用 Java 实现一个生成 Markdown 文本的工具
    9.19作业
  • 原文地址:https://blog.csdn.net/zssrxt/article/details/132650180