我不明白我为此构造函数做错了什么
public class Student(String first_name, String last_name, int home1, int home2,int quiz1, int quiz2, int quiz3, int exam1, int exam2)
{
fname=first_name;
lname=last_name;
hw1_grade=home1;
hw2_grade=home2;
quiz1_grade=quiz1;
quiz2_grade=quiz2;
quiz3_grade=quiz3;
exam1_grade=exam1;
exam2_grade=exam2;
}
编译器说:“ {”期望。
另外:
答案 0 :(得分:1)
构造函数在类定义的内部声明为。因此,您首先定义调用,然后在类定义中实现您的构造函数:
public class Student {
//fields
String fname;
String lname;
//... etc.
//here you declare your constructor, like so:
public Student(String first_name, String last_name, int home1, int home2,int quiz1, int quiz2, int quiz3, int exam1, int exam2)
{
fname=first_name;
lname=last_name;
hw1_grade=home1;
hw2_grade=home2;
quiz1_grade=quiz1;
quiz2_grade=quiz2;
quiz3_grade=quiz3;
exam1_grade=exam1;
exam2_grade=exam2;
}
}
另外,在我看来,对于构造函数来说,这些参数太多了。为什么不只包含最重要的那些(名字,姓氏等),然后使用设置方法设置考试成绩?
答案 1 :(得分:1)
def checkForD(value):
if value[0].lower() != 'd':
raise forms.ValidationError('Name must start with d.')
.
.
.
# in my formClass()
name = forms.CharField(max_length=128, validators[checkForD])
...
答案 2 :(得分:0)
如果您要声明一个类,请使用:
public class student{
//statements;
}
否则,如果您要声明该类的构造函数,则:
public class student{
//Parameterized Constructor
public student(//parameters){
//body
}
}