Java onCreate 带有参数,如 initWithParameter c++

发布于 2024-11-03 11:05:18 字数 1681 浏览 0 评论 0原文

可能它太简单了,但我找不到正确的方法。

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-11-10 11:05:18

正确,没有像 c++ 那样的“默认”语法。你必须在构造函数中完成它。请注意,您不需要使用 setter 方法,您可以将 activityCode 设为受保护而不是私有,然后执行以下操作:

activityCode = ACTIVITY_1;

另一个选项是使用构建器模式来构造对象,使用集合请求构建对象时您覆盖(需要时)的构建器内的默认值。

编辑回应以下评论:

对于一些混乱,我深表歉意,因为我称其为“构造函数”,但实际上它不是。

如果在BASEScreen中将访问权限从private更改为protected

public class BASEScreen extends Activity {
    protected Integer activityCode;

那么您可以在SpecialScreen子类中访问它:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activityCode = 1; // Or ACTIVITY_1 if you'd like
    presentOnScreen();
}

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:

activityCode = ACTIVITY_1;

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 to protected from private

public class BASEScreen extends Activity {
    protected Integer activityCode;

You can then access that in the SpecialScreen subclass:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activityCode = 1; // Or ACTIVITY_1 if you'd like
    presentOnScreen();
}
つ可否回来 2024-11-10 11:05:18

如果我正确理解你的问题。 Java 中的任何类都可以有一个或多个构造函数。每个参数可以没有参数,也可以有一组传入的参数(尽管它们必须各自具有唯一的集合/顺序,以便编译器可以知道您打算使用哪一个)。

public class SpecialScreen extends BASEScreen {
    private static final int ACTIVITY_1 = 1;

    // There is a default constructor with no parameters provided for you
    // by default if you don't define any constructors.
    public SpecialScreen() {
       // I'm overriding the default constructor and this one will do 
       // something else.
       super(ACTIVITY_1);
    }

    // But you can also have ones like this.
    public SpecialScreen(int activity) {
       super(activity);
    }
}

当您执行 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).

public class SpecialScreen extends BASEScreen {
    private static final int ACTIVITY_1 = 1;

    // There is a default constructor with no parameters provided for you
    // by default if you don't define any constructors.
    public SpecialScreen() {
       // I'm overriding the default constructor and this one will do 
       // something else.
       super(ACTIVITY_1);
    }

    // But you can also have ones like this.
    public SpecialScreen(int activity) {
       super(activity);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文