【发布时间】:2015-05-12 03:34:29
【问题描述】:
我花了一些时间试图解决这个问题,但显然我遗漏了一些东西,因为我无法让代码正常工作。我目前有一个包含三个类的程序——人、医生和控制台应用程序。 Person 本质上拥有一个包含名字、姓氏、电话号码等信息的构造函数。 Doctor 是 Person 的子类,因为我需要的唯一额外信息是他们的医学专业——我的目标是让它继承 Person 对象中的信息。
ConesoleApplication 类处理人员和医生类的对象创建,此外还为用户提供了一种通过输入修改对象的方法。我已经成功地在这个类中生成了 Persons 的对象并将它们存储在一个数组中,但是我不知道如何对 Doctor 对象做同样的事情。
下面是 ConesoleApplication 类中 AddNewDoctor 方法的代码示例。我没有为所有字段提供输入或将对象存储在数组中,因为我只是想测试它是否有效。
private static void AddNewDoctor() {
int personID = 0;
Person person;
System.out.print("Please enter the identification number of the person that is becoming a doctor: ");
try {
personID = input.nextInt();
input.nextLine();
}
catch (InputMismatchException e) {
System.out.println("Oops!! Please enter only integral numbers");
System.out.println(input.next() + " was not valid input.");
}
person = getPersonID(personID);
if (null == person)
System.out.println ("This person cannot be found \n");
else {
String spec = null;
String firstName = null;
String lastName = null;
String homeAddress = null;
String phoneNumber = null;
firstName = person.getFirstName(firstName);
System.out.print ("Enter speciality of the doctor: ");
spec = input.nextLine();
Doctor b = new Doctor(firstName, lastName, homeAddress, phoneNumber, spec);
System.out.println(b);
}
}
这段代码只会导致
Identification Number: 0
Name: null
Surname: null
Address: null
Mobile/Telephone: null
我希望创建该对象时使用正确的名字和专业,但显然我做错了什么,因为它们没有出现在那里。我不明白为什么专业(规格)字段也没有出现。
这是我用于创建 person 类的对象的工作代码 - 我想我会把它包括在内仅供参考。
private static void AddNewPerson() {
String firstName, lastName, homeAddress, phoneNumber;
input.nextLine();
System.out.print ("Enter the first name of the person: ");
firstName = input.nextLine();
System.out.print ("Enter the last name of the person: ");
lastName = input.nextLine();
System.out.print ("Enter the home address of the person: ");
homeAddress = input.nextLine();
System.out.print ("Enter the phone number of the person: ");
phoneNumber = input.nextLine();
persons[amountPersons] = new Person (firstName, lastName, homeAddress, phoneNumber);
amountPersons++;
System.out.print ("The new person has been successfully added. " + "\n"
+ "" + "\n" );
}
这是 Person 和 Doctor 类的构造函数。
人物
// Default constructor to initialize default instance variables
public Person()
{
firstName = " ";
lastName = " ";
homeAddress = " ";
phoneNumber = " ";
personNumber = 0;
}
//Overloaded constructor designed for objects to spawn
public Person (String firstName, String lastName, String homeAddress, String phoneNumber)
{
// Initialize instance variables
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.phoneNumber = phoneNumber;
personNumber = NumberSystem;
NumberSystem = NumberSystem + 1;
}
医生
public Doctor(String firstName, String lastName, String homeAddress, String phoneNumber, String spec) {
//Constructor with inherited person information and new doctor information
super(firstName, lastName, homeAddress, phoneNumber);
spec = speciality;
int doctorID = 0;
personNumber = doctorID;
}
那里有勇敢的灵魂愿意阅读这一切并让我走上正确的道路吗?试图弄清楚为什么我不能正确创建 Doctor 对象时,我感到非常沮丧。 :(
【问题讨论】:
-
这些打印输出是从哪里来的?是来自
System.out.println(b);吗?如果是这样,请告诉我们Person.toString()或Doctor.toString(),无论哪个被调用。如果没有,请向我们展示生成打印输出的代码。 -
更广泛地说,如果您可以将代码减少到 minimal, complete, verifiable example,这将非常有帮助。删除与问题不直接相关的任何代码,例如输入提示和未调用的构造函数。它不仅可以让您的问题更容易回答,还可以帮助您隔离问题。
-
您能告诉我您是如何尝试创建 Doctor 对象的吗?在哪里?
-
@lealand 当一个问题说我只是无法让代码正常工作时,最后想到的 SE 网站是 Code Review;在那里发布非工作代码就像在这里问有哪些关于可食用花卉的好书 - 不能按预期工作的代码还没有准备好进行同行评审,这些问题通常会在几分钟内关闭 CR .
-
@Mat'sMug 你是对的,我应该在当你让它工作时添加。
标签: java class object inheritance