继承实验室

时间:2015-01-17 23:16:25

标签: java string inheritance

我在一个有几个超级课程的实验室工作。当我使用我编译的TestingClass时,它突出显示以下语法错误:

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

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

测试课程

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);

        }
}

PERSON 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;
  }
}

教师课程

    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;
    }
}

学生课程

    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;
}
}

大学生课程

    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;
    }
}

2 个答案:

答案 0 :(得分:1)

// 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(确定),StringStringdoubleint,{{ 1}}。

我认为你所有的争论都是混乱的,试试这个:

String

答案 1 :(得分:0)

.. aaand在最后说得像这样:

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