我有一个静态计数器变量的问题.在超类("卡")中,我有一个变量来计算注册的卡数量(这是一个票务系统).它是这样写的:
public class Card implements Serializable {
private int id;
public static int nextNr= 000;
Card next;
public Card(int t) {
id= ++nextNr;
next= null;
}
}
Run Code Online (Sandbox Code Playgroud)
该类实现了Serializable,我使用ObjectStream将卡写出到文件中.
但是如果我关闭程序并再次启动它,它可以从文件中读取并确认并再次将文件添加到我的cardregistry.但是,超类中的卡计数器变量被重置,我尝试注册的每张新卡都从001开始.我究竟做错了什么?似乎无法在网上找到关于这个特定问题的任何信息.
解决方案: 我使用DataOutputStream在退出时保存它,并使用DataInputStream在启动时读取它.我不知道这是否是最有效的方法,但它有效.非常感谢你的评论,它帮了我很多!!!!
abstract public class Card implements Serializable {
private int type;
private int cardNr;
private static int nextNr = readCardNr();
Card next; //COllections or not.. hmmmm
public Card(int t) {
cardNr= ++nextNr;
next= null;
type = t;
writeCardNr();
}
public int getType(){
return type;
}
public void setCardNr(int i) { …Run Code Online (Sandbox Code Playgroud) 我刚刚在Spring.io http://spring.io/guides/gs/rest-service/上完成了教程,并创建了一个简单的休息服务.但是,有人知道如何以JSON格式返回多个对象吗?例如,如果我有一个带有姓名和身份证的人员班级,我该如何向/人员添加三个人?