构造函数Java编程
调查类将有一个静态类变量,该变量存储当前受访者的ID。当受访者完成调查时,该值将增加1。 Survey类将有一个实例变量来保存调查的标题。 Survey类应该有两个重载构造函数。 第一个构造函数不应接受任何参数。它应该将调查标题设置为默认值“Customer survey”。 第二个构造函数应接受调查标题的字符串值。它应该将静态响应ID重置为零。它应该将调查标题设置为传递给构造函数的值。 survey类应该有一个generateRespondentId方法,该方法返回响应者ID的下一个值。该方法应该将响应者ID的静态实例变量增加1构造函数Java编程,java,constructor,Java,Constructor,调查类将有一个静态类变量,该变量存储当前受访者的ID。当受访者完成调查时,该值将增加1。 Survey类将有一个实例变量来保存调查的标题。 Survey类应该有两个重载构造函数。 第一个构造函数不应接受任何参数。它应该将调查标题设置为默认值“Customer survey”。 第二个构造函数应接受调查标题的字符串值。它应该将静态响应ID重置为零。它应该将调查标题设置为传递给构造函数的值。 survey类应该有一个generateRespondentId方法,该方法返回响应者ID的下一个值。该方法
public class Survey {
String surveyTitle = "";
static int respondentID = 0;
static int generateRespondentID(){
return ++respondentID;
}
public Survey(){
surveyTitle = "Customer Survey";
}
public Survey(String newSurveyTitle){
respondentID=0;
}
public static void main(String[] args) {
Survey s = new Survey();
System.out.println(s.getSurveyTitle());
System.out.println(s.generateRespondentID());
System.out.println();
Survey s2 = new Survey("My Survey");
System.out.println(s2.getSurveyTitle());
System.out.println(s2.generateRespondentID());
System.out.println();
System.out.println(s2.generateRespondentID());
System.out.println();
}
}
这就是我目前所拥有的。我正在与第二个构造函数斗争,它应该将调查标题设置为传递给构造函数的值
第二个构造函数应接受调查标题的字符串值
您需要将“调查标题”字段设置为字符串值,该值作为参数提供给构造函数。如果你不知道怎么做,我建议你研究一下Java中的赋值运算符。我想这就是你想要实现的,在阅读了你的代码后,我能够理解你正在尝试学习如何将构造函数与getter和setter结合使用,你遇到的问题是你没有申报财产调查
`
public class Survey {
private String surveyTitle;
public String getSurveyTitle() {
return surveyTitle;
}
static int respondentID = 0;
static int generateRespondentID(){
return ++respondentID;
}
public Survey(String surveyTitle) {
this.surveyTitle = surveyTitle;
}
public Survey() {
surveyTitle = "Customer Survey";
}
public static void main(String[] args) {
Survey s = new Survey();
System.out.println(s.getSurveyTitle());
System.out.println(Survey.generateRespondentID());
System.out.println();
Survey s2 = new Survey("My Survey");
System.out.println(s2.getSurveyTitle());
System.out.println(s2.generateRespondentID());
System.out.println();
System.out.println(s2.generateRespondentID());
System.out.println();
}
}
`第一类变量定义为私有级别
private String surveyTitle;
private static int respondentID
并通过其getter和setter访问它们
第二个构造函数将收到调查的标题,它应该只分配先前声明的私有变量,并获取getter
public Survey(String newSurveyTitle){
this.surveyTitle = newSurveyTitle;
this.respondentID=0;
}一些注意事项:
你应该链接你的构造函数
您不应该在id生成中使用静态,但如果使用静态,请使它们成为线程安全的
片段:
public class Survey {
static AtomicInteger respondentID = new AtomicInteger(0);
final private String surveyTitle;
public Survey(String surveyTitle) {
this.surveyTitle = surveyTitle;
}
public Survey() {
this("Customer Survey");
}
static int generateRespondentID() {
return respondentID.incrementAndGet();
}
public String getSurveyTitle() {
return surveyTitle;
}
//Other methods...
}
根据您的要求,下面的代码应该可以工作
public Survey(String newSurveyTitle){
respondentID=0;
SurveyTitle = newSurveyTitle;
}
您缺少在构造函数中设置调查标题的代码 欢迎来到堆栈溢出。我们需要更多的信息来帮助你。你的问题是什么?你在苦苦挣扎的是哪一部分?@Code学徒:它在最底层说。@T.J.Crowder我在苦苦挣扎的第二个构造函数是一个陈述,而不是一个问题。@Code学徒:哦,不把它作为一个问题形成在第一次问题的规模上根本算不上什么。让我困扰的是它的模糊性。@T.J.Crowder不把措辞当作问题通常是模糊性的一种症状……而且最容易指出来让我明白我的观点。谢谢!我现在明白了。我只需要添加“获取调查”标题!你欢迎,你可以接受它作为答案或投票表决的解决方案,如果它解决了手头的问题