有人能解释一下为什么我需要实例化子类吗?

huangapple go评论70阅读模式
英文:

Can someone explain why I have to instantiate subclass?

问题

为什么你在mymethod()中必须实例化subclass()?我以为我们只需要在main()方法中创建对象。(在这里也做了)为什么我们需要做两次?这里是否有继承的特殊属性?

class Super_class {
   int num = 20;

   // 超类的display方法
   public void display() {
      System.out.println("这是超类的display方法");
   }
}

public class Sub_class extends Super_class {
   int num = 10;

   // 子类的display方法
   public void display() {
      System.out.println("这是子类的display方法");
   }

   public void my_method() {
      // 实例化子类 - 为什么要这样做?
      Sub_class sub = new Sub_class();

      // 调用子类的display()方法
      sub.display();

      // 调用超类的display()方法
      super.display();

      // 打印子类变量num的值
      System.out.println("子类中变量num的值:" + sub.num);

      // 打印超类变量num的值
      System.out.println("超类中变量num的值:" + super.num);
   }

   public static void main(String args[]) {
      Sub_class obj = new Sub_class();
      obj.my_method();
   }
}
英文:

Why do you have to instantiate subclass() in the mymethod() ? I thought that we only need to make objects in the main() method.(which is done here too) Why do we have to do it twice? Is there a special property of inheritance here?

class Super_class {
   int num = 20;

   // display method of superclass
   public void display() {
      System.out.println("This is the display method of superclass");
   }
}

public class Sub_class extends Super_class {
   int num = 10;

   // display method of sub class
   public void display() {
      System.out.println("This is the display method of subclass");
   }

   public void my_method() {
      // Instantiating subclass - why do you have to do this?
      Sub_class sub = new Sub_class();

      // Invoking the display() method of sub class
      sub.display();

      // Invoking the display() method of superclass
      super.display();

      // printing the value of variable num of subclass
      System.out.println("value of the variable named num in sub class:"+ sub.num);

      // printing the value of variable num of superclass
      System.out.println("value of the variable named num in super class:"+ super.num);
   }

   public static void main(String args[]) {
      Sub_class obj = new Sub_class();
      obj.my_method();
   }
}

答案1

得分: 2

你不需要在子类上执行这些操作。当你在类内部时,无需实例化即可调用其属性和方法:可以直接调用,所以只需这样做就足够了:

public void my_method() {
    // 调用子类的 display() 方法
    display();

    // 调用父类的 display() 方法
    super.display();

    // 打印子类变量 num 的值
    System.out.println("子类中变量 num 的值:" + num);

    // 打印父类变量 num 的值
    System.out.println("父类中变量 num 的值:" + super.num);
}
英文:

You don't have to do these things you've done on the subclass. When you're inside the class you don't need to instantiate it to call its attribute and methods: you can do it directly, so doing like this is enough:

  public void my_method() {
  
  // Invoking the display() method of sub class
  display();

  // Invoking the display() method of superclass
  super.display();

  // printing the value of variable num of subclass
  System.out.println("value of the variable named num in sub class:"+ num);

  // printing the value of variable num of superclass
  System.out.println("value of the variable named num in super class:"+ super.num);
  }

答案2

得分: 0

你不必这样做,你可以使用子类的this关键字调用display方法,甚至在my_method()内部不需要子类的对象。

但是你在这里所做的事情是在my_method()内部创建了一个新对象,所以基本上你的my_method()方法和display()方法是由不同的对象调用的。

英文:

You do not have to do that, you can call the display method of the sub class with the this keyword or even without that inside my_method() you do not need object of sub class.

But what you have done here simply created a new object inside my_method(), so basically your my_method() method and display() methods are getting called by different object.

答案3

得分: 0

你不需要实例化一个新对象,事实上,我认为那是一种不好的编码习惯。你需要做的是使用 this 关键字引用当前对象,或者直接调用 display();,这隐式地意味着 this.display();。如果你创建一个新对象,如果有任何实例变量被设置,它们会重置为它们的默认值,因为你引用的实例不是调用该方法的实例,所以可能会出现意外的行为。

英文:

You don't need to instantiate a new object, and in fact, I would consider that a bad coding practice. What you need to do is reference the current object using the this keyword, or just calling display();, which implicitly means this.display();. If you create a new object, if there are any instance variables set, they would reset to their default values because the instance you are referencing is not the same instance the method was called on, so you could get unexpected behavior.

huangapple
  • 本文由 发表于 2020年5月30日 10:52:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定