继承实验室

Inheritance Lab

我和超级情侣 类 一起在实验室工作。当我使用我编译的 TestingClass 时,它突出显示了以下语法错误:

CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",

以下是我在本实验中的所有代码:

测试中CLASS

public class TestingClass
 {
 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);

CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
                                         4.0, 1, "English");
System.out.println(ima);

        }
}

个人CLASS

    public class Person
{
  private String myName ;   // name of the person
  private int myAge;        // person's age
  private String myGender;  // "M" for male, "F" for female

  // constructor


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

  public String getName()
  {
    return myName;
  }


    public int getAge()
    {
    return myAge;
  }

  public String getGender()
  {
    return myGender;
  }

  public void setName(String name)
  {
    myName = name;
  }

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

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

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

老师CLASS

    public class Teacher extends Person
{
    // instance variables - replace the example below with your own
    private String mySubject;
    private double myAnnualSalary;

    /**
     * Constructor for objects of class Teacher
     */
    public Teacher(String name, int age, String gender, String Subject, double AnnualSalary)
    {
        // initialise instance variables
        super(name, age, gender);
        mySubject = Subject;
        myAnnualSalary = AnnualSalary;
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
   public String getSubject()
   {
       return mySubject;
   }

   public double getAnnualSalary()
   {
       return myAnnualSalary;
   }

   public void setSubject(String Subject)
   {
       mySubject = Subject;
   }

   public void setAnnualSalary(double AnnualSalary)
   {
       myAnnualSalary = AnnualSalary;
   }

   public String toString()
   {
       return super.toString() + "Subject:" + mySubject + "Annual Salary:" + myAnnualSalary;
    }
}

学生CLASS

    public class Student extends Person
    {
        private String myIdNum;    // Student Id Number
        private double myGPA;      // grade point average

        // constructor
        public Student(String name, int age, String gender,
                 String idNum, double gpa)
                 {
            // use the super class' constructor
            super(name, age, gender);

            // initialize what's new to Student
            myIdNum = idNum;
            myGPA = gpa;
        }

        public String getIdNum()
        {
            return myIdNum;
        }

        public double getGPA()
        {
            return myGPA;
       }


public void setIdNum(String idNum)
{
    myIdNum = idNum;
}

public void setGPA(double gpa)
{
    myGPA = gpa;
}

// overrides the toString method in the parent class
public String toString()
{
return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
}
}

大学生CLASS

    public class CollegeStudent extends Student
{
    // instance variables - replace the example below with your own
    private int myYear;
    private String myMajor;
    /**
     * Constructor for objects of class dasf
     */
    public CollegeStudent(String idNum, double gpa, String name, int age, String gender, int Year, String Major)
    {
        // initialise instance variables
        super(name, age, gender, idNum, gpa);
        myMajor = Major;
        myYear = Year;
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public String getMajor()
    {
        return myMajor;
    }

    public int getYear()
    {
        return myYear;
    }

    public void setMajor(String Major)
    {
        myMajor = Major;
    }

    public void setYear(int Year)
    {
        myYear = Year;
    }
    public String toString()
    {
        return super.toString() + "Year:" + myYear + "Major:" + myMajor;
    }
}
// Your call:
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
                                      4.0, 1, "English");

// The constructor
public CollegeStudent(String idNum, double gpa, String name, 
            int age, String gender, int Year, String Major)

如您所见,构造函数接受了很多参数,并且在执行此操作时类型很重要。

它要的类型有:StringdoubleStringintStringintString.

但是,您传递的是:Stringint(好的)、StringStringdoubleintString

我认为你只是把参数弄乱了,试试这个:

CollegeStudent ima = new CollegeStudent("UCB123",  4.0, "Ima Frosh", 18,  
                                        "F", 1, "English");

..aa 最后这样写:

return super.toString() + ", Year: " + myYear + ", Major: " + myMajor;