Java是面向对象的编程语言。继承是最重要的功能之一。
我们使用封装来隐藏对象状态。在下面的程序中,我应该使用private访问修饰符来设置年龄和计数器,但是为了测试这种继承,我应该使用public。


为什么在直接对象状态访问的情况下继承不起作用
(原始类型或对象引用)。 SOP 3的输出与预期的不同。
由于编译器在SOP 13和SOP 14行中未给出任何错误,因此为什么显示父亲类详细信息而不是子类。




public class Father {

    public int age = 50;
    /*
     * Counter keeps track of total no of instances created so far.
     */
    public static int counter = 0;

    public Father(){
        super();
        synchronized (Father.class) {
            ++Father.counter;
        }
    }

    public int getAge(){
        return this.age;
    }

    public static int getStaticCount(){
        return Father.counter;
    }

}

public class Child extends Father {

    public int age = 25;
    public static int counter = 0;

    public Child(){
        super();
        synchronized (Child.class) {
            ++Child.counter;
        }
    }

    public int getAge(){
        return this.age;
    }

    public static int getStaticCount(){
        return Child.counter;
    }

    public static void main(String args[]){

        Father father = new Father();
        Father child = new Child();
        Child realChild = new Child();

        System.out.println("Expecting Father Class details to be printed");
        System.out.println("SOP 1 : Father Age : "+father.age);     //prints 50 as expected.
        System.out.println("SOP 2 : Father Age : "+father.getAge());//prints 50 as expected.

        System.out.println("Expecting Child Class details to be printed");
        /*
         * Why inheritance does not work in case of direct integer access.
         */
        System.out.println("SOP 3 : Child Age : "+child.age); //prints 50 ?? , Father Age . Why ?
        System.out.println("SOP 4 : Child Age : "+child.getAge());//prints 25 as expected.

        System.out.println("Expecting Child Class details to be printed");
        System.out.println("SOP 5 : Child Age : "+realChild.age); //prints 25 as expected.
        System.out.println("SOP 6 : Child Age : "+realChild.getAge());//prints 25 as expected.

        /*
         *Total No of static Count : proper way of accessing static field using Class Name.
         */
        System.out.println("SOP 7 : Father Instance Count : Using Class Reference :"+Father.counter);
        System.out.println("SOP 8 : Father Instance Count : Using Class Reference :"+Father.getStaticCount());

        /*
         * Incorrect Way to use static. Since Compiler allows this lets see output.
         */

        System.out.println("SOP 9 : Father Instance Count : Using Object Reference :"+father.counter); //prints 3 as expected.
        System.out.println("SOP 10 : Father Instance Count : Using Object Reference :"+father.getStaticCount());//prints 3 as expected.

        /*
         *Total No of static Count : proper way of accessing static field using Class Name.
         */
        System.out.println("SOP 11 : Child Instance Count : Using Class Reference :"+Child.counter); // output is 2 as expected
        System.out.println("SOP 12 : Child Instance Count : Using Class Reference :"+Child.getStaticCount()); // output is 2 as expected

        /*
         * Incorrect Way to use static.Since Compiler allows this lets see output.
         * This invokes function of parent class. Why ? Inheritance does not work for static fields.
         */
        System.out.println("SOP 13 : child Instance Count : Using Object Reference :"+child.counter); // output is 3 but expected is 2 .          why ?
        System.out.println("SOP 14 : child Instance Count : Using Object Reference :"+child.getStaticCount()); // output is 3 but expected is 2 .  why ?

        /*
         * Incorrect Way to use static.Since Compiler allows this lets see output.
         * This invokes function of parent class. Why ?
         */
        System.out.println("SOP 15 : child Instance Count : Using Object Reference :"+realChild.counter); // output is 2 as expected
        System.out.println("SOP 16 : child Instance Count : Using Object Reference :"+realChild.getStaticCount()); // output is 2 as expected
    }

}


我的问题是为什么继承仅适用于实例方法。为什么SOP 3,SOP 13和SOP 14的输出不同

最佳答案

原因是多态性不适用于实例变量。您正在做的是可变阴影。

在SOP 3和4中,子类中具有相同名称的变量在基类中隐藏该变量。并且由于这是在编译时解决的,因此选择了静态类型的值。

在SOP 13和SOP 14中,发生这种情况的原因相同。在该方法的范围内,孩子的类隐藏变量是未知的。

从JSL:


  在一个成员中声明或继承的成员m的声明范围
  类类型C(第8.1.6节)是C的整个主体,包括任何嵌套的
  类型声明。

关于java - Java中的继承:对象状态和行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12440348/

10-09 00:39