英文:
Hello, in Java, i was a bit confused about the calling constructors into objects in main method. Here below is the example of it
问题
在主方法中尝试创建Course对象时,Eclipse会显示"构造函数Course()未定义",我不知道为什么会出现这种情况。
这是我的Course类及其构造方法;
public class Course{
private String CourseCode;
private String day;
private int StudentCount;
private int Capacity;
private double averageGrade;
public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode = CourseCode;
this.day = day;
this.StudentCount = StudentCount;
this.Capacity = Capacity;
this.averageGrade = averageGrade;
}
}
以及试图创建名为Course的对象的主方法;
Course c = new Course();
英文:
When i am trying to create an Course object in main method, eclipse says that "The constructor Course() is undefined" i dont know why it occurs.
Here is my Course class and its constructions;
public class Course{
private String CourseCode;
private String day;
private int StudentCount;
private int Capacity;
private double averageGrade;
public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode=CourseCode;
this.day = day;
this.StudentCount =StudentCount;
this.Capacity = Capacity;
this.averageGrade =averageGrade;
}
And the main method that trying to create an object called Course;
Course c = new Course();
答案1
得分: 1
现在您已经定义了构造函数,您需要使用它并提供所有指定的参数。
Course c = new Course(
"CMSC-101",
"Wednesday",
/* 学生人数 */ 20,
/* 容量 */ 25,
/* 平均成绩 */ 4.0);
英文:
Now that you've defined a constructor, you need to call it with all the arguments it specifies.
Course c = new Course(
"CMSC-101",
"Wednesday",
/* student count */ 20,
/* capacity */ 25,
/* average grade */ 4.0);
答案2
得分: 0
Course c = new Course();
通过在主方法中编写上述代码行,您正在调用默认构造函数,即不传递参数作为参数,在这种情况下,字段会被初始化为它们的默认值。
而在您的代码中
public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode=CourseCode;
this.day = day;
this.StudentCount =StudentCount;
this.Capacity = Capacity;
this.averageGrade =averageGrade;
}
是一个带参数的构造函数,您正在传递一些值(参数),并使用这些值设置变量的某些状态。
在主方法中遇到编译问题,因为它找不到类中的默认构造函数。
解决方法
要么像这样传递参数:
Course c = Course("ABC", "Monday", 23, 40, 2.30);
要么创建一个默认构造函数(两种构造函数都可以存在于您的类中)。
英文:
Course c = new Course();
by writing above line in main method you're invoking a default constructor i.e. no params passed as an argument, in this case the fields are initialized of there default values.
whereas in your code
public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode=CourseCode;
this.day = day;
this.StudentCount =StudentCount;
this.Capacity = Capacity;
this.averageGrade =averageGrade;
}
is a Parametrized contsructor i.e. your passing some values (Parameter) and your setting some state of your variables using that.
in main method encounter compilation issues because it won't find a default constructor on your class
solution
either pass Param like
Course c = Course("ABC", "Monday", 23, 40, 2.30);
or create a default constructor (Both the constructors can exist in your class)
答案3
得分: 0
因为您正在创建带参数的构造函数,它替代了类的默认构造函数,在创建对象时没有传递任何参数。
有两种方法可以解决这个问题。
- 在您的类中创建一个非参数化的构造函数,同时保留您的带参数构造函数。
public Course{
public Course(){}
}
- 在创建对象时在构造函数内传递参数。
public Course{
Course c = new Course("CourseCode", "Monday", 10, 100, 90.5);
}
英文:
Because you are creating Parameterized constructor and it's replacing default constructor of your class, and while you are creating object you are not passing any argument.
There are two way to solve this problem.
- Create one non parameterized constructor in your class along with your parameterized constructor.
public Course{
public Course(){}
}
- While creating object pass argument within constructor.
public Course{
Course c = new Course("CourseCode", "Monday", 10, 100, 90.5);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论