从另一个类创建对象

时间:2015-05-12 03:34:30

标签: java class object inheritance

我花了一些时间试图解决这个问题,但显然我错过了一些东西,因为我无法让代码正常工作。我目前有一个程序有三个类 - 人,医生和ConsoleApplication。 Person本质上拥有一个构造函数,其中包含firstname,lastname,phonenumber等信息.Moctor是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

我希望用正确的firstName和special来创建对象,但显然我做错了,因为它们不会出现在那里。我不明白为什么专业(规范)领域也不会出现。

这里是我创建人类对象的工作代码 - 我想我只是为了参考而包含这些代码。

        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的对象,我感到非常沮丧。 :(

1 个答案:

答案 0 :(得分:1)

您尚未发布Doctor类的字段,但似乎speciality是字段,spec参数传递给构造函数。而不是

spec = speciality;

它应该是:

speciality = spec;
相关问题