luohzzz

导航

封装

![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/9002bef2e88c88dcce7f107060527e58.png#pic_center)
封装
```java
public class oopStudent {
//private与public对应的

//private属性私有,private:私有
private String name;
private int id;
private char sex;
private int age;

//get 获得这个数据
public String getName(){
return this.name;
}

//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}
public int getAge(){
return age;
}

public void setAge(int age){
if (age<120 || age>0){
this.age = age;
}else {
this.age = 0;
}
}
}
//test
public class oopDemo9 {
public static void main(String[] args) {

oopStudent luo = new oopStudent();
luo.setName("罗hz");

System.out.println(luo.getName());

luo.setAge(99);
System.out.println(luo.getSex());

}

}

posted on 2021-04-30 17:03  luohzzz  阅读(16)  评论(0编辑  收藏  举报