为什么超类的实例变量没有在子类方法中重写 [英] why instance variable of super class is not overridden in sub class method

查看:113
本文介绍了为什么超类的实例变量没有在子类方法中重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么超类的实例变量没有在子类方法中重写
请参阅下面的代码..在哪个方法中打印被覆盖但是变量a不是。
以及代码允许在子类中写入重复变量的原因..

Why instance variable of super class is not overridden in sub class method see my code below..in which method print is overridden but variable a is not. and why code allow to write duplicate variable in subclass..

class B
{
     int a=10;
     public void print()
     {
         System.out.println("inside B super class");
     }

}
 class C extends B
 {
     int a=20;
     public void print()
     {
         System.out.println("inside C sub class");
     }


 }
public class A  {
    public static void main(String[] args) {
        B b=new C();
        b.print();//it will print inside c sub class
        System.out.println(b.a);//it will print super class variable value=10


    }

}


推荐答案


为什么超类的实例变量未在子类方法中被覆盖,请参阅下面的代码...

Why instance variable of a superclass is not overridden in subclass method see my code below ...

因为实例变量不能在Java中重写。在Java中,只能覆盖方法。

Because instance variables CANNOT be overridden in Java. In Java, only methods can be overridden.

当您声明一个与超类中现有字段同名的字段时,新字段隐藏现有字段。超类中的现有字段仍然存在于子类中,甚至可以使用...遵循正常的Java访问规则。

When you declare a field with the same name as an existing field in a superclass, the new field hides the existing field. The existing field from the superclass is still present in the subclass, and can even be used ... subject to the normal Java access rules.


因为实例变量不能在Java中重写,但为什么呢?为什么在Java中以这种方式完成?是什么原因?

Because instance variables CANNOT be overridden in Java, but why? why is it done in this manner in Java? What's the reason?

他们为什么这样设计?

Why did they design it that way?


  1. 因为重写变量会从根本上破坏超类中的代码。例如,如果覆盖更改了变量的类型,则可能会更改使用原始变量的父类中声明的方法的行为。在最坏的情况下,它使它们无法编译。

  1. Because overriding variables would fundamentally break code in the superclass. For example, if an override changes the variable's type, that is likely to change the behavior of methods declared in the parent class that used the original variable. At worst, it renders them uncompilable.

例如:

   public class Sup {
       private int foo;
       public int getFoo() {
           return foo;
       }
   }

   public class Sub extends Sup {
       private int[] foo;
       ...
   }

如果 Sub。 foo 覆盖(即替换) Sup.foo getFoo()如何工作?在子类上下文中,它将尝试返回错误类型的字段的值!

If Sub.foo overrides (i.e. replaces) Sup.foo, how can getFoo() work? In the subclass context, it would be trying to return a value of a field of the wrong type!

如果被覆盖的字段不是私有的,它会更糟糕的是。这将以一种非常基本的方式打破Liskov Substitutability Principle(LSP)。这消除了多态性的基础。

If fields that were overridden were not private, it would be even worse. That would break the Liskov Substitutability Principle (LSP) in a pretty fundamental way. That removes the basis for polymorphism.

另一方面,覆盖字段无法实现任何无法做到的事情更好以其他方式。例如,一个好的设计将所有实例变量声明为private,并根据需要为它们提供getter / setter。可以重写getter / setter ,并且父类可以通过直接使用私有字段或声明getter / setter final

On the flipside, overriding fields would not achieve anything that cannot be done better in other ways. For example, a good design declares all instance variables as private and provides getters/setters for them as required. The getters/setters can be overridden, and the parent class can "protect" itself against undesirable overrides by using the private fields directly, or declaring the getters/settersfinal.






参考文献:


References:

  • Java Tutorial - Hiding Fields
  • JLS Example 8.3.1.1-3 - Hiding of Instance Fields.

这篇关于为什么超类的实例变量没有在子类方法中重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆