用户应该在命令行中输入名称,姓氏和年龄,并显示在JoptionPlane中。然后它再次显示博士在参数前面,年龄增加1.但是我的公共地球方法有问题,我一直收到错误
"constructor Person in class Person cannot be applied to given types;
public Earthling(String name1, String fn1, int age1){
^
required: String,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
预期输出(对话框):
比尔约翰逊今年58岁。比尔约翰逊博士今年59岁。
继承我的代码:
import javax.swing.JOptionPane;
public class Inheritance {
/**
* main method which begins the program
*
* @param args is the people and age inputed
*/
public static void main(String[] args){
if (args.length <= 1) {
System.out.println("Please enter a viable argument");
System.exit(1); // ends the program
}
Integer age = Integer.parseInt(args[2]);
// Creates a person object
Earthling familyname1 = new Earthling(args[0],args[1], age);
//put here first so it displays without the Dr.
String firstOutput = familyname1.toString();
//calls the phd and birthday methods
familyname1.phd();
familyname1.birthday();
String secondOutput = familyname1.toString();
JOptionPane.showMessageDialog(null, firstOutput);
JOptionPane.showMessageDialog(null, secondOutput);
}
}
/* Stores the first name and age for a person */
class Person {
protected String name;
protected Integer age;
/**
* The Constructer, used for creating objects and initializing data
*
* @param n1 is the Persons first name
* @param a1 is the Persons age
*
*/
public Person(String n1, int a1){
name = n1;
age = a1;
}
/* adds Dr. to the name */
public void phd() {
name = "Dr. " + name;
}
/* adds 1 to the age */
public void birthday() {
age = age + 1;
}
/**
* displays the data in each objects data field
*
* @return The Persons name, family name and age
*/
public String toString(){
String output = name + " is " + age + " years old.";
return output;
}
}
class Earthling extends Person {
protected String familyName;
//Problem Here!
public Earthling(String name1,String fn1, int age1){
name = name1;
familyName = fn1;
age = age1;
}
public String toString() {
String output = name + familyName + " is " + age + " years old.";
return output;
}
}
答案 0 :(得分:1)
你需要在Earthling的构造函数中调用super(name1, age1)
作为第一件事。
请参阅http://docs.oracle.com/javase/tutorial/java/IandI/super.html,了解Java中super
关键字的解释,并调用超类'constructur。