gpt4 book ai didi

java - 从另一个类创建对象

转载 作者:行者123 更新时间:2023-12-01 11:34:03 25 4
gpt4 key购买 nike

我花了一些时间试图解决这个问题,但显然我错过了一些东西,因为我无法让代码正常工作。我目前有一个包含三个类的程序 - person、doctor 和 ConsoleApplication。 Person 本质上拥有一个包含名字、姓氏、电话号码等信息的构造函数。Doctor 是 Person 的子类,因为我需要的唯一额外信息是他们的医学专业 - 我的目标是让它继承 Person 对象中的信息。

ConesoleApplication 类除了为用户提供通过输入修改对象的方法之外,还处理从 person 类和 doctor 类创建对象的操作。我已经成功地在此类中生成了 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

我希望能够使用正确的名字和专业创建该对象,但显然我做错了一些事情,因为它们没有出现在那里。我不明白为什么专业(spec)字段也没有出现。

这是我用于创建 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 对象而感到非常沮丧。 :(

最佳答案

您还没有发布 Doctor 类的字段,但似乎 speciality 是该字段,而 spec 是传递给构造函数的参数。所以而不是

spec = speciality;

应该是:

speciality = spec;

关于java - 从另一个类创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181321/

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