Java 继续接收自身的协助,以及缺少方法体错误
我做这项作业已经有几天了,我想不出这个问题。作业如下 使用以下准则设计和实现一个代表个人的类以及3个子类: a、 创建一个名为Person的类及其三个子类Employee、Student、Retired b、 此人有以下数据字段:姓名、出生年份、正在学习和已就业。它还提供了设置和获取每个字段值的方法,以及计算当前年龄和显示个人状态的方法。Person类中还包含一个将isStudying和isEmployed字段设置为false的构造函数。如果愿意,欢迎添加其他数据字段和方法 c、 对于Person、Employee、Student和Retired类,getStatus方法根据属性的当前值返回下表所示的值: 1.最后,创建一个Java测试类,该类使用Person类进行模拟。在测试类中,您至少应该:a)构造4个个人实例,b)打印实例的名称c)根据实例的年龄、isStudying和isEmployed属性值打印实例的状态 以下代码实际上位于同一项目中的5个不同文件中 第一个文件:Java 继续接收自身的协助,以及缺少方法体错误,java,methods,subclass,Java,Methods,Subclass,我做这项作业已经有几天了,我想不出这个问题。作业如下 使用以下准则设计和实现一个代表个人的类以及3个子类: a、 创建一个名为Person的类及其三个子类Employee、Student、Retired b、 此人有以下数据字段:姓名、出生年份、正在学习和已就业。它还提供了设置和获取每个字段值的方法,以及计算当前年龄和显示个人状态的方法。Person类中还包含一个将isStudying和isEmployed字段设置为false的构造函数。如果愿意,欢迎添加其他数据字段和方法 c、 对于Perso
public class Person2 {//begin class
//declare variables
String name;
int year_of_birth;
boolean isStudying;
boolean isEmployed;
int age;
public Person2(boolean isEmployed, boolean isStudying){//begin constructor
this.isEmployed = false;
this.isStudying = false;
}//end constructor
public int getYear(){//get year method
return year_of_birth;
}//end method
public String getName(){//get name method
return name;
}//end method
public boolean getEmployed(){//get employed method
return isEmployed;
}//end method
public boolean getStudying(){//get employed method
return isStudying;
}//end method
public int getAge(int year_of_birth){//get year method
age = 2014 - year_of_birth;
return age;
}//end method
public int getStatus(int age);{//begin method
this.age = age;
if (age < 30 && isStudying == false && isEmployed == false || true){
System.out.println(name + " is a student");
} else if(age > 30 || age < 65 && isStudying == false && isEmployed == false || true){
System.out.println(name + " is an employee");
} else if(age > 65 && isStudying == false && isEmployed == false){
System.out.println(name + " is retired");
}
}//end method
public void setName(String name){//set name method
this.name = name;
}//end method
public void setYear (int year){//set year method
this.year_of_birth = year;
}//end method
public void setEmployed(boolean employed){//set employed method
this.isEmployed = employed;
}//end method
public void setAge (){//set year method
this.age = age;
}//end method
public static void main(String[] args) {
}
}//end class
第三个文件:
class Retired extends Person2 {
public Retired(boolean isEmployed, boolean isStudying) {//begin constructer
super(isEmployed, isStudying);
this.isEmployed = false;
this.isStudying = false;
}//end constructer
}//end class
第四文件:
class Employee extends Person2 {
public Employee(boolean isEmployed, boolean isStudying) {//begin constructer
super(isEmployed, isStudying);
this.isEmployed = true;
this.isStudying = false;
}//end constructer
}//end class
第五个文件:
public class PersonTest {//begin class
public static void main(String[] args) {//begin main
Person2 user1 = new Person2(false, true);
user1.setName("John Doe");
user1.setYear(1986);
System.out.println("The clients name is " + user1.getName() + ".");
System.out.println("The client is " + user1.getAge(1986) + ".");
user1.getStatus(age);
//new user
Person2 user2 = new Person2(true, false);
user2.setName("Mary Joe");
user2.setYear(1975);
System.out.println("The clients name is " + user2.getName() + ".");
System.out.println("The client is " + user2.getAge(1975) + ".");
user2.getStatus(age);
//new user
Person2 user3 = new Person2(true, false);
user3.setName("Forrest Burtner");
user3.setYear(1924);
System.out.println("The clients name is " + user3.getName() + ".");
System.out.println("The client is " + user3.getAge(1924) + ".");
user3.getStatus(age);
//new user
Person2 user4 = new Person2(false, false);
user4.setName("John Connor");
user4.setYear(1910);
System.out.println("The clients name is " + user4.getName() + ".");
System.out.println("The client is " + user4.getAge(1910) + ".");
user3.getStatus(age);
}//end main
}//end class
我所有的问题似乎都源于getStatus方法,不确定我做错了什么。任何帮助都将不胜感激。更改Person2类,因为在您的方法中包含拼写错误
公共字符串getStatus(int age)
即分号最后,我不知道编译器是如何处理的,通常它被视为一个空语句,当您返回的状态通常是String时,因此将方法的类型从int更改为String,并且在PersonTest
类中获取用户的年龄并调用getStatus()
显示年龄并显示结果
public String getStatus(int age){
this.age = age;
if (age < 30 && isStudying == true && isEmployed == false){
return name + " is a student";
} else if((age > 30 || age < 65) && isStudying == false && isEmployed == true){
return name + " is an employee";
} else if(age > 65 && isStudying == false && isEmployed == false){
return name + " is retired";
}
return null;
}
一切正常这一行和其他类似的行可能是问题所在:
if (age < 30 && isStudying == false && isEmployed == false || true)
这是一个关于如何处理该函数的示例。您必须自己应用分配中的规则。我的问题是getStatus方法中的“我接收分配到自身”错误和“缺少方法体”错误:public int getStatus(int age);{//begin method this.age=age;if(age<30&&isStudying==false&&isEmployed==false | | | true){System.out.println(name+“是学生”)}else if(age>30 | | age<65&&isStudying==false&&isemployeed==false | | true){System.out.println(name+“是员工”)}else如果(age>65&&isStudying==false&&isEmployed==false){System.out.println(name+“已退休”);}//end method在PersonTest中“age”在哪里分配了值?请稍等,我正在更新它是的,这解决了问题。不知何故,Person2类中我的setAge方法下仍有一个错误。同样的事情是“分配给它自己”。导致此错误的原因是什么。public void setAge(int age){this.age=age;}如果它解决了问题,你可以在回答问题时接受它,然后投赞成票。汉克斯马上就解决了。试着投赞成票,我的声誉不够高是的,在给出解决方案之前我会尝试分析问题我不认为
public int getStatus(int age);
将以任何方式编译,由于一些愚蠢的错误,向下投票并不是一个天才。就洞察力而言,if语句看起来确实更干净了。我之所以大于30而小于65,是因为它在表中的读取方式。我可以将表张贴在这里,它将正确粘贴。@M.Sharma:确实,还有一个额外的;
那里。重新投票:如果你根据人们对你的问题/答案的评论来假设谁对你的问题/答案投了票,你会错得多,比对的多。即使是对的,抱怨也是毫无意义的。是的,这肯定解决了问题,但作业说我必须在Person2类中创建一个构造函数但是,我想我已经到了我能弄明白这一点的时候了。我感谢你的帮助,我投票支持了这一点。
public class PersonTest {
public static void main(String[] args) {
Person2 user1 = new Person2(false, true);
user1.setName("John Doe");
user1.setYear(1986);
System.out.println("The clients name is " + user1.getName() + ".");
System.out.println("The client is " + user1.getAge(1986) + ".");
System.out.println(user1.getStatus(user1.getAge(1986)));
Person2 user2 = new Person2(true, false);
user2.setName("Mary Joe");
user2.setYear(1975);
System.out.println("The clients name is " + user2.getName() + ".");
System.out.println("The client is " + user2.getAge(1975) + ".");
System.out.println(user2.getStatus(user2.getAge(1975)));
Person2 user3 = new Person2(false, false);
user3.setName("Forrest Burtner");
user3.setYear(1924);
System.out.println("The clients name is " + user3.getName() + ".");
System.out.println("The client is " + user3.getAge(1924) + ".");
System.out.println(user3.getStatus(user3.getAge(1924)));
Person2 user4 = new Person2(false, false);
user4.setName("John Connor");
user4.setYear(1910);
System.out.println("The clients name is " + user4.getName() + ".");
System.out.println("The client is " + user4.getAge(1910) + ".");
System.out.println(user3.getStatus(user4.getAge(1910)));
}
}
if (age < 30 && isStudying == false && isEmployed == false || true)
if ( ( (age < 30)
&&
(isStudying == false)
&&
(isEmployed == false)
)
||
true
)
public int getStatus(int age) { //begin method
this.age = age;
if (age < 30 && isStudying && !isEmployed) {
System.out.println(name + " is a student");
} else if (age >= 30 && !isStudying && isEmployed) {
System.out.println(name + " is an employee");
} else if (age >= 65 && !isStudying && !isEmployed) {
System.out.println(name + " is retired");
} else {
System.out.println(name + " is something else");
}
} //end method