当前位置:网站首页>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 .
边栏推荐
- D28:maximum sum (maximum sum, translation)
- SPI based on firmware library
- I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
- CSP window
- Gossip about redis source code 82
- Powerful blog summary
- Double efficiency. Six easy-to-use pychar plug-ins are recommended
- Distributed transaction -- middleware of TCC -- selection / comparison
- 想请教一下,十大劵商如何开户?在线开户是安全么?
- Solve the problem that the kaggle account registration does not display the verification code
猜你喜欢

EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
![[CSDN Q & A] experience and suggestions](/img/db/dff3173dda24ca5740729b54a81153.jpg)
[CSDN Q & A] experience and suggestions

leetcode-43. String multiplication

Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

Correlation analysis summary

2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination

STM32 key light

2/14 (regular expression, sed streaming editor)

2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
随机推荐
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Unity elementary case notes of angry birds Siki college 1-6
A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Idea set class header comments
2/14 (regular expression, sed streaming editor)
Tencent interview: can you find the number of 1 in binary?
Gossip about redis source code 76
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Axure resources and prototype tool Axure RP 9 download
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Idea a method for starting multiple instances of a service
Advanced C language - pointer 2 - knowledge points sorting
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
JDBC Technology
Data storage - interview questions
Is user authentication really simple
Zipper table in data warehouse (compressed storage)