我很感激这里的任何帮助。
执行以下代码,Buyer2 ID始终覆盖buyer1 ID。意味着我总是得到ID = 2。我不确定以下代码有什么问题。我认为这是我的枚举和get方法始终保留最后一个值。
Buyer bOne = new Buyer("buyer1", 1);
Buyer bTwo = new Buyer("buyer2", 2);
其余代码:
public enum Fruits {
Banana("banana", "B"),
Apple("apple","A"),
Orange("orange","O");
private String type, ID;
private Fruits(String type, String ID){
this.type = type;
this.ID = ID;
}
public String getType() {
return type;
}
public String getID() {
return ID;
}
public void setID(String ID){
this.ID = ID;
}
}
public class Player {
private String name;
private Fruits banana, apple, orange;
private int ID;
public buyer(String name, int ID) {
this.name = name;
this.ID = ID;
banana = Fruits.Orange;
apple = Fruits.Apple;
orange = Fruits.Orange;
banana.setID("B"+ID);
apple.setID("A"+ID);
Orange.setID="O"+ID;
}
}
感谢
答案 0 :(得分:1)
你的Fruits
枚举是可变的,这可能不是你想要的。 setID()
上的Fruits
方法可能是设计错误。由于不同的水果(Banana,Apple,...)是一个单一的实例,通过你的所有程序共享,他们需要反映一些globabl状态,而不是某些特定于买家的状态。
尝试删除此setter,看看是否可以重构代码......
答案 1 :(得分:0)
Fruits.java
package test;
public class Buyer {
private String name;
private Fruits banana, apple, orange;
private int ID;
public Buyer(String name, int ID) {
this.name = name;
this.ID = ID;
banana = Fruits.Orange;
apple = Fruits.Apple;
orange = Fruits.Orange;
banana.setID("B"+ID);
apple.setID("A"+ID);
orange.setID("O"+ID);
}
public static void main (String []args){
Buyer bOne = new Buyer("buyer1", 1);
System.out.println("id = " + bOne.ID + " name =" + bOne.name);
Buyer bTwo = new Buyer("buyer2", 2);
System.out.println("id = " + bTwo.ID + " name =" + bTwo.name);
}
}
Buyer.java
function loadSave(){
var saveData = $("#saveText").text();
var pieceData = saveData.split("|");
var posData = [];
var puzzleInfo = pieceData[0].split(",");
createGrid(parseInt(puzzleInfo[1]));
createPieces(parseInt(puzzleInfo[1]), puzzleInfo[0]);
oldSource = imgSource;
imgSource = puzzleInfo[0];
console.log(saveData);
$("#puzzleContainer img").attr("src", imgSource);
for(i=1; i < pieceAmount+1; i++){
posData = pieceData[i].split(";");
$(".piece:nth-child("+i+")").css({
top: posData[0],
left: posData[1],
});
}
$( "#pieceAmount" ).slider({
value: puzzleInfo[1],
});
$("#pieceAmountValue").html(puzzleInfo[1]*puzzleInfo[1]);
}
对consol的结果:
id = 1 name = buyer1
id = 2 name = buyer2
答案 2 :(得分:0)
也许检查你的最后一行:)
$re = '(?s)^<{LANGB}^>(.*)^^.*^<{/LANGB}^>';^
答案 3 :(得分:0)
枚举的每个实例都保证只构造一次,因此最后一个值保留。