Java 实例化新对象时如何传入类类型的参数?
假设我创建了一个名为“grade”的类和一个长度为32的grade类型数组。我可以使用什么样的数据类型作为下面方法中的第二个参数来执行所需的代码Java 实例化新对象时如何传入类类型的参数?,java,Java,假设我创建了一个名为“grade”的类和一个长度为32的grade类型数组。我可以使用什么样的数据类型作为下面方法中的第二个参数来执行所需的代码 grade[] studentGrades = new grade[32]; public static void populateArray(grade[] list, ????? grade){ for(int index = 0; index < list.length; index++){ //
grade[] studentGrades = new grade[32];
public static void populateArray(grade[] list, ????? grade){
for(int index = 0; index < list.length; index++){
// grade here should call the default construcor of the grade class
list[index] = new grade();
} //end of for loop
} //end of populateArray
grade[]学生成绩=新成绩[32];
公共静态无效公共阵列(等级[]列表,等级){
for(int index=0;index
您不需要第二个参数。new grade()
调用使用不需要参数的构造函数实例化一个新的grade对象(如果没有其他具有该签名的构造函数,则为默认构造函数)
顺便说一句,我们希望类的名称是大写的,变量的名称不是大写的
您的代码应该如下所示
Grade[] studentGrades = new Grade[32];
public void populateArray(Grade[] list){
for(int index = 0; index < list.length; index++){
// grade here should call the default construcor of the grade class
list[index] = new Grade();
} //end of for loop
} //end of populateArray
Grade[]学生成绩=新成绩[32];
公共无效公共阵列(等级[]列表){
for(int index=0;index
如果grade
已经是一个类,那么就不应该有第二个参数。你打算为grade的子类传入名称吗?谢谢。我解决了我的问题。该方法需要一个抛出FileNotFoundException语句。FileNotFoundException?显然是发布信息太少/问题错误的情况?:)不,我得到了我想要的答案,而且是正确的。我的方法的应用就是给出FileNotFoundException的方法。因此,我关于FileNotFoundException的声明(不是编码声明)对于讨论来说信息太少。