当前位置:网站首页>Package details_ Four access control characters_ Two details of protected
Package details_ Four access control characters_ Two details of protected
2022-07-04 03:19:00 【hv102】
Encapsulation is one of the three characteristics of object-oriented .
The concept of encapsulation :
High cohesion , Low coupling .
The function and meaning of encapsulation :
Our programming should pursue ,“ High cohesion , Low coupling ”. High cohesion means that the internal data operation details of a class are completed by themselves , External interference is not allowed , The second coupling is to expose only a small number of methods to external use , Try to facilitate external calls .
4, The specific advantages of being encapsulated in :
Improve the security of the code
Improve code reusability
“ High cohesion ”: Package details , Easy to modify internal code , Improve maintainability .
“ Low coupling ”: Simplify external calls , Easy for callers to use , Easy to expand and collaborate .
The implementation of encapsulation ---- Use access control
Java It's using “ Access control character ” To control which details need to be encapsulated , What needs to be exposed .
Java in 4 Kind of “ Access control character ” Namely :pritave( Private )\default( Illegal )\protected( The protected )\public( Public ).

【 notes 】 About protected Two details of :
1. If parent and child are in same package , Subclasses can access the parent class protected member , You can also access the of the parent object
protected member
2. If subclass and parent are not in same package , Subclasses can access the parent class protected member , Cannot access parent object
Of protected member
Simple rules encapsulated in development :
- Properties are generally used private Access right .
- After the property is private , Provide corresponding get/set Method to access related properties , These methods are usually public Embellished , To provide assignment and read operations on properties ( Be careful : boolean Variable get The method is is start !)
- Method : Some auxiliary methods only used in this class can be used private modification , I hope the methods called by other classes use public modification .
【 Example 】 JavaBean Packaging demonstration of
public class Person
∥ Properties are generally used private modification
private String name;
private int age;
private boolean flag;
∥ Provide... For properties public Embellished set/get Method
public String getName((
return name;
public void setName(String name){
this.name name;
public int getAge0
return age;边栏推荐
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
- Leetcode51.n queen
- Teach you how to optimize SQL
- 機器學習基礎:用 Lasso 做特征選擇
- Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
- Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Eh, the log time of MySQL server is less than 8h?
- Global and Chinese markets for electroencephalogram (EEG) devices 2022-2028: Research Report on technology, participants, trends, market size and share
- 基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
猜你喜欢

Dare to climb here, you're not far from prison, reptile reverse actual combat case
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/94/2bdc31ec05595dbbc8a7a8d6b22252.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

WP collection plug-in free WordPress collection hang up plug-in

Talking about custom conditions and handling errors in MySQL Foundation

1day vulnerability pushback skills practice (3)

Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
![[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love

Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

Unity controls the selection of the previous and next characters
随机推荐
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
static hostname; transient hostname; pretty hostname
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Record a problem that soft deletion fails due to warehouse level error
Love and self-discipline and strive to live a core life
Setting methods, usage methods and common usage scenarios of environment variables in postman
Talking about custom conditions and handling errors in MySQL Foundation
Short math guide for latex by Michael downs
@Scheduled scheduled tasks
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
Baijia forum the founding of the Eastern Han Dynasty
Redis transaction
Recent learning fragmentation (14)
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
MySQL query
Have you entered the workplace since the first 00???
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
What are the conditions for the opening of Tiktok live broadcast preview?