开发者

Java onCreate with parameter like in initWithParameter c++

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 th开发者_运维问答at. 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.


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜