Java 抽象数据类型中方法调用的顺序?

发布于 2024-11-30 17:03:20 字数 686 浏览 0 评论 0原文

当您有一个实现了方法行为的抽象类以及没有实现任何行为时,方法调用的顺序是什么?

假设我的抽象类称为 Abs,它有两个子类,Sub1 和 Sub2

在情况 1 中,Abs 包含方法 Meth1 的实现代码

public abstract class Abs{

  public void Meth1(){
    //Some code
  }
}

在完全不同的类中,我有方法:

MyMethod(Abs a){
  a.Meth1();
}

其中我传递 Sub1 或 Sub2 作为 Abs 的替代品

在情况 2 中,Abs 不包含实现代码(但 Sub1 和 Sub2 包含),

public abstract class Abs{

  public abstract void Meth1();
}

并且我调用相同的代码:

MyMethod(Abs a){
  a.Meth1();
}

在传入 Sub1 或 Sub2 后。

每种情况下方法调用的顺序是什么?它总是先转到超类 Abs,然后再转到子类吗?是否先到子类,因为子类作为参数传入,然后JVM检查子类中是否有提供实现代码,如果没有,则有实现代码则调用超类方法?

What is the ordering of method invocation when you have an abstract class with method behaviour implemented and also when no behaviour is implemented?

Lets say my abstract class is called Abs and it has two subclasses, Sub1 and Sub2

In case 1, Abs contains the implementation code for method Meth1

public abstract class Abs{

  public void Meth1(){
    //Some code
  }
}

In a completely different class i have the method:

MyMethod(Abs a){
  a.Meth1();
}

where I pass either Sub1 or Sub2 as substitute for Abs

In case 2, Abs doesnt contain the implementation code (but Sub1 and Sub2 do)

public abstract class Abs{

  public abstract void Meth1();
}

and i call the same:

MyMethod(Abs a){
  a.Meth1();
}

after passing in either Sub1 or Sub2.

What is the ordering of the method calls in each case? Does it always go to the superclass Abs and then to the subclass? Does it go to the subclass first, because the sublclass was passed in as the parameter, then the JVM checks whether there is implementation code provided in the subclass and if not, then the superclass method is called if there is implementation code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-12-07 17:03:20

如果子类重写了方法,则根本不会调用父方法实现,除非子类有 super.overridenMethodName() 调用。它可以位于重写方法实现的开头或结尾或任何其他位置。

另一方面,如果抽象类具有抽象方法,则子类在编译时被迫实现它并提供一些逻辑。

If the subclass overrides a method, then the parent method implementation is not called at all, unless the subclass has a super.overridenMethodName() call. It can be at the beginning or the end or anywhere else of the overriden method implementation.

On the other hand, if an abstract class has an abstract method, then subclasses are forced at the compile time to implement it and provide some logic.

故事和酒 2024-12-07 17:03:20

对于非静态方法m,方法调用表达式om()是通过确定o引用的对象的运行时类来执行的。然后,调用该类的方法实现(在类本身内继承或定义)。

每个类都从其超类继承每个(可见)方法,除非它声明具有相同签名的方法,在这种情况下,声明的方法实现将覆盖继承的方法。

换句话说,调用的是最具体的超类中的实现。仅此而已;特别是,不会自动调用该方法的继承实现。当然,被调用的方法可能会使用 super.m() 依次调用任何继承的方法实现。

请注意,调用静态方法的规则是不同的。此外,虽然接收器的运行时类(在我们的示例中为 o)确定要调用的方法,但重载是使用参数的编译时类型而不是其运行时类型来解决的。

For a non-static method m, the method invocation expression o.m() is executed by determining the runtime class of the object referred to by o. Then, that class' method implementation (inherited or defined within the class itself) is invoked.

Every class inherits every (visible) method from its super class, unless it declares a method with the same signature, in which case the declared method implementation overrides the inherited one instead.

Put differently, the implementation in the most specific super class in invoked. That is all; in particular, inherited implementations of that method are not automatically invoked. The invoked method may, of course, in turn invoke any inherited method implementation using super.m().

Note that the rules are different for the invocation of static methods. Also, while the runtime class of the receiver (o in our example) determines the method to be invoked, overloading is resolved using the compile time types of the parameters, not their runtime types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文