Encapsulation and access control of Java (1)
Encapsulation and access control of Java (1)
I always think I understand the concept of encapsulation, but I really can't say anything if I really want to say it. I can only silently improve my understanding of packaging through the examples around me and Book Theory.
For example, when we play games, we can only obtain gold coins by completing the specified tasks, and we can't directly modify the value of gold coins. As players, if we can easily modify secrets, it's a mess. The designers obviously don't want us to do this. They allow us to enjoy the game, but these taboos can't be touched. This is an example of encapsulation.
Concept of encapsulation
Encapsulation is one of the three characteristics of object-oriented. The state information of the object is hidden inside the object. External programs are not allowed to directly access the internal information of the object, but operate and access the internal information through the methods provided by this class.
advantage:
Consider:
Hide the hidden and expose the exposed.
Access control character
Control level public > protected > Default > private
Access control level table
Private (access right of current class): the decorated member can only be accessed inside the class.
Default (package access permission): default means that there are no modifiers. The default members can be accessed by other classes in the same package. The concept of package will be mentioned later.
Protected (subclass access permission): modified members can be accessed by other classes in the same package or subclasses in different packages. The subclasses and superclasses will be summarized later~
Public: the modified member can be accessed by all other classes, whether in the same package or with inheritance relationship.
be careful
Property privatization
Let's take a look at the simple classes and tests we wrote earlier:
package com.my.pac08;
public class People {
public static void main(String[] args) {
Man man = new Man();
man.age = -4;
man.name = "12345";
man.run();
}
}
class Man {
int age;
String name;
void run() {
System.out.println("running..");
}
}
package com.my.pac07;
public class Person {
//private 修饰符对成员变量 进行隐藏
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
if (name.length() > 5) {
System.out.println(name+"的长度太长,取名失败!");
return;
}
System.out.println("取名成功!");
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0) {
System.out.println("输入年龄不合法!");
return;
}
this.age = age;
}
}
package com.my.pac07;
public class PersonTest {
public static void main(String[] args) {
Person p = new Person();
/*private修饰属性,不在同一个类中无法直接访问,需要
使用对应的getter和setter方法。
错误 p.name = 5;
错误 p.age = 10;
*/
//名字长度超过限制,通过加入逻辑控制输入
p.setName("Longname");
System.out.println(p.getName());
//赋符合标准的名字
p.setName("Dady");
System.out.println("p的名字是:"+p.getName());
//年龄超出限制,不能小于0
p.setAge(-4);
System.out.println(p.getAge());
//赋正常年龄值
p.setAge(10);
System.out.println("p的年龄是:"+p.getAge());
}
}
The above simple example is to privatize attributes, hide what needs to be hidden, provide methods to access attributes, and display what needs to be displayed. This is one of the manifestations of encapsulation.
Link 2: Java encapsulation and access control (2)