简体   繁体   English

从子类更改超类实例变量

[英]Change superclass instance variables from subclass

I got this task and I can't quite figure out how to solve it: "Change all three of the x-variables related to the C-class." 我完成了这项任务,但我还不太清楚如何解决它:“更改与C类相关的所有三个x变量。”

class A {
    public int x;
}

class B extends A {
    public int x;
}

class C extends B {
    public int x;

    public void test() {
        //There are two ways to put x in C from the method test():
        x = 10;
        this.x = 20;

        //There are to ways to put x in B from the method test():
        ---- //Let's call this Bx1 for good measure.
        ---- //Bx2

        //There is one way to put x in A from the method test();
        ---- //Ax1
    }
}

To test, I set up this: 为了测试,我设置了这个:

public class test {
    public static void main(String[] args)
    {
        C c1=new C();
        c1.test();
        System.out.println(c1.x);

        B b1=new B();
        System.out.println(b1.x);

        A a1=new A();
        System.out.println(a1.x);
    }
}

Which gives 20, 0, 0. 得出20、0、0。

Now, I figured out I could write Bx1 like this: 现在,我知道可以这样写Bx1

super.x=10;

That would change the x in B , but I could not figure out how to call it in my test.java . 那会改变Bx ,但是我不知道如何在test.java调用它。

How do you get Bx1 , Bx2 , Ax1 , and how do you call them for a test? 您如何获得Bx1Bx2Ax1以及如何称呼它们进行测试?

You can access the superclass's version of x by using a superclass type reference: 可以通过使用超类类型引用访问超类的x版本:

System.out.println("A's x is " + ((A)this).x);

That will get A#x . 那将得到A#x

But in general, it's a very bad idea to shadow a superclass's public instance members. 但是总的来说,隐藏超类的公共实例成员是一个非常糟糕的主意。

Example: ( live copy on IDEOne ) 示例: IDEOne上的实时副本

class Example
{
    public static void main (String[] args) throws java.lang.Exception
    {
        new C().test();
    }
}

class A {
    public int x = 1;
}

class B extends A {
    public int x = 2;
}

class C extends B {
    public int x = 3;

    public void test() {
        //There are two ways to put x in C from the method test():
        System.out.println("(Before) A.x = " + ((A)this).x);
        System.out.println("(Before) B.x = " + ((B)this).x);
        System.out.println("(Before) C.x = " + this.x);
        ((A)this).x = 4;
        System.out.println("(After) A.x = " + ((A)this).x);
        System.out.println("(After) B.x = " + ((B)this).x);
        System.out.println("(After) C.x = " + this.x);
    }
}

Output: 输出:

(Before) A.x = 1
(Before) B.x = 2
(Before) C.x = 3
(After) A.x = 4
(After) B.x = 2
(After) C.x = 3

this is how your test method could look like 这就是您的测试方法的样子

void test() {
    this.x = 30;
    A a = this;
    a.x = 10;
    B b = this;
    b.x = 20;
}

It´s important to note, that you are accessing the variable of the Type of the class that you defined, so in that case you would Access, x from A , and x from B by defining a variable due to the this keyword. It's重要的是要注意,你正在访问您所定义的类的类型的变量,所以在这种情况下,你将访问, xA ,和xB通过定义一个变量,由于this关键字。

Using getters and setters 使用getter和setter

class A { A类{

public int x;

} }

class B extends A { B类扩展了A {

public int x;

public void setAx(int x) {
    super.x = x;
}
public int getAx() {
    return super.x;
}

} }

class C extends B { C类扩展到B {

public int x;

public void test() {

    x = 10;
    this.x = 20;

}
public void setBx(int x){
    super.x = x;
}
public int getBx() {
    return super.x;
}

public static void main(String[] args)
{
    C c1= new C();
    c1.x = 1;
    c1.setAx(2);
    c1.setBx(3);

    System.out.println(c1.getAx()+"/"+c1.getBx()+"/"+c1.x);
}

} }

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

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