当前位置:网站首页>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 .
边栏推荐
- Iclr2022: how does AI recognize "things I haven't seen"?
- Analysis: misunderstanding of choosing WMS warehouse management system
- 2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
- [source code] VB6 chat robot
- Gossip about redis source code 77
- What are the application fields of digital twins in industry?
- Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
- Social network analysis -social network analysis
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- Recommendation of knowledge base management system
猜你喜欢

URL (data:image/png; Base64, ivborw0k... Use case

How will the complete NFT platform work in 2022? How about its core functions and online time?
![[PHP basics] cookie basics, application case code and attack and defense](/img/7c/551b79fd5dd8a411de85c800c3a034.jpg)
[PHP basics] cookie basics, application case code and attack and defense

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
![[Mongodb] 2. Use mongodb --------- use compass](/img/d5/0eb7dd4c407fbf2e9ba1b175f5424d.jpg)
[Mongodb] 2. Use mongodb --------- use compass
![Docking Alipay process [pay in person, QR code Payment]](/img/30/665580241020ee3adb872e725f0624.jpg)
Docking Alipay process [pay in person, QR code Payment]
![[C language] break and continue in switch statement](/img/ae/5967fefcf3262c9d3096e5c7d644fd.jpg)
[C language] break and continue in switch statement

Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival

How to write a good title of 10w+?

MySQL is installed as a Windows Service
随机推荐
Iclr2022: how does AI recognize "things I haven't seen"?
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
How to make icons easily
1214 print diamond
Idea set class header comments
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Comment obtenir une commission préférentielle pour l'ouverture d'un compte en bourse? Est - ce que l'ouverture d'un compte en ligne est sécurisée?
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Make small tip
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
Test the influence of influent swacth on the electromagnetic coil of quartz meter
Gossip about redis source code 78
Zipper table in data warehouse (compressed storage)
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Regular expressions and text processors for shell programming
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
Social network analysis -social network analysis
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Distributed transaction -- middleware of TCC -- selection / comparison