简体   繁体   English

访问父类的变量显示子类变量

[英]Accessing the variable of parent class shows subclass variable

I encountered an example which is not at all clear to me. 我遇到了一个我完全不清楚的例子。 In this example result is : feline cougar cc 在此的示例结果是:猫美洲狮cc

But we call in method super.type. 但是我们调用方法super.type。

class Feline {
   public String type = "f ";
   public Feline() {
   System.out.print("feline ");
   }
 }
 public class Cougar extends Feline {
   public Cougar() {
     System.out.print("cougar ");
      }
      void go() {
       type = "c ";
        System.out.print(this.type + super.type);
     }
      public static void main(String[] args) {
        new Cougar().go();
         }
        }

I expected feline cougar c f. 我期望猫美洲狮c f。

There is only one variable. 只有一个变量。 However you access that variable, it will give the same value. 但是,如果您访问该变量,它将给出相同的值。

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

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