我有一个作业,我必须显示一个类的所有属性。
我的问题是如何在不必为person对象添加额外属性的情况下使代码工作?
我让代码工作但显然我不应该对原来的 person 类进行任何更改。这是:
class person
{
String firstName;
String lastName;
int age;
person(String informedFirstName, String informedLastName, int informedAge)
{
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}
String whatIsUp()
{
return "undetermined";
}
String getName()
{
return firstName +" "+lastName;
}
String getAge()
{
String str = String.valueOf(age);
return str;
}
String getInfo()
{
return ("Name = "+ getName() + " age="+ getAge());
}
}
我的工作代码是:
app class
class app
{
public static void main(String[] args)
{
student st1 = new student("Zack","Mills",21,".....");
professor p1 = new professor("Fred","Fonseca",44,".....");
TA ta1 = new TA("Paris","Hilton",20,"......");
System.out.println(st1.getInfo());
System.out.println(p1.getInfo());
System.out.println(ta1.getInfo());
}
}
人类:
class person
{
String firstName;
String lastName;
int age;
String status;
person(String informedFirstName, String informedLastName,
int informedAge, String informedStatus) //< ---- added attribute informedStatus
{
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
status = informedStatus;
}
String whatIsUp()
{
return "undetermined";
}
String getName()
{
return firstName +" "+lastName;
}
String getAge()
{
String str = String.valueOf(age);
return str;
}
String getStatus(){
return status;
}
String getInfo()
{
return ("Name = "+ getName() + " age="+ getAge()) + getStatus();
}
}
学生班:
class student extends person
{
String status;
student(String informedFirstName, String informedLastName, int informedAge, String informedStatus)
{
super(informedFirstName, informedLastName, informedAge, informedStatus);
if (age <= 25) status = informedStatus = "Traditional";
else status = informedStatus = "Non-Traditional";
}
String whatIsUp()
{
int n = 0;
String b = "...";
n = (int) (Math.random()*2);
if (n == 0) b = "reading";
if (n == 1) b = "talking";
return b;
}
String getStatus()
{
return " Status = " + status;
}
}
教授班:
class professor extends person
{
String status;
professor(String informedFirstName, String informedLastName, int informedAge, String informedStatus)
{
super(informedFirstName, informedLastName, informedAge, informedStatus);
status = informedStatus = "Computer Science";
}
String whatIsUp()
{
int n = 0;
String b = "...";
n = (int) (Math.random()*2);
if (n == 0) b = "reading";
if (n == 1) b = "talking";
return b;
}
String getStatus()
{
return " Degree = " + status;
}
}
TA课程:
class TA extends person
{
String status;
TA (String informedFirstName, String informedLastName, int informedAge, String informedStatus)
{
super(informedFirstName, informedLastName, informedAge, informedStatus);
status = informedStatus = "Excellent";
}
String whatIsUp()
{
int n = 0;
String b = "...";
n = (int) (Math.random()*2);
if (n == 0) b = "reading";
if (n == 1) b = "talking";
return b;
}
String getStatus()
{
return " Communication Skills = " + status;
}
}
对不起所有代码,非常感谢任何帮助。
答案 0 :(得分:0)
我不确定在为person类添加一个属性时会出现什么问题。但是,如果您不想触摸它,我会建议您另类。
创建一个扩展人的类扩展。将新状态属性添加到该类。 现在为TA,学生和教授扩展新的personExtension类。
class personExtension extends person
{
String status;
personExtension(String informedFirstName, String informedLastName, int informedAge, String informedStatus)
{
super(informedFirstName,informedLastName,informedAge);
status = informedStatus;
}
}
答案 1 :(得分:0)
实际上,您可以通过从studen
扩展person
并在新类中使用新属性status
来完成所有操作。这样做你就拥有一个具有person
所有属性的类,包括添加的新属性。
只需从人员中删除属性status
,然后根据需要保持不变。
在student
的构造函数中,使用person
关键字传递super
的属性。
student(String informedFirstName, String informedLastName, int informedAge,
String informedStatus)
{
super(informedFirstName, informedLastName, informedAge);
if (age <= 25) status = informedStatus = "Traditional";
else status = informedStatus = "Non-Traditional";
}
班级student
的对象可让您访问所有属性
student s = new student("John", "Doe", 25, "some status");
只需添加一个,请尝试尊重java命名约定,类名应以大写字母开头。
修改强>
要让getInfo()
返回包含状态的字符串,您必须在子类student
中覆盖该方法,然后再次使用super
关键字调用getInfo()
来自{ {1}}并将person
连接到它。
status