ERROR实际和正式参数列表的长度不同

时间:2015-04-24 10:04:42

标签: java

  

构造函数类javaapplication中的Student 14.Student不能   适用于给定类型;需要:   java.lang.String,int,java.lang.String,java.lang.String,double found:   java.lang.String,double reason:实际和正式的参数列表   长度不同

这是给我错误的行

super(idNum, gpa);

这是我的代码:

package javaapplication14;

public class JavaApplication14     
{   
    public static void main(String[] args) 
    {
        Person bob = new Person("Coach Bob", 27, "M");
        System.out.println(bob);

        Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
        System.out.println(lynne);
        Teacher mrJava = new Teacher("Duke Java",34,"M","Computer Science",50000);  
        System.out.println(mrJava);        
     }
}

class Person
{
    protected String myName ;           
    protected int myAge;             
    protected String myGender;    

    public Person(String name, int age, String gender)
    {
        myName = name; 
        myAge = age ; 
        myGender = gender;
    }

    @Override
    public String toString()
    {
        return myName + ", age: " + myAge + ", gender: " +myGender;   
    }


    //These are the set methods
    void setName(String name)
    {
        myName = name;       
    }

    void setAge(int age)
    {
        myAge = age ;       
    }

    void setGender(String gender)
    {
        myGender = gender;       
    }

    //These are the get methods
    String myName()
    {
        return myName;     
    }

    int myAge()
    {
        return myAge;     
    }

    String myGender()
    {
        return myGender;     
    }
}


class Student extends Person
{
    protected String myIdNum;    
    protected double myGPA;

    public Student(String name, int age, String gender,String idNum, double gpa)              
    {
        super(name, age, gender);
        myIdNum = idNum;
        myGPA = gpa; 
    }

    //These are the set methods
    void setmyIdNum(String idNum)
    {
        myIdNum = idNum; 
    }

    void setmyGPA(double gpa)
    {
        myGPA = gpa;  
    }

    @Override
    public String toString()
    {
        return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
    }

    public String myIdNum()
    {
        return myIdNum;
    }

    double myGPA()
    {
        return myGPA;
    }
}


class Teacher extends Person
{
    public double mySalary;
    public String mysubjectName;   

    public Teacher( String name, int age, String gender, String 
subjectName,double salary )
    {
        super(name, age, gender); 
        mysubjectName = subjectName;
        mySalary = salary;  
    }

    //These are the setter methods
    void setsubjectname(String subjectName)
    {
        mysubjectName = subjectName;
    }

    void setsalary(double Salary)
    {
        mySalary = Salary;    
    }

    @Override
    public String toString()
    {
        return super.toString() + ", subject name: " + mysubjectName + ", Salary: "  
+ mySalary;
    }

    //These are the getter methods
    public String getsubjectname(String subjectName)
    {
        return subjectName;
    }
    double getsalary(double Salary)
    {
        return Salary ;
    } 

class CollegeStudent extends Student
{
    public String myMajor;    
    public int myYear;

    public CollegeStudent(String name, int age,String gender, 
    String idNum, double gpa, int year, String major )       
    {
        super(idNum, gpa);
        myMajor = major;
        myYear = year; 
    }


    void setmajor(String Major)
    {
        myMajor = Major;
    }

    void setyear(int Year)
    {
        myYear = Year;
    }

    @Override
    public String toString()
    {
        return super.toString() + ", major: " + myMajor + ", Year: " + myYear;
    }

    //These are the getter methods
    public String getmajor(String myMajor)
    {
        return myMajor;
    }
    public int getyear(int myYear)
    {
        return myYear ;
    }
}
}

3 个答案:

答案 0 :(得分:1)

在Student class

中没有这样的构造函数有两个参数

答案 1 :(得分:0)

您的超类Student具有以下带有5个参数的构造函数

public Student(String name, int age, String gender,String idNum, double gpa) 

但是,在CollegeStudent构造函数中,super(idNum, gpa);期望一个带有两个参数Student(String idNum, double gpa)的构造函数,这在超类中是缺失的。因此,在Student类定义构造函数,如:

 class Student extends Person {
   public Student(String idNum, double gpa){

   }
 }

答案 2 :(得分:0)

班级Student以及班级Person没有constuctor signature (String, double).

所以你必须在其中一个超类中创建这个构造函数,否则你必须调用另一个构造函数。