Java onCreate 带有参数,如 initWithParameter c++
可能它太简单了,但我找不到正确的方法。
在 C++ 中,我可以编写 initWithParameter: xxx 来实例化一个类,然后在 init 中设置一些在 init 时给定值的实例变量。
在Java中我不知道该怎么做。目前我执行以下操作:
public class SpecialScreen extends BASEScreen{
private static final int ACTIVITY_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //create the instance
defineScreenType (ACTIVITY_1); //second call to set the instance variable
presentOnScreen();
}
在 BASEScreen 中时:
public class BASEScreen extends Activity {
private Integer activityCode; // which activity should I do?
@Override
public void onCreate(Bundle savedInstanceState) { // the creation
super.onCreate(savedInstanceState);
}
// the setting of the instance variable
public void defineScreenType(int screenID) {
activityCode = screenID;
}
这不是最好的方法。如何更好地做到这一点?
感谢
ADDED 在 BASEScreen 中显示 SpecialScreen 的调用:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent i;
switch (item.getItemId()) {
case OTHER_PAGE_ID:
//
if (activityCode == ACTIVITY_1) {
i = new Intent(this, SpecialScreen2.class);
i.putExtra("Task", ACTIVITY_2);
startActivityForResult(i, ACTIVITY_2);
finish();
} else {
i = new Intent(this, SpecialScreen1.class);
i.putExtra("Task", ACTIVITY_1);
startActivityForResult(i, ACTIVITY_1);
finish();
}
return true;
ps 我知道不再需要放置 Extra 了。这是我在拥有两个 SpecialScreen 子类之前所做的事情,并且总是使用此参数调用 BASEScreen。
may be its too simple but I couldnt find the right way.
In C++ I can write initWithParameter: xxx
to instantiate a class and then in the init set some instance variables given the value at init time.
In Java I don't know how to do that. Currently I do the following:
public class SpecialScreen extends BASEScreen{
private static final int ACTIVITY_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //create the instance
defineScreenType (ACTIVITY_1); //second call to set the instance variable
presentOnScreen();
}
While in BASEScreen:
public class BASEScreen extends Activity {
private Integer activityCode; // which activity should I do?
@Override
public void onCreate(Bundle savedInstanceState) { // the creation
super.onCreate(savedInstanceState);
}
// the setting of the instance variable
public void defineScreenType(int screenID) {
activityCode = screenID;
}
This can't be the best way of doing it. How to do this better?
Thanks
ADDED to show the calling of the SpecialScreen within BASEScreen:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent i;
switch (item.getItemId()) {
case OTHER_PAGE_ID:
//
if (activityCode == ACTIVITY_1) {
i = new Intent(this, SpecialScreen2.class);
i.putExtra("Task", ACTIVITY_2);
startActivityForResult(i, ACTIVITY_2);
finish();
} else {
i = new Intent(this, SpecialScreen1.class);
i.putExtra("Task", ACTIVITY_1);
startActivityForResult(i, ACTIVITY_1);
finish();
}
return true;
ps I know that putting the Extra is not required anymore. This was the way I did it before I had the two SpecialScreen subclasses and always called the BASEScreen with this parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确,没有像 c++ 那样的“默认”语法。你必须在构造函数中完成它。请注意,您不需要使用 setter 方法,您可以将
activityCode
设为受保护而不是私有,然后执行以下操作:另一个选项是使用构建器模式来构造对象,使用集合请求构建对象时您覆盖(需要时)的构建器内的默认值。
编辑回应以下评论:
对于一些混乱,我深表歉意,因为我称其为“构造函数”,但实际上它不是。
如果在
BASEScreen
中将访问权限从private
更改为protected
,那么您可以在
SpecialScreen
子类中访问它:Correct, there is no "default" syntax like in c++. You have to do it in the constructor. Mind you, you don't need to use the setter method, you could make
activityCode
protected rather than private, and just do:The other option is using the Builder Pattern to construct your objects, using a set of defaults inside the builder that you override (when needed) when requesting the object be built.
Edit in response to comments below:
I apologize for some confusion as I was calling it a "constructor" when it's not.
If in
BASEScreen
you change the access toprotected
fromprivate
You can then access that in the
SpecialScreen
subclass:如果我正确理解你的问题。 Java 中的任何类都可以有一个或多个构造函数。每个参数可以没有参数,也可以有一组传入的参数(尽管它们必须各自具有唯一的集合/顺序,以便编译器可以知道您打算使用哪一个)。
当您执行 new 时,每个函数都会被调用,例如:
BASEScreen porcupine = new SpecialScreen(); // 无参数构造函数。
或
BASEScreen 豪猪 = new SpecialScreen(5); // 接受参数的构造函数。
If I'm understanding your question properly. Any class in Java can have one or more constructors. Each one can have either no parameters or some set of parameters you pass in (although they have to each have a unique set/order so the compiler can tell which one you intend to use).
Each is invoked when you perform a new, for example:
BASEScreen porcupine = new SpecialScreen(); // No parameter constructor.
or
BASEScreen porcupine = new SpecialScreen(5); // Constructor that takes the parameter.