简体   繁体   English

在超类中调用方法使用子类的对象如何工作?

[英]calling method in super class using object of sub class how it work?

class SuperClass{  
  public void print(){
    System.out.println("I'm super class...");
  }  
  public void someMethod(){
    System.out.println("any thing");
  }
}  

class SubClass extends SuperClass{ 
  @Override
  public void print(){
    System.out.println("I'm sub class...");
  }  

  public static void main(String args[]){  
    SuperClass a=new SubClass(); 
    a.print();  
    a.someMethod();
  }  
}  

a.print() known as dynamic binding and it's know which method to call in run time and choose method of SubClass because the object is SubClass it's true? a.print()称为动态绑定,它知道在运行时调用哪个方法并选择SubClass的方法,因为该对象是SubClass它是真的吗?

a.someMethod() how JVM deal with this method it's not in sub class and the object from sub class? a.someMethod() JVM如何处理这个方法,它不是在子类中而是来自子类的对象?

When you do: 当你这样做时:

SuperClass a = new SubClass();

You're saying that you want to make an instance of SubClass , where you only want methods declared in SuperClass to be visible. 您说要创建SubClass的实例,您只希望在SuperClass声明的方法可见。 If you had declared any new methods in SubClass , they would not be visible. 如果您在SubClass声明了任何新方法,则它们将不可见。

The object will still be using the SubClass implementations when it can (ie when you override them). 该对象仍然可以使用SubClass实现(即当你覆盖它们时)。 Methods which haven't been overridden will use the implementation that was inherited. 尚未重写的方法将使用继承的实现。

a.print() is an example of method overriding which is runtime polymorphism . a.print()方法重写的一个例子,它是运行时多态性 That is, at runtime it decides whether to call the subclass method or the super class method (here your SubClass method is called as it is overridden and matches the parameter signature). 也就是说,在运行时它决定是调用子类方法还是超类方法(这里调用SubClass方法,因为它被覆盖并匹配参数签名)。

In case of someMethod() call, you have not defined it in your SubClass. someMethod()调用的情况下,您尚未在SubClass中定义它。 That means, your SubClass automatically got a someMethod() when you inherited it from Superclass as it is public . 这意味着,当您从Superclass继承它时,您的SubClass自动获得someMethod() ,因为它是public Putting it in simple words, SubClass also has the definition of someMethod() . 换句话说,SubClass也有someMethod()的定义。 Thats how JVM got to know which method to call. 这就是JVM如何知道调用哪种方法。

Also in SuperClass a , the variable 'a' will be stored in stack whereas the reference will be stored in heap. 同样在SuperClass a ,变量'a'将存储在堆栈中,而引用将存储在堆中。 You have constructed the reference using SubClass 's constructor. 您已使用SubClass的构造函数构造了引用。 Meaning the variable 'a' in stack refers to a ' SubClass ' object in heap. 意味着堆栈中的变量“a”指的是堆中的“ SubClass ”对象。 That is why 'a' is able to call someMethod 这就是'a'能够调用someMethod

"calling method in super class using object of sub class" is one of the scenario of Polymorphism in Java . “使用子类对象调用超类中的方法”是Java多态性的场景之一。

If the sub-class implements a method with same definition as that of super-class, calling the method with the object of sub-class is going to override the method of super-class with the method of sub-class. 如果子类实现的方法与超类的定义相同,则使用子类的对象调用方法将使用子类的方法覆盖超类的方法。

Hence, if you want to call the method of super class with the object of sub-class, don't override the method. 因此,如果要使用子类的对象调用超类的方法,请不要覆盖该方法。

SuperClass a=new SubClass();

This definition makes "a" capable to make a reference to type SuperClass or any sub-class that extend the SuperClass. 这个定义使得“a”能够引用类型SuperClass或任何扩展SuperClass的子类。 As object "a" is created from SubClass, by definition, the object can access the methods of the SubClass and SuperClass. 对象“a”是从SubClass创建的,根据定义,该对象可以访问SubClass和SuperClass的方法。 This explaination should address both of your questions. 这个解释应该解决你的两个问题。

Refer to the java documentation here . 请参阅此处的java文档。

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

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