类的继承

2019-08-16 12:16:12来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

类的继承

1.子类覆盖父类的成员变量

class A{
    String name = "tom";
    A(String name){
        this.name = name;
    }
}

class B extends A{
    String name = "jack";
    B(String name){
        super(name);
    }
}

public class BigInt {
    public static void main(String[] args) {
        B b = new B("helen");
        System.out.println(b.name);
    }
}

运行结果:

jack

 

---------------------------------------------------------------------------------------------------------------------------------------

class A{
    String name = "tom";
    A(String name){
        this.name = name;
    }
}

class B extends A{
    String name = "jack";
    B(String name){
        super(name);
    }
}

public class BigInt {
    public static void main(String[] args) {
        A b = new B("helen");
        System.out.println(b.name);
    }
}

运行结果:

helen

 

---------------------------------------------------------------------------------------------------------------------------------------

class A{
    String name = "tom";
    A(String name){
        this.name = name;
    }
}

class B extends A{
    String name = "jack";
    B(String name){
        super(name);
        this.name = name;
    }
}

public class BigInt {
    public static void main(String[] args) {
        A b = new B("helen");
        System.out.println(b.name);
    }
}

运行结果:

helen

 

---------------------------------------------------------------------------------------------------------------------------------------

class A{
    String name = "tom";
    A(String name){
        this.name = name;
    }
}

class B extends A{
    String name = "jack";
    B(String name){
        super(name);
        this.name = name;
    }
}

public class BigInt {
    public static void main(String[] args) {
        B b = new B("helen");
        System.out.println(b.name);
    }
}

运行结果:

helen

 


原文链接:https://www.cnblogs.com/sunzhongyu008/p/11329134.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:spring框架是怎么样通过properties来获得对象的?

下一篇:JUC AQS ReentrantLock源码分析(一)