从另一个类创建对象

时间:2020-11-29 20:12:02

I've spent some time trying to solve this, but clearly I'm missing something because I just can't get the code to work correctly. I currently have a program with three class - person, doctor and ConsoleApplication. Person essentially holds a constructor with information like firstname, lastname, phonenumber etc. Doctor is a subclass of Person as the only extra information I need is their medical specialty - I'm aiming for it to inherit the information in the Person object.

我花了一些时间试图解决这个问题,但显然我错过了一些东西,因为我无法让代码正常工作。我目前有一个程序有三个类 - 人,医生和ConsoleApplication。 Person本质上拥有一个构造函数,其中包含firstname,lastname,phonenumber等信息.Moctor是Person的子类,因为我需要的唯一额外信息是他们的医学专业 - 我的目标是继承Person对象中的信息。

The ConesoleApplication class handles creation of objects from the person and doctor classes, in addition to giving the user a way to modify the objects through input. I've managed to successfully spawn objects of Persons in this class and store them in an array, but I can't figure out how to do the same for the Doctor objects.

除了为用户提供通过输入修改对象的方法之外,ConesoleApplication类还处理来自person和doctor类的对象的创建。我已成功地在这个类中生成Persons对象并将它们存储在一个数组中,但我无法弄清楚如何为Doctor对象做同样的事情。

Here's an example of the code for the AddNewDoctor method in the ConesoleApplication class. I don't provide inputs for all the fields or store the object in an array because I'm simply trying to test if it works.

下面是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);

    }

}

This code will simply result in

这段代码只会导致

Identification Number: 0
Name: null
Surname: null
Address: null
Mobile/Telephone: null

I was hoping that the object would be created with correct firstName and specialty, but apparently I've done something wrong because they don't appear there. I don't understand why the speciality (spec) field doesn't appear either.

我希望用正确的firstName和special来创建对象,但显然我做错了,因为它们没有出现在那里。我不明白为什么特殊(spec)字段也没出现。

Here's the working code I have for creating objects of the person class - I thought I'd include this just for reference.

这是我用于创建人类对象的工作代码 - 我想我将其包含在内仅供参考。

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

Here's the constructors for the Person and Doctor classes.

这是Person和Doctor类的构造函数。

Person

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

Doctor

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

Is there are brave soul out there that is willing to read this all and put me on the correct path? I've gotten very frustrated from trying to figure out why I can't create objects of Doctor correctly. :(

是否有勇敢的灵魂愿意阅读这一切并让我走上正确的道路?我试图找出为什么我无法正确创建Doctor的对象,我感到非常沮丧。 :(

1 个解决方案

#1


You haven't posted the fields for Doctor class, but it seems speciality is the field and spec the parameter passed to constructor. So instead of

你还没有发布Doctor类的字段,但似乎特殊的是字段和spec传递给构造函数的参数。而不是

spec = speciality;

it should be:

它应该是:

speciality = spec;

#1


You haven't posted the fields for Doctor class, but it seems speciality is the field and spec the parameter passed to constructor. So instead of

你还没有发布Doctor类的字段,但似乎特殊的是字段和spec传递给构造函数的参数。而不是

spec = speciality;

it should be:

它应该是:

speciality = spec;