我很想知道,在创建新的<条码>代用性条码>类别时,然后超越<条码>onCreate(<>/条码>方法,我总是自动添加:super.onCreate(
)。 情况如何? 抽象或母阶级中是否有 j关键词迫使这一点?
我不知道不要说超级班子是非法的,但我在某些方法中记得,我只是因为没有这样做而放弃了例外。 这是否也融入了 j? 你们能否用一些关键词来做到这一点? 或者如何做到这一点?
我很想知道,在创建新的<条码>代用性条码>类别时,然后超越<条码>onCreate(<>/条码>方法,我总是自动添加:super.onCreate(
)。 情况如何? 抽象或母阶级中是否有 j关键词迫使这一点?
我不知道不要说超级班子是非法的,但我在某些方法中记得,我只是因为没有这样做而放弃了例外。 这是否也融入了 j? 你们能否用一些关键词来做到这一点? 或者如何做到这一点?
在支助说明图书馆中增加:
dependencies {
compile com.android.support:support-annotations:22.2.0
}
http://tools.android.com/tech-docs/support-annotations”rel=“noreferer”>http://tools.android.com/tech-docs/support-annotations
@Call Super
如果你想要
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没有任何东西要求超级部队,在你想要的时候,就有很多例子。 你们能够用超级武力的唯一地点是建筑商。 所有建筑商都必须叫上超层建筑商。 如果不明确写字,就将插入一个论点(没有论据),如果不存在任何争论,那么你必须明确说话。
顺便说一句,提醒大家,如果你想要的话,你可以称之为超级执行。
你们可能会出现错误,因为你没有做多余的事情,因为你没有要求执行。
顺便说一句只会帮助你做事,避免例外情况。
http://developer.android.com/vis/android/app/Actative.html#onCreate (android.os.Bundle” rel=“nofollow” http://developer.android.com/vis/android/app/Actative.html#onCreate(android.os.Bundle)
选修班必须通过超级班级采用这种方法。 如果它们不这样做,就会出现一个例外。
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...