java - 返回该对象之前增加对象中的静态变量

标签 java android arrays

我有一些代码可以创建称为权重的对象。现在,这些权重有一些子类,称为 - WeightSmall、WeightMedium 和 WeightLarge - 每个子类都有自己的静态变量,称为 onScreen。当添加 WeightSmall、WeightMedium 或 WeightLarge 之一时,此变量应该递增,但是这些变量会在调用 create 方法时返回,而不是添加到数组中。我有一个 Weight 对象数组 - 有没有办法访问父类数组中元素的子类类型?

这是创建权重的代码:

public Weight createWeight() {
    decider = Math.random() * 1;
    // creates rocks randomly with the lowest chance for l, and the highest chance for m
    if (decider <= 0.33) {
        // small weight
        return new WeightSmall(BitmapFactory.decodeResource(getResources(), R.drawable.weight_s), new Random().nextInt(screenWidth), -10);
    } else if (decider <= 0.5 && decider > 0.33) {
        // large weight
        return new WeightLarge(BitmapFactory.decodeResource(getResources(), R.drawable.weight_l), new Random().nextInt(screenWidth), -10);
    } else {
        // medium weight
        return new WeightMedium(BitmapFactory.decodeResource(getResources(), R.drawable.weight_m), new Random().nextInt(screenWidth), -10);
    }
} 

对于WeightSmall 来说,需要做的是,它需要检查WeightSmalls onScreen 变量,看看它是否小于,比方说3。如果它返回重量。然而,我想不出一种方法来访问 WeightSmall 的 onScreen 变量,因为它需要多次创建,我尝试将它们实现到 ArrayList 中,但它会导致更新方法变得复杂。这是该类的其余代码(重要):

public void render(Canvas canvas) {
    if (canvas != null) {
        canvas.drawColor(Color.WHITE);
        player.draw(canvas);
        Weight[] weightArray = weights.toArray(new Weight[0]);
        for (Weight weight : weightArray) {
            weight.draw(canvas);
        }
    }
}

// updates the weight's position on the screen and checks collision with the player
public void update() {
    Weight[] weightArray = weights.toArray(new Weight[0]);
    for (Weight weight : weightArray) {
        weight.update();
        if (weight.getBounds().intersect(player.getBounds())) {
            player.setTouched(false);
            Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
            this.getContext().startActivity(gameOverIntent);
            ((Activity) getContext()).finish();
        }
    }
}

// count down timer spawning weights in every tick
public void timer() {
    if (start == true) {
        if (weightSpawnTimer != null) {
            weightSpawnTimer.cancel();
            weightSpawnTimer = null;
        }
        weightSpawnTimer = new CountDownTimer(30000, 800) {

            public void onTick(long millisUntilFinished) {
                weights.add(createWeight());
            }

            public void onFinish() {
                weightSpawnTimer.start();
            }
        }.start();
    }
}

为清楚起见进行编辑:我需要做的是在 onTick 方法中,检查权重的 onScreen 变量的子类是否 <= 3,如果是,则创建一个新权重,如果不是,则不执行任何操作。一旦权重离开屏幕,就减少该变量,以便可以创建该子类的新权重。

最佳答案

怎么样

class WeightSmall {
    public WeightSmall(...) {
        // increment static
    }
}

让每个类负责在创建实例时增加自己的编号。

从实例修改静态变量通常被认为是不好的做法。少数合法用例通常是某种实例计数。至少当你不倒计时时。

倒计时是麻烦开始的地方,因为对象确实有一个定义的开始,但不能保证它们的结束。您可以在 finalize 中倒计时 - 即当垃圾收集器找到您的实例时 - 但这不能保证很快或根本不会发生。使用它来找出屏幕上有多少实例将与实际数字相关,但可能是完全错误的。

因此,当您想知道屏幕上显示了多少个对象时,一旦负责显示对象的位置让我们离开一个,您必须主动倒数该数字。 由于这已经是想要知道屏幕上有多少对象的类的责任,因此它也应该跟踪它自己的局部变量中的数字。

通过使用static 属性,您可以将自己限制为只有 1 个屏幕。如果你让对方数数,你就不会限制自己。

关于java - 返回该对象之前增加对象中的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20619545/

相关文章:

java - 为什么我的 clickListener 仅在我第二次调用它时才使我的应用程序崩溃

arrays - 优化中的 Julia 抽象类型?

java - 从 ListView 单击获取自定义对象

android - 如何将数据从 android 服务(从 onStart 或 OnCreate)传递到 android Activity

JavaFx:使进度条不确定

java - Azure 脱机同步来自 1 :n relationship in Android mobile app 的数据

c++ - 已排序数组的一部分被反转

php5 : does the 'copy' of an array for a foreach incur overhead?

java - osmdroid - 更高的缩放级别?

java - Java MAVEN 示例中的 XpathException 表达式使用未绑定(bind)的命名空间前缀