如何从多个类访问同一个实例

时间:2018-01-22 12:54:43

标签: java android instance

我正在尝试从多个类访问同一个类的实例。 我有一个用于我的炮塔的课程,我正在尝试使用我的View类绘制炮塔,同时使用我的逻辑类更新其位置。我不明白如何检查类是否已经有一个正在运行的实例,以及它是如何访问正在运行的实例。我无法在网上找到任何我理解的东西,而且它正好在我的脑海中,所有的帮助都表示赞赏!

炮塔

public class Turrets {

    int health, x, y, speed;
    Bitmap sprite;

    public Turrets (Context context){ }

    public void isMoving(){
        x += speed;
    }

    public int getHealth() {return health;}

    public int getX() {return x;}

    public int getY() {return y;}

    public Bitmap getSprite() {return sprite;}
}

SimpleTurret

public class SimpleTurret extends Turrets {

        public SimpleTurret(Context context){
            super(context);

            sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.test_sprite);

            health = 50;

            x = 300;
            y = 100;
            speed = 1;
        }
}

逻辑

public class Logic implements Runnable{

    Boolean isRunning;
    private Thread logicThread;

    SimpleTurret simpleTurret;

    public Logic(Context context, boolean running){

        simpleTurret = new SimpleTurret(context);

        logicThread = new Thread(this);
        logicThread.start();

        isRunning = running;
    }

    @Override
    public void run() {
        while (isRunning){
            gameView.rapidFireTurret.isMoving();
        }

    }
}

GameView

public class GameView extends SurfaceView implements Runnable {

    private boolean running = true;

    SurfaceHolder surfaceHolder = getHolder();

    SimpleTurret simpleTurret;
    RapidFireTurret rapidFireTurret;

    public GameView (Context context){
        super(context);

        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (running){
            DrawCanvas();
        }
    }

    public void DrawCanvas(){
        Canvas canvas = surfaceHolder.lockCanvas();
        if (surfaceHolder.getSurface().isValid()){
            canvas.drawColor(Color.RED);
            canvas.drawBitmap(simpleTurret.getSprite(), simpleTurret.getX(), simpleTurret.getY(), null);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

像类一样使用Singleton。

// Java program implementing Singleton class
// with getInstance() method
class Singleton {
    // static variable single_instance of type Singleton
    private static Singleton single_instance_first = null;
    private static Singleton single_instance_second = null;
    private static Singleton single_instance_third = null;

    // variable of type String
    public String s;

    // private constructor restricted to this class itself
    private Singleton() {
        s = "Hello I am a string part of Singleton class";
    }

    // static method to create instance of Singleton class
    public static Singleton getInstance(int index) {
        switch (index) {
            case 0: {
                if (single_instance_first == null)
                    single_instance_first = new Singleton();
                return single_instance_first;
            }
            case 1: {
                if (single_instance_second == null)
                    single_instance_second = new Singleton();
                return single_instance_second;
            }
            case 2: {
                if (single_instance_third == null)
                    single_instance_third = new Singleton();
                return single_instance_third;
            }
            default: {
                if (single_instance_first == null)
                    single_instance_first = new Singleton();
                return single_instance_first;
            }
        }
    }
}

并使用

Singleton firstSingleton = Singleton.getInstance(0);
Singleton secondSingleton = Singleton.getInstance(1);
Singleton thirdSingleton = Singleton.getInstance(2);

答案 1 :(得分:0)

将您的班级更改为此

    public class Turrets {

    int health, x, y, speed;
    Bitmap sprite;
    static Singleton mInstance;
    Context mContext;

    public Turrets (Context context){
        this.mContext=context;
    }

    public void isMoving(){
        x += speed;
    } 

    public int getHealth() {return health;}

    public int getX() {return x;}

    public int getY() {return y;}

    public Bitmap getSprite() {return sprite;}

    public static Turrets getInstance()
    {
        if (mInstance == null) {
            mInstance = new Turrets(mContext);
        }else {
        return mInstance;
        }
    }
    }