封装的概念
1、将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
2、把尽可能多的东西藏起来,对外提供便捷的接口
封装的好处
便于使用者正确使用系统,防止错误修改属性 有助于系统之间的松耦合,提高系统独立性 提高软件的可重用性 降低了构建大型系统的风险
public class Dog {
private String name;
private String color;
private int age;
public Dog() {
}
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("名字: