2. 定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。SetRecord给出学号、姓名和成绩的赋值,GetRecord通过学号得到考生的成绩。
public class Student {
/**
* @param args
*/
private int ID;
private String name;
private float score;
public void SetRecord(int ID,String name,float score){
this.ID=ID;
this.name=name;
this.score=score;
}
public float getRecord(int ID){
if(ID==this.ID)
return this.score;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.SetRecord(0,"alex",100);
float Sco=s.getRecord(0);
System.out.print(Sco);
}
}
给出上题中设计类的构造函数,要求初始化一条记录(学号、姓名、成绩)。
public class Student {
/**
* @param args
*/
private int ID;
private String name;
private float score;
Student(int ID,String name,float score){
this.ID=0;
this.name="666";
this.score=65;
}
public void SetRecord(int ID,String name,float score){
this.ID=ID;
this.name=name;
this.score=score;
}
public float getRecord(int ID){
if(ID==this.ID)
return this.score;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student(0,"sdfs",12);
//s.SetRecord(0,"alex",100);
float Sco=s.getRecord(0);
System.out.print(Sco);
}
}
|