正确使用 "extends" 在命令行参数中显示继承
Using "extends" correctly to display inheritance in a Commandline argument
用户应该按顺序在命令行中输入姓名、姓氏和年龄,然后它会显示在 JoptionPlane 中。然后它再次显示在年龄增加 1 的参数前面的博士。但是我的 Public Earthling 方法有问题,我不断收到错误
"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
预期输出(对话框):
Bill Johnson is 58 years old.
Dr. Bill Johnson is 59 years old.
这是我的代码:
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;
}
}
您需要在 Earthling 的构造函数中首先调用 super(name1, age1)
。
有关 Java 中 super
关键字的解释以及调用超类的构造函数,请参阅 http://docs.oracle.com/javase/tutorial/java/IandI/super.html。
用户应该按顺序在命令行中输入姓名、姓氏和年龄,然后它会显示在 JoptionPlane 中。然后它再次显示在年龄增加 1 的参数前面的博士。但是我的 Public Earthling 方法有问题,我不断收到错误
"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
预期输出(对话框):
Bill Johnson is 58 years old.
Dr. Bill Johnson is 59 years old.
这是我的代码:
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;
}
}
您需要在 Earthling 的构造函数中首先调用 super(name1, age1)
。
有关 Java 中 super
关键字的解释以及调用超类的构造函数,请参阅 http://docs.oracle.com/javase/tutorial/java/IandI/super.html。