我需要调用“super.XXX”吗?每当我重写一个方法时?

发布于 2024-10-19 00:05:38 字数 281 浏览 1 评论 0原文

比如说,如果我不在超类 Activity 的重写方法中调用 super.onPause,那么当 onPause() 为叫。但是,当我在类的方法(onCreateonStartCommand、...)中没有 super.XXX 调用时,不会显示错误派生自服务

那么什么情况下我应该在重写方法中调用 super.XXX 呢?

Say, if I don't call super.onPause in a override method from superclass Activity, I would get an error when onPause() is called. But errors don't show up when I have no super.XXX calls in methods (onCreate, onStartCommand, ...) of a class derived from Service.

So on what conditions should I call super.XXX in a override method?

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

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

发布评论

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

评论(5

文档告诉您您需要调用 onPause 如果您派生 Activity 类:

派生类必须调用超类对此方法的实现。如果不这样做,就会抛出异常。

Service 文档在 onStartCommand 文档

一般来说(不是特别在 Android 中),当您派生时,您应该调用超类的方法,除非您知道不应该这样做。这是一个需要根据具体情况做出的决定,但默认情况下(我想说)是你这样做。

The documentation tells you that you need to call onPause if you derive the Activity class:

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

The Service docs don't require that in the onStartCommand documentation.

In general (not specifically in Android stuff), when you derive, you should call through to the superclass's methods unless you know you shouldn't. It's a decision that needs to be case-by-case, but the default (I'd say) would be that you do it.

沦落红尘 2024-10-26 00:05:38

那么在什么情况下我应该在重写方法中调用 super.XXX ?

对此不可能给出有用的答案。它完全取决于重写方法和被重写方法的预期目的和行为。

如果您不知道,则需要执行以下一项或多项操作:

  • 阅读该方法的 javadoc,
  • 查看实现该方法的类的示例,和/或
  • 查看您要重写的方法的代码。

在这种特殊情况下, javadoc 给出了明确的答案。

So on what conditions should I call super.XXX in a override method?

It is not possible to give a useful answer to this. It entirely depends on the intended purpose and behavior of the override method and the method being overridden.

If you don't know, you need to do one or more of the following:

  • read the javadocs for the method,
  • look at examples of classes that implement the method, and / or
  • look at the code of the method you are overriding.

In this particular case, the javadoc gives a clear answer.

失与倦" 2024-10-26 00:05:38

如果手册如 @tj-crowder 所说,您必须致电他们。

它说

派生类必须调用
超类的实现
这个方法。如果他们不这样做,
将抛出异常。

如果不是,那么原因是,如果您正在调用函数,您是否希望启动“正常”行为?

例如,如果您重写 onKeyDown() 您可以在按下某个键(例如“后退”)后执行某些操作。然后你可以选择:

  • 如果你想要执行“back”的正常操作,请调用 super
  • 如果你不想运行正常操作,请不要调用它。

You must call them if the manual says so as @t-j-crowder said.

it says

Derived classes must call through to
the super class's implementation of
this method. If they do not, an
exception will be thrown.

If not, then the reasoning is that if you are calling a function, do you want the 'normal' behaviour to be started?

For instance, if you override the onKeyDown() You could do something after a certain key is pressed (e.g. 'back'). Then you have the choice:

  • If you want the normal action of 'back' to be executed, call the super
  • If you DON'T want to run the normal action, don't call it.
一身仙ぐ女味 2024-10-26 00:05:38

客户端代码只能知道子类Activity(称之为MyActivity),客户端代码无法知道Activity。但是,MyActivity 可以知道它的基类 Activity。

如果重写的方法 onPause() 与其基类相同,则无需显式重写它。

Client code can only know subclass Activity (call it MyActivity), Client code cannot know Activity. However, MyActivity can know its base class Activity.

If the overridden method onPause() is same as its base class, you don’t need to override it explicitly.

痴意少年 2024-10-26 00:05:38

基于Android文档:与Activity生命周期回调方法不同,服务生命周期回调方法不需要调用这些回调方法的超类实现。

Based on Android document: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods for service lifecycle callback methods.

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