简体   繁体   English

我怎么知道何时/何地调用超类的重写方法

[英]how do I know when/where to invoke the overridden method of the super class

This question occured to me while programming a Android application, but it seems to be a general programming question more. 在编写Android应用程序时,这个问题出现了,但它似乎是一个更普遍的编程问题。

The situation is, I am extending (subclass-ing) an class from a library, and overriding a method. 情况是,我正在从库中扩展(子类化)一个类,并覆盖一个方法。 how do I know if I should invoke the method of super-class? 我怎么知道我是否应该调用超类的方法? and when? 什么时候? (in the beginning of the overridden method or in the end?) (在重写方法的开头或最后?)

For example, I am overriding the method "public boolean onCreateOptionsMenu(Menu menu)" from class "Activity" in Android platform. 例如,我在Android平台的类“Activity”中覆盖了方法“public boolean onCreateOptionsMenu(Menu menu)”。 And I saw someone write "return super.onCreateOptionsMenu(menu)" in the end of the method, in an example. 在一个例子中,我看到有人在方法的最后写了“return super.onCreateOptionsMenu(menu)”。 But how do I know it should be done this way? 但我怎么知道它应该这样做? and it is correct or not? 这是对还是不对? what's the difference if I begin my method with "super.onCreateOptionsMenu(menu)"? 如果我用“super.onCreateOptionsMenu(menu)”开始我的方法有什么区别?

BR, Henry BR,亨利

I don't think you can answer this question in the abstract: it depends on the behavior of the superclass method you're overriding. 我不认为你可以抽象地回答这个问题:它取决于你所覆盖的超类方法的行为。

Depending on circumstances, it may be appropriate to: 根据具体情况,可能适合:

  • call super first 先打电话给超级
  • call super last 最后打电话
  • handle some cases yourself (customizations), call super for the rest 自己处理一些情况(自定义),其余部分调用super
  • never call super 从不打电话给超级

Hopefully the documentation for the particular class you're overriding will tell you if/when it's necessary to call super. 希望您所覆盖的特定类的文档将告诉您是否/何时需要调用super。

Unfortunately, there is no rule for this. 不幸的是,这没有规则。 You need to refer to the API docs and call super if the docs say you need to. 如果文档说你需要,你需要参考API文档并调用super。

One hint as to whether you'll probably need to or not in Android's case is if the method you're overriding is one of the lifecycle methods. 关于你是否可能需要或不需要Android的一个提示是,你所覆盖的方法是生命周期方法之一。 In this case, you can be fairly certain that you need to call super. 在这种情况下,您可以相当确定需要调用super。

These are valid questions, but unfortunately there is no general rule to follow here. 这些是有效的问题,但不幸的是,这里没有一般规则。

Whether or not you need to call super method depends on the fact if the super method does something that needs to be done. 是否需要调用super方法取决于super方法是否需要执行某些操作。 In other words: are you extending or replacing the overridden method? 换句话说:您是在扩展还是替换被覆盖的方法? A good API documentation of the class should give you the answer. 一个好的API文档应该给你答案。 Also, libraries often follow some conventions to make it clear how to use them. 此外,图书馆通常遵循一些惯例来明确如何使用它们。

The answer to the question where to place the super call depends on when you want to execute you're extension. 问题在哪里发出超级电话的答案取决于你想要执行扩展的时间。 Does it need to run before or after the super method? 是否需要在超级方法之前或之后运行? Most often you first call super and then do something extra. 大多数情况下,你首先打电话给超级,然后做额外的事 But if you need to prepare something for the super method, for example modifying some object state or manipulate the arguments, you place the code before the super call. 但是如果你需要为super方法准备一些东西,例如修改一些对象状态或操纵参数,你可以在超级调用之前放置代码。 Again, the API documentation should give you the answer here. 同样,API文档应该在这里给出答案。

Android will in most of the cases cause a exception if you forget to call super. 如果您忘记拨打超级电话,Android会在大多数情况下导致异常。 I would trust that you don't have to call super if you don't do it and it doesn't throw. 如果你不这样做,我会相信你不必打电话给超级而且它不会扔掉。

In the google groups discussions some best practices for the lifecycle methods evolved(They are not officially backed by data but used by many programmers): 在谷歌小组讨论中,生命周期方法的一些最佳实践得到了发展(它们不是由数据正式支持,而是由许多程序员使用):

  • If you are in a creating method like onCreate, onResume, etc. call super as the first statement. 如果你在onCreate,onResume等创建方法中,则调用super作为第一个语句。 Thus you can be sure that everything that has to be prepared from the superclass is prepared. 因此,您可以确保准备好从超类准备的所有内容。
  • If you are in a closing method like onSaveInstanceState, onPause call super last. 如果您使用的是onSaveInstanceState等关闭方法,则onPause会调用super last。 Now you could be sure that nothing gets removed or changed to a bad state before you got everything done. 现在,您可以确保在完成所有操作之前不会删除任何内容或将其更改为错误状态。

您不需要调用super.method(),只有在您需要它时才需要它。

When you override a method in a child class then it depends on the type of the instance on which you are calling that method. 当您重写子类中的方法时,它取决于您调用该方法的实例的类型。

for example: 例如:

   Class Animal{
        public eat(){
           //..
        }
   }

   Class Dog extends Animal{
        public eat(){
           //..
        }
  }

now if you say new Dog().eat() 
it executes Dog's eat method.

if you say new Animal().eat()
it executes Animal's eat method.

And you may have code like this 你可能有这样的代码

       Animal a = new Dog();
       a.eat();

which again executes the Dog's eat method as the actual instance is of the type Dog. 它再次执行Dog's eat方法,因为实际的实例是Dog类型。

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

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