简体   繁体   English

具有2种不同类型的对象数据

[英]Object data with 2 different types

I have the following: 我有以下内容:

public class Notifier{
    CustomPlayer mCustomPlayer;
    CurrentPlayer mCurrentPlayer;
}

public class MainActivity extends Activity{

    public void onCreate(){
        Notifier ntf = new Notifier();
        if( index == 0){
            ntf.mCustomPlayer = new CustomPlayer(this);
        }
        else{
            ntf.mCustomPlayer = new CurrentPlayer(this); // having problem here
        }
    }
}

In Notifier class, I just want to have one object mCustomPlayer to switch between CustomPlayer and CurrentPlayer in MainActivity class. 在Notifier类中,我只想让一个对象mCustomPlayer在MainActivity类中的CustomPlayer和CurrentPlayer之间切换。

I tried adding the following in Notifier class, 我尝试在Notifier类中添加以下内容,

public class Notifier{
    CustomPlayer mCustomPlayer;
    CurrentPlayer mCurrentPlayer;

    public Object getType(int index) {
        if (index == 1) {
            return CurrentPlayer.class;
        }
        else {
            return CustomPlayer.class;
        }
    }
}

With that I am having a problem when trying to initialize mCustomPlayer in MainActivity class. 这样,尝试在MainActivity类中初始化mCustomPlayer时遇到问题。

ntf.mCustomPlayer = new (ntf.mCustomPlayer)getType(0); // compile error

Is there a way to realize this? 有没有办法实现这一目标?
It has been a day since I am trying to configure out the correct implementation. 自从我尝试配置正确的实现以来已经过了一天。
Should I use Interface in this case? 在这种情况下应该使用接口吗?

To use the new keyword you must supply a class (ie new MyClass() ). 要使用new关键字,您必须提供一个类(即new MyClass() )。

You could use reflection for this... but wouldn't it be much simpler to just have a common superclass (or interface) for CustomPlayer and CurrentPlayer ? 可以为此使用反射...但是为CustomPlayerCurrentPlayer拥有一个通用的超类(或接口)会不会更简单吗?

For example, suppose both CustomPlayer and CurrentPlayer have the playOne() and playTwo() methods. 例如,假设CustomPlayerCurrentPlayer都具有playOne()playTwo()方法。 You could then define: 然后,您可以定义:

public interface Player {
    void playOne();
    void playTwo();
}

public class CurrentPlayer implements Player {
    @Override
    public void playOne() { 
        // code
    }

    @Override
    public void playTwo() {
        // code
    }
}

private class CustomPlayer implements Player {
    @Override
    public void playOne() {
        // code
    }

    @Override
    public void playTwo() {
        // code
    }
}

public class Notifier {
    Player mPlayer;
 }

And then assign mPlayer with new CurrentPlayer() or new CustomPlayer() You can then call any methods on the interface. 然后为mPlayer分配new CurrentPlayer()或新的CustomPlayer()然后,您可以在接口上调用任何方法。

You could using Reflection: 您可以使用反射:

public class Notifier{

    public CommonInterface getInstance(int index, Class<Activity> activity){
      Class<?> claz = getType(0);
      Constructor<?> cons = claz.getConstructor(activity);
      return (CommonInterface) cons.newInstance(this);
      //or you could just type cast it manually if you do not wish to use CommonInterface
}

But having a common interface is the right way to go. 但是拥有通用接口是正确的方法。 You dont have to worry about reflection then. 您不必担心反射。

Since you have same function for both the classes so use interface and access the object - 由于两个类的功能相同,因此请使用interface并访问对象-

public class MainActivity extends Activity{
    interface CurrentPlayer  { void game(); }
    interface CustomPlayer   { void game(); }

    interface Player extends CurrentPlayer, CustomPlayer { }

     public void onCreate(){
        Player swan = new Player() {
            @Override 
            public void game() {
                System.out.println("Swan Player");  //Swan Player
            }
        };
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM