java技术博客

jsp博客
数据加载中……
StudentTest2.java
/*
*学生类,包括学生的基本信息
*/
public class studentTest2{
public static void main(String[] args){
int i;
for(i=0;i<10;i++) {
Student tom=new Student("Tom"+i);
if(i%2==0){
tom.setStudentSex("man");
}else{
tom.setStudentSex("female");
}
tom.setStudentAddress("America");
tom.setStudentNumber();
System.out.println(tom.toString());
}}}
class Student{
private String strName="";
private int number=0;
private String strSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
private Static int nextNumber=1;
public Student(String name){
strName=name;
}
public String getStudentName(){
return strName;
}
public int getStudentNumber(){
return number;
}
public void setStudentNumber(){
number=nextNumber;
nextNumber++;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpciality;
}
public void setStudentSpeciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public String toString (){
String information="student name="+strName+",student number="+number;
if(!strSex.equals(""))
information +=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}}

posted on 2008-10-21 20:25 郭兴华 阅读(155) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: