zoukankan      html  css  js  c++  java
  • JAVA--继承

      项目开发遇到一个问题,稀里糊涂给实现了,现在重点讨论一下这是个什么情况,大神们如果看到希望给点指点。

      问题:子类与父类具有相同的属性和方法,将子类实例化为父类,调用对应属性的get、set方法,打印出的信息显示了,子类的属性值,请问这是什么原因?

    代码如下--父类:

    public class Freath {
        
        private int a = 1;
    
        public int getA() {
            return a;
        }
    
        public void setA(int a) {
            this.a = a;
        }
    
    }

    子类A:

    public class A extends Freath{
        private int a = 2;
    
        public int getA() {
            return a;
        }
    
        public void setA(int a) {
            this.a = a;
        }
        
    }

    子类B:

    public class B extends Freath{
        private int a = 3;
    
        public int getA() {
            return a;
        }
    
        public void setA(int a) {
            this.a = a;
        }
    }

    测试类:

    public class Test {
    
        public static void main(String[] args) {
            Freath fa = new A();
            Freath fb = new B();
            System.out.println(fa.getA()+"");
            System.out.println(((A) fa).getA()+"");
            System.out.println(fb.getA()+"");
            System.out.println(((B) fb).getA()+"");
        }
        
    }

    输出结果:

      

      首先这样的结果,确实是我想要的,不过对于里面的具有逻辑,甚是不解,还望能得到指点。

      对于上面的情况我又进行了一下修改,打印出的结果就完全变了:

    父类:

    public class Freath {
        
        private int a = 1;
    
        public int getA() {
            return a;
        }
    
        public void setA(int a) {
            this.a = a;
        }
    
    }

    子类A:

    public class A extends Freath{
        private int a = 2;
    
    //    public int getA() {
    //        return a;
    //    }
    //
    //    public void setA(int a) {
    //        this.a = a;
    //    }
    
    }

    子类B:

    public class B extends Freath{
        private int a = 3;
    
    //    public int getA() {
    //        return a;
    //    }
    //
    //    public void setA(int a) {
    //        this.a = a;
    //    }
    
    }

    测试类:

    public class Test {
    
        public static void main(String[] args) {
            Freath fa = new A();
            Freath fb = new B();
            System.out.println(fa.getA()+"");
            System.out.println(((A) fa).getA()+"");
            System.out.println(fb.getA()+"");
            System.out.println(((B) fb).getA()+"");
        }
        
    }

    结果:

      

      对于这两种情况的产生,我的理解是,第一种情况属于:父类的属性是私有的,子类无法继承,父类的属性方法是公开的,子类可以进行继承重写,之所以出现上面的情况,就是父类进行相当于一个提供调用子类方法的入口,子类重写这些方法后,当调用时,便打印出了子类的属性值。第二种情况则是由于父类的属性是私有的,子类无法进行继承,子类继承了父类的方法,但没有进行重写,所以调用时,便将父类的信息打印出来了。

  • 相关阅读:
    Spring Session设计思路
    Jinja2 is a full-featured template engine for Python
    Computer Science:《The Essence of Winning and Losing》
    开源 敏捷 devops Lean Startup Huawei CRM
    BRMS ILOG
    日志易 IDS Snort 入侵检测
    理解和了解别人的正确姿势
    谷歌趋势 Google Trends
    OpenZipkin · A distributed tracing system
    Arthas OAM
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/5009314.html
Copyright © 2011-2022 走看看