【问题标题】:constructor and hiding a filed in Java构造函数和隐藏Java中的字段
【发布时间】:2014-09-12 04:51:22
【问题描述】:

我正在做这个学校练习,我不明白为什么以下两种情况会产生不同的结果。有人可以解释为什么在第一种情况下int x 的 A 是 100? C 中的 int x 不是遮蔽了 A 中的 int x 吗?我也在代码中评论了我的问题。非常感谢!

class A {  // Case 1 
  public int x = 1;
  public String k() { return "A" + this.x; }
 }
class B extends A {
   public int x = 10;
}
class C extends B {
  public int x = 100;
  public String k() { return "C" + this.x; }
}
class TestABC {
  public static void main(String[] args) {
    A a1 = new C();
    C c1 = new C();
 } 
}
System.out.println(a1.x +"," + c1.x);// It prints out 1, 100.
   //However I thought it'd print out 100, 100, as the int x in C shadows int x in A. 

另一种情况是

class A1 { //case 2
  int x=10;
  public void method() {
  System.out.print("A:" + this.x + " ");
  }
}
class B1 extends A1 {
  int x=20;
public void method() {
  System.out.print("B:" + this.x + " ");
}
public static void main(String args[]) {
   A1 s = new B1();
   s.method();// It prints out B: 20. 
   //What's the difference between this one and a1.x in the previous example? 
   //Why it prints out the subclass but in the previous code a1.x prints out the parent class?
   }
} 

【问题讨论】:

标签: java inheritance binding constructor typing


【解决方案1】:

第一种情况打印出1, 100,因为它获取了 VARIABLE 的值 比如:

Get the value of x in A and get the value of x in C

覆盖仅发生在 METHODS 上,而不会发生在 VARIABLES 上。 这就是为什么在您的第二种情况下它调用 B1 而不是 A1 中的方法

这称为运行时多态。


编辑 2:

让我进一步解释一下:

当我们调用方法时,在运行时,JVM 不会查看引用变量的类型,而是查看对象的类型并运行该方法。

但是对于变量,情况正好相反,JVM 不是查看对象的类型,而是查看引用变量的类型 ,并获取该变量

举个例子:

class Animal {

    String whatAmI = "An Animal";

    public void sayHi() {
        System.out.println("Hi from Animal");
    }
}

class Dog extends Animal {

    String whatAmI = "A Dog";

    public void sayHi() {
        System.out.println("Hi from Dog");
    }
}

示例运行:

//Reference variable type               //Object type
Animal              anAnimal            = new Animal();
Dog                 aDog                = new Dog();
Animal              anotherDog          = new Dog();

anAnimal.sayHi();       //Hi from Animal
aDog.sayHi();           //Hi from Dog
anotherDog.sayHi();     //Hi from Dog

我们得到这些输出是因为 JVM 调用了 Object 类型而不是 Reference 变量类型的 sayHi() 方法。

但问题是,它与变量的工作方式不同。 如果我们尝试类似:

System.out.println(anAnimal.whatAmI);       //An Animal
System.out.println(aDog.whatAmI);           //A Dog
System.out.println(anotherDog.whatAmI);     //An Animal

我们有这些输出是因为 JVM 从 Reference 变量的类型中获取了 whatAmI 变量。

记住这一点的一个技巧是关注等号(=)。

  • 调用方法时,调用右侧的方法 等号 (=)。
  • 获取变量时,获取左边的变量 等号 (=) 的一侧。

【讨论】:

  • 那么在第一种情况下,当使用“A a1= new C();”启动 a1 时,它包含 int x= 1 ,但不包含 int x= 100 作为 C 类对象?谢谢!
  • 感谢您的回答!还有一个问题:那么在第二种情况下,s 仍然包含 int x=10?如果是这样当method() System.out.print("B:" + this.x + " ");被称为,“this”指的是什么?
  • 在第二种情况下,x 指的是 20,因为在您的 System.out.print() 方法中,您使用 this 并记住,this 指的是 当前正在执行的 类。在这种情况下是 C,所以我们得到 C 的 x 的值。
  • 好的,谢谢!你的意思是在第二种情况下,方法 System.out.print() 引用类 B1,即使没有包含 int x= 20 的对象,但 B1 仍然是执行类,所以变量 int x= 20用过吗?
【解决方案2】:

是的。这是运行时多态性或覆盖。

案例1:A a = new C(); 案例2:C c = new C();

多态性规则:

                                          Variable            Method          Static Method
Reference of Superclass(Case 1)             Super               Sub               Super
Reference of Subclass(Case 2)                Sub                Sub                Sub

这些是多态性的规则。因此,如果您创建这些类型的任何成员变量,您将始终看到这种格式的结果。

因此你看到的结果。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2013-02-22
    • 2013-10-29
    相关资源
    最近更新 更多