提问者:小点点

Java扩展了Java类并凌驾于他们的方法之上


我将创建一个Java控制台应用程序,该应用程序定义一个类,将其扩展到另外两个类中,覆盖所有三个类的toString(),将类实例化为三个对象,在这些对象上调用toString(),并输出每个toString()调用的返回值。

应用程序将实例化Course中的三个对象,FlexPathCourse和GuidedPathCourse,并调用它们对应的toString()方法。

我有下面的代码。 但是对于FlexPathCourse.java和GuidedPathCourse.java,我在“super(code1,hours,title1)”所在的位置得到一条错误消息“class object中的Constructor object不能应用于给定的类型”。 你能解释一下那是什么意思吗?我应该怎么做? 提前谢谢你。

Main Class

public class U1A1_InheritOverridetoString {

    public static void main(String[] args) {
        Course c1 = new Course("TBD", 3, "TBD");
        FlexPathCourse c2 = new FlexPathCourse("IT2230", 3, "Introduction to Database Systems");
        GuidedPathCourse c3 = new GuidedPathCourse("ITFP4739", 3, "Mobile Cloud Computing Application Development");

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }

}

Course.java

public class Course {
    protected String code;
    protected int creditHours;
    protected String title;

    public Course(String code1, int hours, String title1){
            code = code1;
            creditHours = hours;
            title = title1;
    }

    @Override
    public String toString(){
        return "Java class name = 'Course' " + "Course Code = " + code;
    }
}

FlexPathCourse.java

public class FlexPathCourse {
    private String optionalResources;

    public FlexPathCourse (String code1, int hours, String title1){
        super(code1, hours, title1);
    }
        @Override
        public String toString(){
            return "Java class name = 'FlexPathCourse' " + "Course Code = " + code;
        }

    }

GuidedPathCourse.java

public class GuidedPathCourse {
    private String requiredResources;
    private int duration;

    public GuidedPathCourse(String code1, int hours, String title1){
        super(code1, hours, title1);
    }
    @Override
    public String toString(){
        return "Java class name = 'GuidedPathCourse' " + "Course Code = " + code;
    }
}

共2个答案

匿名用户

在GuidedPathCourse和FlexPathCourse中添加扩展课程:

public class GuidedPathCourse extends Course {

匿名用户

好像你错过了延长课程的时间,

public class FlexPathCourse extends Course

public class GuidedPathCourse extends Course