I have a Java class called Game which has a non-static integer called score.
I would like to implement a static method which would increase the score of each Game object by 1, named increaseAllScore(). Is this possible? Could I simulate something like this or is there any way around this?
You could do it with an implementation like this:
int score;
static int scoremodifier;
public static void increaseAllScore() {
scoremodifier++;
}
public int getScore() {
return score + Game.scoremodifier;
}
The only way to do this is to provide a mechanism for the static method to access a reference to the Game object(s). One way to do this is to have each Game object register itself in a static data structure.
For instance, you might do this:
public class Game {
private static Set<WeakReference<Game>> registeredGames
= new HashSet<WeakReference<Game>>();
private int score;
public Game() {
// construct the game
registeredGames.add(new WeakReference(this));
}
. . .
public static incrementAllScores() {
for (WeakReference<Game> gameRef : registeredGames) {
Game game = gameRef.get();
if (game != null) {
game.score++;
}
}
}
}
I'm using a WeakReference<Game>
here so that the set doesn't prevent the game from being garbage-collected when there are no other references to it.
It's technically possible but it's generally a bad design * . Instead create a container for all your games (call it class Games
) that will hold references to all created Game
instances. Most likely Games
class will have a createGame()
method to fully control the lifecycle of all created games.
Once you have Games
class, it can have non-static increaseAllScores()
method that will basically iterate over all created Game
instances and increase score of all of them one-by-one.
* - make a static List<Game>
of all instances and modify that list inside Game
constructor.
This would be an outline of a solution to your problem:
static final List<Game> games = new ArrayList<>();
public class Game {
public Game() {
games.add(this);
}
}
public static void increaseAllScore() {
for (Game g : games) game.increaseScore();
}
This is possible, but needs some bookkeeping. In essence, you have to keep a static set of pointers to all existing games. In a game's constructor, you need to add it to this list, and you need to remove it again in the destructor.
A probably better way would be to have a static variable called scoreOffset
or similar. Then, you can calculate an game's score by taking the instance score and adding the static scoreOffset
.
如果您的increaseAllScore
方法具有对Game
实例的静态访问权限(您可以在参数中传入列表,或者具有静态存储的列表),则只能执行此操作。
It is not possible; first learn the basics of Object-Oriented Programming.
As a work-adound you could hold a reference to all the Games:
public class Game {
private static List<Game> allGames = new ArrayList<Game>();
public Game createNewGame() {
Game game = new Game();
allGames.add(game);
return game;
}
public static void increaseAllGames() {
for (Game game : games) {
game.increaseScore();
}
}
}
This is just an implementation example; for design I would not place them in the same class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.