gpt4 book ai didi

java - 不断接收对其自身的赋值,并丢失方法体错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:40 25 4
gpt4 key购买 nike

我已经完成这项作业几天了,但我无法解决这个问题。任务如下。

使用以下准则设计并实现一个代表 Person 的类以及 3 个子类:

a.创建一个名为 Person 的类及其三个名为 Employee、Student、Retired 的子类。

b.Person 具有以下数据字段:name、year_of_birth、isStuying 和 isEmployed。它还具有用于设置和获取每个字段的值的方法,以及计算当前年龄和显示人员状态的方法。 Person 类中还包含将 isStuying 和 isEmployed 字段设置为 false 的构造函数。如果您愿意,欢迎您添加其他数据字段和方法。

c.对于您的 Person、Employee、Student 和 Retired 类,getStatus 方法根据属性的当前值返回下表中显示的值:

1.最后,创建一个 Java 测试类来模拟使用您的 Person 类。在您的测试类中,您至少应该:a) 构造 Person 的 4 个实例,b) 打印实例的名称 c) 根据实例的年龄、isStuying 和 isEmployed 属性的值打印实例的状态。

以下代码实际上位于同一项目的 5 个不同文件中。

第一个文件:

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 Student extends Person2 {//begin class

public Student(boolean isEmployed, boolean isStudying, int age) {//begin constructer
super(isEmployed, isStudying);
this.isEmployed = false;
this.isStudying = true;
}//end constructer
}//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 类中的更改,如您的方法中包含拼写错误 public String getStatus(int Age); 即最后的 分号,我不知道编译器如何处理它,通常它被视为空语句,并且当您返回通常为 String 的状态时,因此将方法的类型从 int 更改为字符串,并在 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;
}

PersonTest

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)));
}
}

一切都会顺利

关于java - 不断接收对其自身的赋值,并丢失方法体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com