当前位置:网站首页>Why use get/set instead of exposing properties
Why use get/set instead of exposing properties
2022-07-04 00:19:00 【Hello】
One 、 problem
Most fields are accessed through get/set Method to indirectly access , Why not set the field to public property directly public Well ? The answer lies in the future possibility of the former .
Two 、 Maintain control over this attribute
adopt get/set Disclose it and use public The main difference between attributes is Maintain control over this attribute . If you expose a field , That means you can access it directly . then , The caller can do anything , Whether intentionally or unintentionally . for example , You can set the field to null , If you use this field in another method , Then this method may be caused by Null pointer exception And collapse .
however , If provided get/set, It can provide indirect access while fully controlling . The only way to set values is through set, The value is obtained by get , Fields have only one entry and one exit . because get/set Is a way to allow code blocks , So you can verify them , Object determines whether to set the caller value . This also applies to get Method —— You can decide to return the actual reference or clone it , And return it to the caller .
therefore ,get/set It acts as a fuse or circuit breaker . The current must pass through the fuse , If something goes wrong , The fuse is separated from the main circuit , The circuit is safe . Again , If anything goes wrong ,set This value will not be passed to the class member field .
Generally, I don't write anything in get/set In the method . Only return and set fields , Just like public fields . By writing get/set, Created a clause for adding any validation methods in the future , There is currently no validation , But if anything goes wrong in the future , It's just set Add validation logic .
3、 ... and 、 The code is generic
When a field has no such validation constraint , Why bother writing get/set? The key to the problem is to avoid unnecessarily complicating the code . Developers are trying to make the code base more generic , Adapt to any change . Although most of the changes you think of will never come . therefore ,get/set Will not complicate the code , This will be verified in the code .
边栏推荐
- Gossip about redis source code 80
- Zipper table in data warehouse (compressed storage)
- 【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
- Ramble 72 of redis source code
- (Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
- Introducing Software Testing
- Stock price forecast
- D27:mode of sequence (maximum, translation)
- Entropy and full connection layer
- How to trade spot gold safely?
猜你喜欢
![[CSDN Q & A] experience and suggestions](/img/db/dff3173dda24ca5740729b54a81153.jpg)
[CSDN Q & A] experience and suggestions

SPI based on firmware library

ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
![[C language] break and continue in switch statement](/img/ae/5967fefcf3262c9d3096e5c7d644fd.jpg)
[C language] break and continue in switch statement

2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination

The interviewer's biggest lie to deceive you, bypassing three years of less struggle

Unity elementary case notes of angry birds Siki college 1-6

A method to solve Bert long text matching

Vscode regular match replace console log(.*)
![P3371 [template] single source shortest path (weakened version)](/img/c5/bcb22e1ea573253005e56ebbd73bb7.jpg)
P3371 [template] single source shortest path (weakened version)
随机推荐
The interviewer's biggest lie to deceive you, bypassing three years of less struggle
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
Selenium library 4.5.0 keyword explanation (II)
D30:color tunnels (color tunnels, translation)
Gossip about redis source code 77
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
BBS forum recommendation
【leetcode】374. Guess the size of the number
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Tencent interview: can you pour water?
炒股開戶傭金優惠怎麼才能獲得,網上開戶安全嗎
Pytorch learning notes 5: model creation
Ramble 72 of redis source code
After the Lunar New Year and a half
NLP pre training technology development
How to write a good title of 10w+?
What does redis do? Redis often practices grammar every day
[source code] VB6 chat robot
Briefly understand the operation mode of developing NFT platform