简体   繁体   English

如何在超类中更改变量

[英]how can I change variables in super class

I want to write a program in java that consists of three classes, A , B , and C , such that B extends A and C extends B . 我想在java中编写一个由三个类ABC ,这样B扩展AC扩展B Each class defines an instance variable (named "x"). 每个类都定义一个实例变量(名为“ x”)。

how can I write a method in C to access and set A's version of x to a given value, without changing B or C's version? 如何在C编写一个方法来访问并将A's x版本设置为给定值,而不更改BC's版本?

I tried super.x but It wasn't true. 我尝试了super.x但事实并非如此。

any help? 任何帮助? thanks for your attention in advance 感谢您的提前关注

You can access A's version of x like this: 您可以像这样访问A的x版本:

((A)this).x

as long as x wasn't declared private in class A . 只要xA类中没有声明为private I've just tested it. 我刚试过它。

Note that for fields, there is no overriding (as there is for methods). 请注意,对于字段,没有覆盖(就像方法一样)。 Thus, for an object of class C, there will be three x fields, but two of them can't be accessed normally because they are hidden by the other field named x . 因此,对于C类对象,将有三个x字段,但是其中两个字段无法正常访问,因为它们被名为x的另一个字段隐藏。 But casting the object as above will allow you to get at it, if it would have been visible if not hidden. 但是如上所述,如果没有隐藏,那么如上所述将对象转换为允许你获取它。

I think it is very poor practice to declare fields of the same name in a class and its subclasses. 我认为在类及其子类中声明同名字段是非常糟糕的做法。 It's confusing. 这令人困惑。 It can happen legitimately if, say, you have a class A and you later change the implementation of A and add a new private field z ; 如果您拥有A类并且稍后更改A的实现并添加新的私有字段z ,那么它可以合法地发生; in that case, it may not be possible to make sure no subclasses of A already have a field z , since you don't even always know what all the subclasses are (if A is a class you've distributed publicly, for instance). 在这种情况下,可能无法确保A子类都没有字段z ,因为您甚至不总是知道所有子类是什么(例如,如果A是您公开分配的类) 。 I think it's for that reason that Java allows you to have fields of the same name, and why the hiding rules are the way they are, because it allows things like this to work without breaking all the other subclasses. 我认为正是出于这个原因,Java允许您使用相同名称的字段,以及隐藏规则为何如此使用,因为它允许这样的事情在不破坏所有其他子类的情况下起作用。 Other than that, though, I recommend not having fields of the same name in superclasses and subclasses. 除此之外,我建议不要在超类和子类中使用相同名称的字段。 Perhaps if they're all private it might be OK, though. 也许,如果他们都是private那可能没问题。

I misread your question. 我误解了你的问题。 You can't do what you're trying to do. 你不能做你想做的事。

Extending classes means adding information in several layers, ultimately resulting in one object. 扩展类意味着在多个层中添加信息,最终产生一个对象。 Although there are multiple layers, this doesn't mean that the layers are separate of eachother. 尽管存在多个层,但这并不意味着这些层彼此分开。

The variable X will be defined at one level (probably A ) and after that the other classes will use this variable (if it's declared protected ), but they won't have their own copy of it. 变量X将在一个级别(可能是A )上定义,然后,其他类将使用该变量(如果声明为protected ),但是它们将没有自己的副本。 You can only access your direct superclass. 您只能访问直接超类。
This class might give you additional access to its own superclass, but you don't have direct contact with the super-super class. 此类可能为您提供访问其自己的超类的其他权限,但您没有与超超类的直接联系。

Do the following 请执行下列操作

public static void main(String[] args) throws Exception {
    C c = new C();
    System.out.println("c:" + c.x);
    System.out.println("a:" + ((A)c).x);
    c.changeAX();
    System.out.println("c:" + c.x);
    System.out.println("a:" + ((A)c).x);
}

static class A {
    int x;
}

static class B extends A {
    int x;
}

static class C extends B {
    int x;

    public void changeAX() {
        ((A)this).x = 4;
    }
}

Fields are resolved relative to the declared type of the reference. 相对于声明的引用类型解析字段。 The above prints 以上打印

c:0
a:0
c:0
a:4

The field will have to have at least protected visibility. 该字段必须至少具有protected可见性。


You don't want to be hiding class members, it's bad practice because it can easily confuse anyone trying to figure out which member you are referring to. 你不想隐藏班级成员,这是不好的做法,因为它很容易混淆任何试图找出你所指的成员的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM