当前位置:网站首页>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 .
边栏推荐
- How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
- How to make recv have a little temper?
- 网上的低佣金链接安全吗?招商证券怎么开户?
- Regular expressions and text processors for shell programming
- Idea a method for starting multiple instances of a service
- What does redis do? Redis often practices grammar every day
- [Mongodb] 2. Use mongodb --------- use compass
- Double efficiency. Six easy-to-use pychar plug-ins are recommended
- Enter MySQL in docker container by command under Linux
- P3371 [template] single source shortest path (weakened version)
猜你喜欢
Enter MySQL in docker container by command under Linux
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
[C language] break and continue in switch statement
[PHP basics] session basic knowledge, application case code and attack and defense
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
Vscode regular match replace console log(.*)
随机推荐
Analysis: misunderstanding of choosing WMS warehouse management system
Distributed transaction -- middleware of TCC -- selection / comparison
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
D29:post Office (post office, translation)
Stock price forecast
Selenium library 4.5.0 keyword explanation (4)
Idea integrates Microsoft TFs plug-in
Gossip about redis source code 73
Smart fan system based on stm32f407
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Gossip about redis source code 80
Several ways to set up a blog locally [attach relevant software download links]
Version rollback revert don't reset better reset must be forced
JDBC Technology
Pytorch learning notes 5: model creation
Recommendation of knowledge base management system
2020.2.14
Gossip about redis source code 83