Java 如何继承静态字段并对其进行更改';s值多少?
我正在开发程序/游戏,其中我有带参数的静态实用程序类Java 如何继承静态字段并对其进行更改';s值多少?,java,inheritance,static,Java,Inheritance,Static,我正在开发程序/游戏,其中我有带参数的静态实用程序类 class ParamsGeneral { public static final int H_FACTOR = 100; public static int MAX_SCORE = 1000; ... } 然后我需要在某些特定情况下覆盖这些值,例如在地图上玩有限的分数。所以我做了以下几点: class ParamsLimited extends ParamsGeneral { public static int M
class ParamsGeneral {
public static final int H_FACTOR = 100;
public static int MAX_SCORE = 1000;
...
}
然后我需要在某些特定情况下覆盖这些值,例如在地图上玩有限的分数。所以我做了以下几点:
class ParamsLimited extends ParamsGeneral {
public static int MAX_SCORE = 500;
// other params stay same
}
class Player {
ParamsGeneral par;
public Player() {
if(onLimitedMap()){
par = new ParamLimited();
}
}
public boolean isWinner() {
if(this.score == par.MAX_SCORE) {
return true;
}
return false;
}
}
预期用途如下:
class ParamsLimited extends ParamsGeneral {
public static int MAX_SCORE = 500;
// other params stay same
}
class Player {
ParamsGeneral par;
public Player() {
if(onLimitedMap()){
par = new ParamLimited();
}
}
public boolean isWinner() {
if(this.score == par.MAX_SCORE) {
return true;
}
return false;
}
}
我并没有实际测试这段代码,因为IDE抱怨通过实例调用静态字段,还抱怨字段隐藏。我清楚地看到这段代码很糟糕,所以有没有办法实现这一点,或者我必须分别编写每个param类
PS:我知道我应该使默认类抽象并使用getter,我只是想知道是否有一种方法可以使值静态可访问。您不能覆盖静态成员-在Java中,方法和字段都不能被覆盖。但是,在这种情况下,您似乎不需要执行任何操作:因为在
par
变量中有一个ParamsGeneral
的实例,所以非静态方法可以执行常规重写所需的操作
class ParamsGeneral {
public int getMaxScore() {
return 1000;
}
}
class ParamsLimited extends ParamsGeneral {
@Override public int getMaxScore() {
return 500;
}
}
...
public boolean isWinner() {
// You do not need an "if" statement, because
// the == operator already gives you a boolean:
return this.score == par.getMaxScore();
}
拥有
MAX_分数
与公共静态获取者保持私有静态;然后你可以调用ParamsGeneral.getMaxScore
和ParamsLimited.getMaxScore
分别得到1000和500我不会对普通游戏和有限游戏使用子类化。我将使用枚举,如:
public enum Scores {
GENERAL (1000),
LIMITED (500),
UNLIMITED (Integer.MAX_INT);
private int score;
private Scores(int score) { this.score = score; }
public int getScore() { return score; }
}
然后,在构建游戏时,您可以执行以下操作:
Params generalParams = new Params(Scores.GENERAL);
Params limitedParams = new Params(Scores.LIMITED);
等等
这样做可以让你改变游戏的性质,同时保持你的价值观集中。想象一下,对于您想到的每种类型的参数,您都必须创建一个新类。它可能会变得非常复杂,你可能有数百个类 最简单的解决方案是:
class ParamsGeneral {
public static final int H_FACTOR = 100;
public static final int MAX_SCORE = 1000;
public static final int MAX_SCORE_LIMITED = 500;
...
}
class Player {
int maxScore;
public Player() {
if(onLimitedMap()){
maxScore = ParamsGeneral.MAX_SCORE_LIMITED;
}
else {
maxScore = ParamsGeneral.MAX_SCORE;
}
}
public boolean isWinner() {
if(this.score == this.maxScore) {
return true;
}
return false;
}
}
不需要ParamsGeneral的实例,它只是游戏中静态定义的集合。我想说的是,这里可能不需要静态字段/方法,其实就这么简单。为什么不创建它的一个实例并获得完整的继承呢?这可能是C语言的一个坏习惯,我在C语言中使用simple#define来设置全局参数。在我看来,这似乎有点过头了,只使用数值参数创建实例。我希望避免这种方法,因为它不会解决问题。问题是有很多参数,但只有少数几个是专门的。让我们说30个中的2个。而且所有参数都需要以相同的方式可访问-因此,超级类ParamsGeneral.@jnovacho在不了解更多架构的情况下,很难向您提供最佳建议。只是不要硬编码子类,因为在特殊情况下,参数越多,子类将变得非常不可维护。如果一个参数有5个值,另一个参数有3个值,那就是15个类。如果另一个有4个值,那就是60个类。。。