回顾昨天的内容
封装
一、该露的露,该藏的藏
①我们程序设计要追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅仅暴露少量的方法给外部使用
二、封装(数据的隐藏)
①通常,应禁止直接访问一个对象中数据的实际表示,而应该通过操作接口来访问,这称为信息隐藏
三、记住这句话就够了;私有属性:get/set
public class text1{
public static void main(String []args){
student xh=new student;
xh.getName("小红") ;//给名字附“小红”
xh.getAge(19);//给年龄附19
xh.getId(1433223);//给学号附1433223
System.out.println(xh.getName());
System.out.println(xh.getAge());
System.out.println(xh.getId());
}
}
public class student{
//属性私有
private String name;//名字
private int id;//学号
private int age;//年龄
//提供一些可以操作这个属性的方法!
// 提供一些public的get、set方法
//get 获得这个数据
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public int getId(){
return this.id;
}
//set 给这个数据设置值
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public void setId(int id){
this.id=id;
}
}
}