------解决方案-------------------- http://wq882519.blog.163.com/blog/static/981767920081015524299/ 传送门
------解决方案-------------------- Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. 封装这种技术可以让外部通过public方法访问该类中私有的区域。如果一个区域被声明为私有,那么它将不能被外部类看到,因此被这个类所隐藏,正是出于这个原因,封装也被用来隐藏数据。 Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. 封装可以被形容为一个保护屏障,它可以防止代码和数据被外部类任意的随机访问。访问数据和代码被接口严格的控制。 The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code. 封装主要的好处是可以修改我们自己实现的代码而不必破坏其他人所写的代码。封装有可维护性,灵活性和扩展性。 以下代码就实现了一个简单的封装。
Java code
public class EncapTest{
private String name;
private String idNum;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}
------解决方案-------------------- 摘自网络,感觉这样的解释还凑合!