Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重置变量和整个类_Java_Class_Variables - Fatal编程技术网

Java 重置变量和整个类

Java 重置变量和整个类,java,class,variables,Java,Class,Variables,我正在制作一个java black jack游戏,我需要处理每一轮新游戏的类在每次调用它(即每一轮新游戏)时都被重置。该类具有以下变量: public static LinkedList<String> playerHand = new LinkedList<>(), dealerHand = new LinkedList<>(); // creates hands for player and dealer public static int playerV

我正在制作一个java black jack游戏,我需要处理每一轮新游戏的类在每次调用它(即每一轮新游戏)时都被重置。该类具有以下变量:

public static LinkedList<String> playerHand = new LinkedList<>(), dealerHand = new LinkedList<>(); // creates hands for player and dealer
public static int playerValue, dealerValue; // value of the hand
“gameplay”是java包,“NewRound”是类,“newHand”是启动游戏的空白,“s”是赌注

我的问题是,类是否会重置,如手动值和LinkedList等。。。如果我一直这样叫它?或者我必须在NewRound类中添加一个MAIN,并这样调用它:

new gameplay.NewRound();

回答您的问题:没有内置的方法“重置”类的现有实例,除非您实现了一个方法,该方法可以执行此操作。大多数情况下,您只是创建一个新实例,而垃圾收集器关心的是从未使用的对象中释放内存。在某些情况下,您更愿意实现“重置”行为:对象创建成本太高,或者某些业务逻辑只需要一定数量的现有实例

基本上,使代码优雅易读的最佳方法是使其复制域对象的行为

我的建议是:一个
Game
类的实例,它将跟踪整个游戏。另外,这个
游戏将有一个方法来创建
新回合
,它将返回一个完全干净的
新回合

`
public class Game{
    private List<Round> roundList = new LinkedList<Round>();
    public Round startNewRound(int bet){
        Round newRound = new Round(bet);
        this.roundList.add(newRound);
        return newRound;
    }
}

public class Round{
    public List<String> dealerHand;
    public List<String> playerHand;
    public int bet;
    public Round(int bet){
        this.dealerHand = new LinkedList<String>();
        this.playerHand = new LinkedList<String>();
        this.bet = bet
    }
    public String compareHands(){
        //Some comparison between hands, return "dealer" or "player"
    }
}

public static void main(String[] args){
    Game game = new Game();
    Round roundOne = game.startNewRound(1);
    roundOne.dealerHand.add("Ace Of Spades");
    roundOne.playerHand.add("Some other card");
    System.out.println(roundOne.compareHands+" wins!");
    Round roundTwo = game.startNewRound(4);
    roundTwo.dealerHand.add("Another Ace Of Spades");
    roundTwo.playerHand.add("Some other stupid card");
    System.out.println(roundTwo.compareHands+" wins!");
}
`
公开课游戏{
private List roundList=new LinkedList();
公开回合开始新回合(整数下注){
新回合=新回合(下注);
this.roundList.add(newRound);
返回新一轮;
}
}
公开课轮{
公开上市交易商;
公开名单玩家;
公共赌注;
公开回合(整数赌注){
this.dealerHand=新建LinkedList();
this.playerHand=newlinkedlist();
this.bet=打赌
}
公共字符串比较句柄(){
//手牌之间的一些比较,返回“庄家”或“玩家”
}
}
公共静态void main(字符串[]args){
游戏=新游戏();
第一轮=游戏开始第二轮(1);
第一轮。发牌人。加上(“黑桃王牌”);
roundOne.playerHand.add(“其他卡片”);
System.out.println(roundOne.compareHands+“wins!”);
第二轮=游戏开始第四轮(4);
第二轮:dealerHand.add(“另一张黑桃王牌”);
roundTwo.playerHand.add(“其他一些愚蠢的卡片”);
System.out.println(roundTwo.compareHands+“wins!”);
}

`

最简单的方法是创建类的新实例。不要担心内存浪费,这是GC的工作。@LuiggiMendoza所以我在文章最后一行中的方法可以满足我的要求吗?如果你的
NewRound
构造函数已经有了重新开始所需的数据,那么它应该可以工作。我认为第一个方法应该可以工作,而不必重置值。为什么不测试一下?@usama8800每次我都需要重置变量
`
public class Game{
    private List<Round> roundList = new LinkedList<Round>();
    public Round startNewRound(int bet){
        Round newRound = new Round(bet);
        this.roundList.add(newRound);
        return newRound;
    }
}

public class Round{
    public List<String> dealerHand;
    public List<String> playerHand;
    public int bet;
    public Round(int bet){
        this.dealerHand = new LinkedList<String>();
        this.playerHand = new LinkedList<String>();
        this.bet = bet
    }
    public String compareHands(){
        //Some comparison between hands, return "dealer" or "player"
    }
}

public static void main(String[] args){
    Game game = new Game();
    Round roundOne = game.startNewRound(1);
    roundOne.dealerHand.add("Ace Of Spades");
    roundOne.playerHand.add("Some other card");
    System.out.println(roundOne.compareHands+" wins!");
    Round roundTwo = game.startNewRound(4);
    roundTwo.dealerHand.add("Another Ace Of Spades");
    roundTwo.playerHand.add("Some other stupid card");
    System.out.println(roundTwo.compareHands+" wins!");
}