2. Java Object Oriented (7)_Encapsulation Idea-Understanding the Encapsulation Idea

2018-04-30

 

Understand the idea of ​​encapsulation

 

Encapsulation is to privatize properties and provide public methods to access private properties.

* Steps to implement encapsulation:

(1) Modify the visibility of the attribute to restrict access to the attribute.

(2) Create a pair of assignment (set method) method and value (get method) method for each property for access to these properties.

(3) In the assignment and value methods, add access restrictions to attributes.

 

**In order to achieve good encapsulation, we usually declare the member variables of the class as private, and then access the variables through public methods . For the operation of a variable, there are generally read and assignment operations. We define two methods to implement these two operations. One is getXxx() (Xxx represents the name of the member variable to be accessed), which is used to read this Member variable operation, the other is setXxx () used to assign values ​​to this member variable.

 

**If an external program can modify the member variables of a class at will, it will cause unpredictable program errors, just like a person's height, which cannot be modified by the outside, and can only be modified .

public class AccpTeacher{

    private String name;

    private int age;

    public String getName(){

    return name;

}

    public void setName(String name){

    this.name = name;

}

    public int getAge(){

    return name;

}

    public void setName(String age){

    if(age<22){

    System.out.println("Age does not match, too young!");

}else{

this.age = age;

}

}

}

 

****Encapsulation Benefits:

(1) Hide the implementation details of the class;

(2) Allow users to access data only through pre-customized methods, and can easily add control

     Logic to limit unreasonable manipulation of properties;

(3) It is easy to modify and enhance the maintainability of the code;

----------------------------------------------------------------------

 

eg:
save contact function:
void save(String name, int age, String phoneNumber, String email, String Address...)
Because multiple information can be saved when saving a contact, the save method must be set to have multiple parameter.
Problem found: The parameter list of the method is exploding.
Think of a contact's information as a whole.
class LinkMan
{
String name;
int age;
String Address;
String phoneNumber;
String email;
}


The above method of saving contacts;
void save(Linkman man){}

---------------------------------------------------------------------------------------

什么是封装(面向对象的三大特征之一):
1.把对象的状态(属性/字段)和行为(功能/方法)看成一个统一的整体,将二者存放在一个独立的模块中(一个类中)。
2.“信息隐藏”:把不需要让外界知道的信息隐藏起来,尽可能隐藏对象功能实现细节,向外暴露方法,保证外界安全(把所有数据信息隐藏起来,尽可能隐藏多的功能,只向外暴露便捷的方法,以供调用)。

-------------------------------------------------------------------------------------------------------------

通过什么来实现隐藏和暴露功功能呢?

高内聚:把该模块的内部数据、功能细节隐藏在模块内部,不允许外界直接干预。
低耦合:该模块只需要给外界暴露少量功能方法。


----------------------------------------------------------------------------------------------------------------

可以把上述代码改成这样,以体现封装性:
class Person
{
  String name;
  private int age;//只能在本类中使用age变量。
  void setAge(int a)
  {
    if(a<0)
    {
      System.out.println("年龄不能为负数");
      return;//表示结束方法
    }
    age = a;
  }
  
}


//演示封装
class PersonDemo
{
  public static void main(String[] args)
  {
    Person p = new Person();
    p.name = "BaiHe";
    p.setAge(17);
    System.out.println(p.name + ","+p.age);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077110&siteId=291194637