简体   繁体   English

Java变量被遮挡的方法被覆盖的概念

[英]Java Variables Shadowed Methods overridden concept

I am struggling to understand Variables Shadowed Methods Overriden Concept of inheritance with Java. 我正在努力用Java了解Variables Shadowed Methods Overriden继承概念。

Case 1: 情况1:

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
}

Output: 8 Accelerate : Sportscar. 输出:8加速:跑车。

Case 2: 案例2:

public class TestClass{
   public static void main(String args[ ] ){
      A o1 = new C( );
      B o2 = (B) o1;
      System.out.println(o1.m1( ) );
      System.out.println(o2.i );
   }
}
class A { int i = 10;  int m1( ) { return i; } }
class B extends A { int i = 20;  int m1() { return i; } }
class C extends B { int i = 30;  int m1() { return i; } }

Output: 30, 20 产量:30,20

So if I understand correctly, the super class variable is always called unless the sub classes variable is called explicitly. 因此,如果我理解正确,除非明确调用子类变量,否则始终调用超类变量。 But the opposite is true for methods where the sub classes overridden method is called unless the super class is called explicitly. 但对于调用子类重写方法的方法则相反,除非显式调用超类。

I would think variables and methods should work the same or there should be a compile error when creating sub classes with the same variables. 我认为变量和方法应该相同,或者在创建具有相同变量的子类时应该存在编译错误。

Can someone explain if this is correct and why java works like this please. 有人可以解释这是否正确以及为什么java会像这样工作。

I would think variables and methods should work the same or there should be a compile error when creating sub classes with the same variables. 我认为变量和方法应该相同,或者在创建具有相同变量的子类时应该存在编译错误。

Well, that's simply not the way Java works. 嗯,这根本不是Java的工作方式。

Variables are not handled polymorphically - there's no concept of "overriding" a variable. 变量不是多态处理的 - 没有“覆盖”变量的概念。 Methods, however, are handled polymorphically. 然而,方法多态的。 Behaviour can be specialized, but not state . 行为可以是专门的,但不是状态

Note that if your variables are private, as they almost always should be, the situation is never even visible. 请注意,如果您的变量是私有的,因为它们几乎总是应该是,那么情况永远不可见。

In java there is no Instance variable overriding concept and variables are not polymorphic as methds. 在java中没有Instance variable overriding concept ,变量不像methd那样是多态的。

So in your case, if you use : 所以在你的情况下,如果你使用:

Car c = new SportsCar();  // c is Object reference of type Car
System.out.println( c.gearRatio+"  "+c.accelerate() );

c.gearRatio refers to the gearRatio in Car and not from SportsCar . c.gearRatio指的是Car的gearRatio,而not from SportsCar In case of method c.accelerate() , method is overridden in SportsCar and it is SportsCar object, so SportsCar.accelerate() is called. 在方法c.accelerate()情况下,在SportsCar重写方法并且它是SportsCar对象,因此调用SportsCar.accelerate()

public static void main(String args[ ] ){
  A o1 = new C( );  // object reference is of type A and object is C
  B o2 = (B) o1;   // object of C can be type casted to B as B is parent to C
  System.out.println(o1.m1( ) ); // C.m1() will be called.  
  System.out.println(o2.i );   // o2 is type B, so B.i will be printed
}

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

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