1.Example:
class Person{
private String name ; // 表示人的姓名
private int age ; // 表示人的年龄
public void tell(){ // 定义说话的方法
System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge()) ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
if(a>=0&&a<=200){
age = a ;
}//通過set方法限制輸入條件
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
};
public class OODemo10{
public static void main(String args[]){
Person per = new Person() ; // 实例化对象
per.setName("张三") ; // 为name属性赋值
per.setAge(-30);
per.tell() ;
}
};
如果有private,沒有set 和get方法下面這樣寫就會報錯,如果都沒有又不能對輸入條件限制。
Person per = new Person() ; per.name = ; per.age = -30;
per.tell() ;
3.powerDesigner的運用。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15127599/viewspace-664245/ ,如需转载,请注明出处,否则将追究法律责任。