[英]Extending Java Classes and Overriding Methods with Overloaded Parameters in ES4X/Graal
[英]Java Extending Java Classes and Overriding Their Methods
我要創建一個 Java 控制台應用程序,它定義了一個 class,將其擴展到另外兩個類,覆蓋所有三個類的 toString(),將這些類實例化為三個對象,在這些對象上調用 toString(),並打印出每個 toString() 調用的返回值。
應用程序將從 Course、FlexPathCourse 和 GuidedPathCourse 實例化三個對象,並調用它們對應的 toString() 方法。
我有下面的代碼。 But for the FlexPathCourse.java and GuidedPathCourse.java, I am getting an error message "Constructor object in class object cannot be applied to given types" at where "super(code1, hours, title1)" is. 你能解釋一下這意味着什么以及我應該怎么做嗎? 先感謝您。
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;
}
}
好像您錯過了擴展您的課程 class 之類的,
public class FlexPathCourse extends Course
或者
public class GuidedPathCourse extends Course
Super 引用了它擴展的 class。
[SuperClass] -> [class]
在你的情況下
[Course] -> [FlexPathCourse]
-> [GuidedPathCourse]
為了告訴Java 這種關系(繼承),您需要使用extends
例如: public class FlexPathCourse extends Course {... }
之后,您可以使用 inheritance 屬性,例如多態性,這是 Java 中的一個重要特性。
在這個鏈接中解釋: https://www.w3schools.com/java/java_polymorphism.asp
Constructor object in class object cannot be applied to given types
意味着您沒有聲明這樣的構造函數,這將在您擴展 class 時解決,因為現在它識別您在課程 class 中聲明的 super() 構造函數
如果你想擴展一個 class 你應該在聲明另一個 class 時使用 extend
public class GuidedPathCourse extends Course{
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;
}
}
與 FlexPathCourse 相同
在 GuidedPathCourse 和 FlexPathCourse 添加extends Course
,如下所示:
public class GuidedPathCourse extends Course {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.