(1)什么是封装?封装的好处?
定义:隐藏对象的属性和细节实现,仅仅对外提供公共访问方式。
好处:将变化隔离
便于使用
提高代码重用性
提高安全性
分类:
狭义的封装:属性的封装与方法的封装 。
广义的封装:包的管理、组件、框架、应用程序。
例如:
//被封装的类
public class Encapsulation {
private int money;//private控制符,将money变量设置为私有变量,外部不可以直接访问。
public int getMoney(){
return money;
}
public void setMoney(int addmoney){
this.money=addmoney;
}
}
//调用封装好的类的测试类:
public class EncapsulationTest {
public static void main(String[] args) {
Encapsulation e = new Encapsulation();
e.setMoney(9999);
int a = e.getMoney();
System.out.println("一共有金额:"+a);
}
}
(3)类的控制,方法控制符,属性修饰符详解,推荐这个博客文章:https://blog.csdn.net/u012454084/article/details/79967135