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

问题描述 投票:0回答:2

我正在尝试从多个类访问同一个类的实例。我有一个用于我的炮塔的课程,我正在尝试使用我的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);
        }
    }
}
java android instance
2个回答
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);

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;
        }
    }
    }
© www.soinside.com 2019 - 2024. All rights reserved.