Java package (personal note ten)

Java wrapper

The official concept

As a Java developer, you must be familiar with encapsulation, one of the object-oriented development methods. Encapsulation is an important principle of object - oriented methods, which is to combine the properties and operations (or services) of an object into an independent whole, and to hide the internal implementation details of the object as much as possible.

Encapsulation is to enclose procedures and data, and access to data can only be done through defined interfaces. Object-oriented computing begins with the fundamental concept that the real world can be represented as a series of fully autonomous, encapsulated objects that access other objects through a protected interface.

Encapsulation is an information hiding technology, which is implemented in Java through the keywords private, protected and public.

Encapsulation brings together all the components of an object. Encapsulation defines how a program references the object's data. Encapsulation actually uses methods to hide the class's data. It controls the extent to which users can modify the class and access the data.

Proper encapsulation can make the code easier to understand and maintain, and it also enhances the security of the code.

Second, the application of packaging in Java

Packaging has two levels of packaging

1) Make the attributes of the class private and cannot be directly accessed by the outside world 

2) Encapsulate complex operations or business logic into methods and publish them to the outside world. In this way, the outside world only knows these published methods, but does not need to know their implementation details.

Example - encapsulating private properties of a class with private

//计算器
public class Calc {
private int wdith=20;
private int height=30;
private String color="灰色"; //加int jia(int a,int b){return a+b;}//减int jian(int a ,int b ){return a-b;}








//Show
void show(){
System.out.println("This is a super calculation, width"+wdith +"length" +height+"color"+color);
System.out.println("It can calculate addition, and subtraction");
}

Example - encapsulating a class's private properties and generated get and set methods with private

public class Man{ //The encapsulation of attributes, a person's name, age, and wife are all private attributes of this object (person)
private String name;
private int age;
private Woman wife;
//The encapsulation of the method provided by the person to the outside world, you can Set wife, name, age can also get man's name and age
// method encapsulation
public void setWife(Woman wife) {
this.wife = wife;
}
public String getName() {
return name;
}
public void setName(String name ) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}


public class Woman {
// 属性封装
private String name;
private int age;
private Man husband;
// 方法封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Man getHusband() {
return husband;
}
public void setHusband(Man husband) {
this.husband = husband;
}
}

//仔细看就会发现,Man类没有提供getWife的方法,这是因为男人不想让自己的妻子被外界访问,接下来呢,就是封装可以把一个对象的属性私有,而提供一些可以被外界访问的属性的方法,比如说,name属性,Man和Woman类都有相应的get和set方法,外界都可以通过这些方法访问和修改同时对一些对象不想让外界访问的属性,就不提供其方法,比如说Man的wife属性,就没有get方法

三、封装的作用

1、对象的数据封装特性彻底消除了传统结构方法中数据与操作分离所带来的种种问题,提高了程序的可复用性和可维护性,降低了程序员保持

数据与操作内容的负担。

2、对象的数据封装特性还可以把对象的私有数据和公开数据分离开,保护了私有数据,减少了可能的模块干扰,达到降低程序复杂性、提高可

控性的目的。


Guess you like

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