English 中文(简体)
• 如何强迫衍生阶级使用超级方法? (Like Cohen)
原标题:How to force derived class to call super method? (Like Android does)

我很想知道,在创建新的<条码>代用性类别时,然后超越<条码>onCreate(<>/条码>方法,我总是自动添加:super.onCreate()。 情况如何? 抽象或母阶级中是否有 j关键词迫使这一点?

我不知道不要说超级班子是非法的,但我在某些方法中记得,我只是因为没有这样做而放弃了例外。 这是否也融入了 j? 你们能否用一些关键词来做到这一点? 或者如何做到这一点?

最佳回答

这里是<条码>活性编号/代码>的源头——几乎全部评论(

/**
 * 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;
}

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.

问题回答

如果你想要<>>/em>子类以实施母类逻辑,一种共同的模式如下:

public abstract class SuperClass implements SomeInterface
{
    // This is the implementation of the interface method
    // Note it s final so it can t be overridden
    public final Object onCreate()
    {
        // Hence any logic right here always gets run
        // INSERT LOGIC

        return doOnCreate();

        // If you wanted you could instead create a reference to the
        // object returned from the subclass, and then do some
        // post-processing logic here
    }

    protected abstract Object doOnCreate();
}

public class Concrete extends SuperClass
{
    @Override
    protected Object doOnCreate()
    {
        // Here s where the concrete class gets to actually do
        // its onCreate() logic, but it can t stop the parent
        // class  bit from running first

        return "Hi";
    }
}

这实际上没有回答你的问题,即,什么促使Eclipse自动插入超级帽子,以实施;但我不认为这样做总是可以删除。

您实际上不能强制实施一种方法,即必须用 Java关键词或类似的话说上超级阶级。 我怀疑,你的例外情况只是来自上级班级中的一些法规,而上级班级检查是按你的做法无效的。 请注意,这明显不同于放弃一个例外(because):你没有打电话super.onCreate()。

如果你想绝对确保也采用超级阶级方法,那么你就必须trick: Don t 允许超品德超成文,但必须采用一种压倒一切的保护方法。

class Super
{
   public final void foo() {
      foo_stuff();
      impl_stuff();
   }

   protected void impl_stuff() {
      some_stuff_that_you_can_override();
   }
}

class Base extends Super
{
  protected void impl_stuff() { 
     my_own_idea_of_impl();
  }
}

这样,用户就必须叫“超级(foo)”或“基地”(foo)”,并且总是作为最后申报的基级版本。 具体执行工作的障碍是无法克服的。

To answer your actual question, the auto-creation of the call to super.onCreate() is a feature of the ADT plugin. In java, you cannot directly force a subclass to call the super implementation of a method, afaik (see the pattern described in other answers for work-around). However, keep in mind that in Android, you are not instantiating Activity objects (or Service objects) directly - you pass an Intent to the system and the system instantiates the object and calls onCreate() upon it (along with other lifecycle methods). So the system has a direct object reference to the Activity instance and is able to check (presumably) some Boolean that is set to true in the superclass implementation of onCreate(). Although I don t know exactly how it is implemented, it probably looks something like this:

class Activity
{
  onCreate()
  {
    superCalled = true;
    ...
  }
  ...
}

在“系统”级中,从中接收活动物体:

...
SomeActivitySubclass someActivitySubclassObject = new SomeActivitySubclass();
someActivitySubclassObject.onCreate();
if (!someActivityObject.isSuperCalled())
{
  Exception e = new Exception(...) //create an exception with appropriate details
  throw e;
}

我的猜测可能比这更复杂,但你会这样做。 Eclipse自动产生这一呼吁,因为ADT的花招告诉它是一种方便。 欢乐!

Java没有任何东西要求超级部队,在你想要的时候,就有很多例子。 你们能够用超级武力的唯一地点是建筑商。 所有建筑商都必须叫上超层建筑商。 如果不明确写字,就将插入一个论点(没有论据),如果不存在任何争论,那么你必须明确说话。

顺便说一句,提醒大家,如果你想要的话,你可以称之为超级执行。

你们可能会出现错误,因为你没有做多余的事情,因为你没有要求执行。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签