Android Eclipse 插件生成的代码中 super() 调用的位置可靠吗?

发布于 2024-10-17 03:24:33 字数 608 浏览 4 评论 0原文

在许多Android方法中,尤其是构造函数和重写方法中,您应该甚至必须使用super()调用父类方法。当您使用 Eclipse Source >覆盖/实现方法... 从带有 TODO 标签的模板中获取代码,如下所示:

public MyCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub      
    super.onDraw(canvas);
} 

我不完全理解超类在每种情况下的作用,因此我总是在 的确切位置插入代码//TODO 标签。在示例中,我将在构造函数中的代码之前和 onDraw() 中的代码之后调用 super()。

我可以始终依赖生成的代码中的这些代码插入位置吗?何时调用 super() 是否有一个简单的规则/解释?

In many of Android methods, especially constructors and overridden methods, you should or even must call the parent class method using super(). When you use the Eclipse Source > Override/Implement Methods... you get code from a template with TODO tags like this:

public MyCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub      
    super.onDraw(canvas);
} 

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags. In the example, I would call super() before my code in the constructor and after my code in onDraw().

Can I always rely on these code insertions locations in the generated code? Is there a simple rule/explanation when to call super()?

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

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

发布评论

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

评论(3

另类 2024-10-24 03:24:33

这是一个好问题。不幸的是,对此没有简单的规则。您需要知道超类实现的作用。有时(如在 View.onDraw() 中),超类实现不执行任何操作;调用 super() 既无害又不必要。在其他情况下(例如Activity.onCreate()),超类实现执行必须在子类处理中的某个时刻执行的关键操作。有时,当您调用 super() 时发生的事情应该发生在子类中的任何处理之前,有时是在其他点。有时您想用自己的超类处理完全替换超类处理,在这种情况下您根本不调用 super() 。您可以完全自由地在子类逻辑中的任何点(甚至多个点)调用超类版本。

在构造函数中,对超类构造函数(如果存在)的调用必须是方法中的第一件事。如果没有,编译器会自动在超类中插入对无参数构造函数的调用。 (如果超类没有无参构造函数,或者子类无法访问它,则编译器会生成错误。)

如果文档没有提供足够的信息,那么您必须查看源代码。 Android 代码可用 此处(Gingerbread 版本)。 API 代码位于core 下。

编辑 该代码在 git.kernel.org 上不再可用。您还可以在以下两个地方浏览代码:

主要代码在存储库 Platform >框架>根据

This is a good question. Unfortunately, there is no simple rule for this. You need to know what the superclass implementation does. Sometimes (as in View.onDraw()), the superclass implementation does nothing; calling super() is both harmless and unnecessary. In other cases (such as Activity.onCreate()) the superclass implementation performs critical operations that must be executed at some point in the subclass's processing. Sometimes what happens when you call super() should come before any processing in the subclass, sometimes at other points. Sometimes you want to completely replace the superclass processing with your own, in which case you don't call super() at all. You have complete freedom to call the superclass version at any point (or even at multiple points) in your subclass's logic.

In constructors, the call to a superclass constructor (if present) must be the first thing in the method. If you don't have one, the compiler automatically inserts a call to the no-argument constructor in the superclass. (If the superclass does not have a no-argument constructor, or if it is not accessible to the subclass, the compiler generates an error.)

If the documentation doesn't provide enough information, then you have to look at the source code. The Android code is available here (Gingerbread release). The API code is under core.

EDIT The code is no longer available at git.kernel.org. Here are two other places where you can browse the code:

The main code is in the repository Platform > Frameworks > Base

风启觞 2024-10-24 03:24:33

<块引用>

我可以始终依赖生成的代码中的这些代码插入位置吗?

不,有时您不想调用 super.method。有时您想先调用它,有时在最后一个地方调用它,等等。这取决于情况。但是,我谈论的是方法,而不是构造函数。

<块引用>

何时调用 super() 是否有简单的规则/解释?

您始终需要将所有super作为前面的答案点。唯一不调用 super 的情况是当超类的构造函数没有参数时;在这种情况下,编译器将为您放置 super

<块引用>

我不太清楚超类在每种情况下的作用,因此我总是在 //TODO 标记的确切位置插入代码

如果您有疑问(我说的是超级方法),您可以随时查看源代码。 Google 代码搜索是一个很好的资源。然后你可以决定是将代码放在 super 方法之前还是之后;或者甚至,根本不放置 super 方法。

请记住,不放置 super 方法在编译时是有效的。但是,除非您调用 super 方法(例如,Activity 类的 onResume 方法),否则 Android 上的某些方法将无法工作。

另外,有时您会决定是否在运行时运行 super 方法。考虑这个经典的例子:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode() ){
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

如果用户按下后退键,您将不会调用 super 方法。如果用户没有这样做,则将工作委托给超级方法。

Can I always rely on these code insertions locations in the generated code?

No, sometimes you don't want to call the super.method. Sometimes you want to call it first, sometimes at the last place, etc. It depends. But, I'm talking about methods, no constructors.

Is there a simple rule/explanation when to call super()?

You will always have to all super as the previous answer points. The only case where you don't call super is when the constructor of the super class has no parameters; in that case the compiler will put the super for you.

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags

If you are in doubt (I'm talking about super methods), you can always take a look at the source code. Google Code Search is a good resource to do so. Then you can decide whether to put your code before or after the super method; or even, don't put the super method at all.

Keep in mind that not putting the super method is valid at compile time. But, some methods on android won't work unless you invoke the super method (for instance, the onResume method of the Activity class).

Also, sometimes you will decide whether to run the super method or not in runtime. Consider this classic example:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode() ){
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

If the user pressed the back key, you won't call the super method. If the user didn't, you delegate the work to the super method.

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