2011-08-31 165 views
-1

有人告诉我,当创建一个新对象时,它需要具有与其构造函数相同的参数。我曾试过,但我仍然得到这些错误。无法找到符号s1.getCourse s1.getName s1.getAge。还有一个无效的构造函数错误。我的继承人代码构造函数错误

public class Person{ 
    private String name; 
    private int age; 

    public Person(String name, int age){ 
    this.name=name; 
    this.age=age; 
    } 
    public String getDetails(){ 
    return "Name: " + name + "Age: " + age; 
    } 

    public void setName(String name){ 
    this.name=name; 
    } 
    public void setAge(int age){ 
    this.age=age; 
    } 

public String getName(){ 
return name; 
} 
public int getAge(){ 
return age; 
} 
     public class Student extends Person{ 
      private String course; 

      public Student(String name, int age,String course){ 
       super(name,age); 
       this.course = course; 
      } 
      public String getDetails(){ 
       return super.getDetails()+"Course: "+ course; 
      } 
      public void setCourse(String course){ 
       this.course=course; 
      } 
      public String getCourse(){ 
       return course; 
      } 

      public String getName(){ 
       return name; 
      } 
     } 

     public String getAge(){ 
      return age; 
     } 
    } 

TestPerson

student++; 
    String name=array[0]; 
    int age=Integer.parseInt(array[1]); 
    String course=array[3]; 
    Student s1= new Student(name,age,course); 


    System.out.println("name "+s1.getName()); 
    System.out.println("age "+ s1.getAge()); 
    System.out.println("course: " + s1.getCourse()); 

    } 
} 
+0

请向我们显示错误消息。你还要导入“学生”班吗? – trojanfoe

+0

你的'人'级是什么? – BoltClock

+0

Anre'name'和'age'受保护或公开? –

回答

0

欲了解更多详细信息u能提供Person类。

您尚未在学生课堂中提供姓名和年龄属性。 如果所有这些都出现在超类中,那么当你调用s1.getName()方法时,它将从学生类中调用。因为它存在于亚班级即学生

+0

能有人帮忙吗?它说找不到符号s1.getName(),但它们通过person类继承。 – JGE18