class Student{
private String name;
private int age;
private String id;
{
System.out.println("学生的国籍为:中国");
}
public Student(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
Show();
}
public int getAge() {
return age;
}
public void setAge(int a) {
if(a>0 && a<130){
age = a;
System.out.println("年龄"+age);
}
else{
System.out.println("你输入的是非法值");
}
}
public void setId(String i){//Id封装了之后,对外提供的一个访问方法。
if(i.length()>=9 && i.length()<=11){
id = i;
System.out.println("学号"+id);
}
else{
System.out.println("你输入的学号小于9位或是大于11位");
}
}
public String getId(){
return id;
}
public void Show(){
System.out.println("姓名:"+name);
}
}
public class StudentDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student st = new Student();
st.setName("美美");
st.setAge(120);
st.setId("2009010412");
}
}
|