在Android的Eclipse插件超强的位置()调用生成的code可靠吗?插件、可靠、位置、Android

2023-09-13 23:48:01 作者:偏爱御姐怪我喽i

在很多Android的方法,尤其是构造函数和覆盖方法,你应该甚至必须使用调用父类的方法超()。当您使用Eclipse 源代码>覆盖/实现方法... 你得到code从具有TODO标签像这样的模板:

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);
} 

我不明白exacly什么超确实在每种情况下,所以我总是在的 // TODO 标记的确切位置插入我的code。在这个例子中,我会在我的code在构造函数中和后,我的code在OnDraw中调用super()()。

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().

我可以永远依靠的产生code这些code插入的位置?有一个简单的规则/解释何时调用超()?

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

推荐答案

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

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.

在构造函数,调用父类的构造函数(如present)必须在方法的第一件事。如果你没有一个时,编译器会自动插入一个调用超类的无参数的构造函数。 (如果超类没有一个无参数的构造函数,或者如果它不能访问的子类,编译器会生成一个错误。)

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.)

如果文件没有提供足够的信息,那么你必须看看源$ C ​​$ C。而Android code可 here (姜饼版本)。该API code是在内核

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.

修改的code不再在git.kernel.org。这里有另外​​两个地方,你可以浏览code:

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

github.com omapzoom.org github.com omapzoom.org

主要code是仓库平台中>框架>基本

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