当前位置:网站首页>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 .
边栏推荐
- Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
- P1339 [USACO09OCT]Heat Wave G
- Distributed transaction -- middleware of TCC -- selection / comparison
- Smart fan system based on stm32f407
- [C language] break and continue in switch statement
- BBS forum recommendation
- Gossip about redis source code 78
- URL (data:image/png; Base64, ivborw0k... Use case
- I wrote a chat software with timeout connect function
- IO flow principle and classification
猜你喜欢
JDBC Technology
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
[C language] break and continue in switch statement
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
Qtcharts notes (V) scatter diagram qscatterseries
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
URL (data:image/png; Base64, ivborw0k... Use case
The interviewer's biggest lie to deceive you, bypassing three years of less struggle
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
随机推荐
JDBC Technology
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
2/14 (regular expression, sed streaming editor)
No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
Distributed transaction -- middleware of TCC -- selection / comparison
How to make icons easily
2022 t elevator repair registration examination and the latest analysis of T elevator repair
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
2022 system integration project management engineer examination knowledge points: software development model
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
What is the Valentine's Day gift given by the operator to the product?
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
Alibaba cloud container service differentiation SLO hybrid technology practice
炒股開戶傭金優惠怎麼才能獲得,網上開戶安全嗎
D26: the nearest number (translation + solution)
P1339 [USACO09OCT]Heat Wave G
How to trade spot gold safely?
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
AI Challenger 2018 text mining competition related solutions and code summary
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination