Java 覆盖超类的实例变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/427756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:30:02  来源:igfitidea点击:

Overriding a super class's instance variables

javainheritance

提问by Warrior

Why are we not able to override an instance variable of a super class in a subclass?

为什么我们不能在子类中覆盖超类的实例变量?

采纳答案by cletus

Because if you changed the implementation of a data member it would quite possibly break the superclass (imagine changing a superclass's data member from a float to a String).

因为如果您更改数据成员的实现,它很可能会破坏超类(想象一下将超类的数据成员从浮点数更改为字符串)。

回答by Jon Skeet

Variables aren't accessed polymorphically. What would you want to do with this that you can't do with a protected variable? (Not that I encourage using non-private mutable variables at all, personally.)

变量不是多态访问的。你想用受保护的变量做什么?(并不是说我个人完全不鼓励使用非私有可变变量。)

回答by Adrian Zanescu

Because you can only override behavior and not structure. Structure is set in stone once an object has been created and memory has been allocated for it. Of course this is usually true in statically typed languages.

因为您只能覆盖行为而不能覆盖结构。一旦创建了对象并为其分配了内存,结构就一成不变。当然,这在静态类型语言中通常是正确的。

回答by Markus Lausberg

Do you mean with overriding you want to change the datatype for example?

例如,您的意思是覆盖要更改数据类型吗?

What do you do with this expression

你用这个表情做什么

public class A {
   protected int mIndex;

   public void counter(){
      mIndex++;
   }

}

public class B extends A {
   protected String mIndex; // Or what you mean with overloading
}

How do you want to change the mIndex++ expression without operator overloading or something like this.

您想如何在没有运算符重载或类似内容的情况下更改 mIndex++ 表达式。

回答by Toon Krijthe

If you have the need to override an instance variable, you are almost certainly inheriting from the worng class.

如果您需要覆盖一个实例变量,您几乎肯定是从wearg 类继承的。

In some languages you can hide the instance variable by supplying a new one:

在某些语言中,您可以通过提供一个新变量来隐藏实例变量:

class A has variable V1 of type X;

class B inherits from A, but reintroduces V1 of type Y.

The methods of class A can still access the original V1. The methods of class B can access the new V1. And if they want to access the original, they can cast themself to class A (As you see dirty programming provokes more dirty progrtamming).

A类的方法仍然可以访问原来的V1。B类的方法可以访问新的V1。如果他们想访问原始版本,他们可以将自己强制转换为 A 类(如您所见,脏编程会引发更脏的编程)。

The best solution is to find another name for the variable.

最好的解决方案是为变量找到另一个名称。

回答by Toon Krijthe

you can override a method,that is all right but what do you mean by overriding a variable? if you want to use a variable at any other place rather than super class u can use super. as in super(variable names); why do you want to override a variable? i mean is there any need?

你可以覆盖一个方法,那没问题,但是覆盖一个变量是什么意思?如果你想在任何其他地方使用一个变量而不是超类,你可以使用 super。就像在超级(变量名)中一样;为什么要覆盖变量?我的意思是有什么需要吗?

回答by Pierre D

He perhaps meant to try and override the value used to initializethe variable. For example,

他可能打算尝试覆盖用于初始化变量的值。例如,

Instead of this (which is illegal)

而不是这个(这是非法的)

public abstract class A {
    String help = "**no help defined -- somebody should change that***";
    // ...
}
// ...
public class B extends A {
    // ILLEGAL
    @Override
    String help = "some fancy help message for B";
    // ...
}

One should do

一个应该做

public abstract class A {
    public String getHelp() {
        return "**no help defined -- somebody should change that***";
    }
    // ...
}
// ...
public class B extends A {
    @Override
    public String getHelp() {
        return "some fancy help message for B";
    // ...
}

回答by dheeraj

we can not overriding structure of instance variables ,but we ovverride their behavior:-

我们不能覆盖实例变量的结构,但我们可以覆盖它们的行为:-

class A
{
int x = 5;
}

class B extends A
{
int x = 7:
}

class Main
{
public static void main(String dh[])
{
A obj = new B();
System.out.println(obj.x);
}
}

in this case output is 5.

在这种情况下,输出为 5。

回答by ssi-anik

class Dad{
    public String name = "Dad";
}
class Son extends Dad{
    public String name = "Son";
    public String getName(){
        return this.name;
    }
}

From main() method if you call

从 main() 方法调用

new Son().getName();

will return "Son" This is how you can override the variable of super class.

将返回“Son” 这是您可以覆盖超类变量的方法。