Java 从另一个类创建对象
我花了一些时间试图解决这个问题,但很明显我遗漏了一些东西,因为我无法让代码正常工作。我目前有一个三类的课程-个人、医生和控制台应用程序。Person本质上拥有一个构造函数,其中包含诸如firstname、lastname、phonenumber等信息。Doctor是Person的一个子类,因为我需要的唯一额外信息是他们的医学专业-我的目标是让它继承Person对象中的信息 ConesoleApplication类处理person和doctor类中对象的创建,并为用户提供通过输入修改对象的方法。我已经成功地在这个类中生成Person对象并将它们存储在数组中,但是我不知道如何对Doctor对象执行同样的操作 下面是ConeSoLeapApplication类中AddNewDoctor方法的代码示例。我不会为所有字段提供输入,也不会将对象存储在数组中,因为我只是尝试测试它是否有效Java 从另一个类创建对象,java,class,object,inheritance,Java,Class,Object,Inheritance,我花了一些时间试图解决这个问题,但很明显我遗漏了一些东西,因为我无法让代码正常工作。我目前有一个三类的课程-个人、医生和控制台应用程序。Person本质上拥有一个构造函数,其中包含诸如firstname、lastname、phonenumber等信息。Doctor是Person的一个子类,因为我需要的唯一额外信息是他们的医学专业-我的目标是让它继承Person对象中的信息 ConesoleApplication类处理person和doctor类中对象的创建,并为用户提供通过输入修改对象的方法。我
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;
}
有没有勇敢的灵魂愿意阅读这一切,让我走上正确的道路?当我试图找出为什么不能正确创建医生的对象时,我感到非常沮丧(您还没有发布医生类的字段,但似乎
专业
是字段,规范
是传递给构造函数的参数。因此
spec = speciality;
应该是:
speciality = spec;
这些打印输出来自哪里?是来自
System.out.println(b);
?如果是,请向我们展示Person.toString()
或Doctor.toString()
,无论调用的是哪一个。如果不是,请向我们展示生成打印输出的代码。更广泛地说,如果您可以将代码缩减为a,这将非常有帮助。删除与问题不直接相关的任何代码,例如未被调用的输入提示和构造函数。这不仅会使您的问题变得复杂sier回答,这将帮助你隔离问题。你能告诉我你是如何尝试创建医生对象的吗?在哪里?@lealand当一个问题说我无法正确使用代码时,最后一个SE站点要考虑的是代码审查;在那里发布非工作代码就像在问关于食用花的好书-没有按预期工作的代码没有准备好进行同行评审,这些问题通常在CR上几分钟内就结束了。@Mat'smugh你是正确的,我应该在你开始工作时添加。嗨-谢谢你的回答。结果是正确的,但我有另一个问题。我想我会给你和c最好的答案用更好的代码创建另一个线程。