【问题标题】:Implicit super constructor StudentTest() is undefined. Must explicitly invoke another constructor"隐式超级构造函数 StudentTest() 未定义。必须显式调用另一个构造函数”
【发布时间】:2014-11-06 03:29:46
【问题描述】:

我正在使用 Eclipse,但出现此错误。

import static org.junit.Assert.*;

public class StudentTest extends Student {

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testGetCurrentEarnedCr() {
        Student student1 = new Student("Jane", "Smith");
        int credits = 45;
        student1.setCurrentEarnedCr(credits);
        assertEquals(credits, student1.getCurrentEarnedCr());
    }

}

我不明白为什么它会给我这个错误。它与Eclipse有关吗?我在网上查了一下,并尝试了建议的解决方案来修复它,如果它实际上是一个 Eclipse 错误但它没有解决任何问题。

此外,如果这有助于解决任何问题,这里是 Student 类。

public class Student
{
    // Information about the individual student
    private double gpa;
    private String firstName, lastName, id;
    private int currentEarnedCr; // Current earned credits (already completed)
    private int anticipatedAdditionalCr; // Anticipated additional credits (currently taking)
    private boolean lascComplete; // Has the student completed LASC requirements
    private boolean majorComplete; //Has the student completed requirements for the major

    // Minimum number of credits required to graduate
    public static final int REQUIRED_CR = 120;

    // Keeps track of the id number to assign to the next student to be created
    private static int nextId = 1;

    /**
     * Creates a new student given a first and last name. An id number is assigned sequentially.
     *
     * @param firstName the student's first name
     * @param lastName the student's last name
     */
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = String.format("%07d",nextId); // 7 digits wide, zero padded
        nextId++;
    }

    /**
     * Returns the number of students which have been created.
     *
     * @return the number of students which have been created
     */
    public static int getStudentCount() {
        return nextId-1;
    }

    /**
     * Set the GPA of the student.
     *
     * @param gpa the student's GPA
     */
    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    /**
     * Returns the student's GPA.
     *
     * @return the student's GPA
     */
    public double getGpa() {
        return gpa;
    }

    /**
     * Sets the student's first name.
     *
     * @param firstName the student's first name
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Returns the student's first name.
     *
     * @returns the student's first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the student's last name.
     *
     * @param lastName the student's last name
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Returns the student's last name.
     *
     * @returns the student's last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Returns the student's ID number.
     *
     * @returns the student's ID number
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the student's current earned credits.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param currentEarnedCr the number of current earned credits
     */
    public void setCurrentEarnedCr(int currentEarnedCr) {
        this.currentEarnedCr = currentEarnedCr;
    }

    /**
     * Returns the student's current earned credits.
     *
     * @return the student's current earned credits
     */
    public int getCurrentEarnedCr() {
        // This should really be done by looking at a
        // list of completed courses, not by returning a variable.
        return currentEarnedCr;
    }

    /**
     * Sets the student's anticipated additional credits (the
     * courses they are currently taking).
     * (This should really be done by looking at a
     * list of registered courses, not by setting it directly.)
     *
     * @param anticipatedAdditionalCr the number of anticipated additional credits
     */
    public void setAnticipatedAdditionalCr(int anticipatedAdditionalCr) {
        this.anticipatedAdditionalCr = anticipatedAdditionalCr;
    }

    /**
     * Returns the student's anticipated additional credits.
     *
     * @return the student's anticipated additional credits
     */
    public int getAnticipatedAdditionalCr() {
        // This should really be done by looking at a
        // list of registered courses, not by returning a variable.
        return anticipatedAdditionalCr;
    }

    /**
     * Sets whether the student has completed the LASC requirements.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param lascComplete whether LASC requirements are complete
     */
    public void setLascComplete(boolean lascComplete) {
        this.lascComplete = lascComplete;
    }

    /**
     * Returns whether the student has completed the LASC requirements.
     *
     * @return whether the student has completed the LASC requirements
     */
    public boolean getLascComplete() {
        // This should really be done by looking at a
        // list of complete courses, not by returning a variable.
        return lascComplete;
    }

    /**
     * Sets whether the student has completed the major requirements.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param majorComplete whether major requirements are complete
     */
    public void setMajorComplete(boolean majorComplete) {
        this.majorComplete = majorComplete;
    }

    /**
     * Returns whether the student has completed the major requirements.
     *
     * @return whether the student has completed the major requirements
     */
    public boolean getMajorComplete() {
        // This should really be done by looking at a
        // list of complete courses, not by returning a variable.
        return majorComplete;
    }

    /**
     * Returns the student's remaining credits to graduate
     * (not including the courses they are currently taking).
     *
     * @return the student's remaining credits to graduate
     */
    public int getCurrentRemainingCr() {
        return REQUIRED_CR - currentEarnedCr;
    }

    /**
     * Returns the student's anticipated remaining credits to graduate
     * (including the courses they are currently taking).
     *
     * @return the student's anticipated remaining credits to graduate
     */
    public int getAnticipatedRemainingCr() {
        return getCurrentRemainingCr() - anticipatedAdditionalCr;
    }

    /**
     * Returns whether the student is ready to graduate.
     *
     * @return whether the student is ready to graduate
     */
    public boolean readyToGraduate() {
        return getCurrentRemainingCr() == 0 && gpa >= 2.0 && lascComplete && majorComplete;
    }
}

【问题讨论】:

标签: java constructor subclass


【解决方案1】:

这不是 eclipse 的问题,而是您在 Studenttest 中继承 Student 类的问题。通常,如果超类没有默认构造函数,则子类应该有一个构造函数,该构造函数应该显式调用父类构造函数。在您的情况下,您的父 Student 类只有一个带参数的构造函数,因此任何扩展 Student 的类都应该有一个构造函数,该构造函数使用 super(firstName.lastName) 显式调用 Student 的这个构造函数,否则 java 将不允许您扩展您的类。

作为您的问题的解决方案,您无需在 StudentTest 类中扩展 Student 类。通常,任何测试任何其他类的功能的测试类都不需要扩展它。

【讨论】:

    【解决方案2】:

    Student 声明一个构造函数:public Student(String firstName, String lastName)

    当您扩展 Student 时,您的子类必须调用超类的构造函数之一。由于Student 只有一个构造函数,您必须调用该构造函数。在 Java 中,构造函数不是通过扩展类来继承的

    您的StudentTest 类扩展了Student,但没有为自己声明构造函数。在这种情况下,编译器将为您创建一个公共的、无参数的构造函数。不幸的是,编译器无法猜测 firstNamelastName 的值应该是什么,因此您需要编写自己的 StudentTest 构造函数:

    public StudentTest()
    {
        super("first", "last");
    }
    

    您当然可以在super 调用中使用不同的值,但您必须以一种或另一种方式提供这些值。将相同的构造函数参数镜像为父类并将值推迟到使用测试类的位置并不少见:

    public StudentTest(String firstName, String lastName)
    {
        super(firstName, lastName);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 2020-09-29
      • 2023-04-08
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      相关资源
      最近更新 更多