如何强制派生类中调用超级方法? (Android这样做) [英] How to force derived class to call super method? (Like Android does)

查看:205
本文介绍了如何强制派生类中调用超级方法? (Android这样做)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,当创建新的活动类,然后重写的onCreate()方法,在Eclipse我总是自动添加: super.onCreate()。这是如何发生的呢?是否有强制这个抽象或父类的Java关键字?

I was wondering, when creating new Activity classes and then overriding the onCreate() method, in eclipse I always get auto added: super.onCreate(). How does this happen? Is there a java keyword in the abstract or parent class that forces this?

我不知道这是违法的不调用超类的,但我记得在我抛出不这样做一个例外,一些方法。难道这还内置到Java?你可以使用一些关键字来做到这一点?或者是如何做的?

I don't know if it is illegal not to call the super class, but I remember in some methods that I got a exception thrown for not doing this. Is this also built-in into java? Can you use some keyword to do that? Or how is it done?

推荐答案

以下是活动#的onCreate()源 - 这是几乎所有的评论(<一href="http://www.google.com/$c$csearch/p?hl=en#uX1GffpyOZk/core/java/android/app/Activity.java&q=activity&exact_package=git://android.git.kernel.org/platform/frameworks/base.git&sa=N&cd=1&ct=rc">original - 看行〜800 ):

Here's the source of Activity#onCreate() - it is almost all comments (original - see line ~800):

/**
 * Called when the activity is starting.  This is where most initialization
 * should go: calling {@link #setContentView(int)} to inflate the
 * activity's UI, using {@link #findViewById} to programmatically interact
 * with widgets in the UI, calling
 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
 * cursors for data being displayed, etc.
 *
 * <p>You can call {@link #finish} from within this function, in
 * which case onDestroy() will be immediately called without any of the rest
 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
 * {@link #onPause}, etc) executing.
 *
 * <p><em>Derived classes must call through to the super class's
 * implementation of this method.  If they do not, an exception will be
 * thrown.</em></p>
 *
 * @param savedInstanceState If the activity is being re-initialized after
 *     previously being shut down then this Bundle contains the data it most
 *     recently supplied in {@link #onSaveInstanceState}.  <b><i>Note: Otherwise it is null.</i></b>
 *
 * @see #onStart
 * @see #onSaveInstanceState
 * @see #onRestoreInstanceState
 * @see #onPostCreate
 */
protected void onCreate(Bundle savedInstanceState) {
    mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
            com.android.internal.R.styleable.Window_windowNoDisplay, false);
    mCalled = true;
}

所以,我的猜测是,该ADT Eclipse插件是何等的自动补充说调用 super.onCreate()为您服务。这是一个总的猜测,虽然。

so, my guess would be that the ADT Eclipse plugin is what's auto-adding that call to super.onCreate() for you. It's a total guess, though.

这篇关于如何强制派生类中调用超级方法? (Android这样做)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆