Похожие чаты

@Dburyak, why to use the getters/setters pattern instead of using

public properties directly?

3 ответов

20 просмотров

This is well explained in tutorials. The whole reason why we have private fields and make them accessible via setters and getters.

626d
This is well explained in tutorials. The whole rea...

I hope you get what I mean. I kinda lack the words to explaij it in shortened form.

Public *fields*. They are called "public fields". This distinction in terminology is important. Properties is a different concept, they are not fields. And getters/setters is not a pattern. It's an OOP concept. There is tons of info about it on the internet. This is a fundamental mechanism and very well explained. Try searching something like "why to use setters and getters". Here's how I explain it to people. In OOP, actually there's no concept of "public fields". There are "properties". They describe ... properties of the object)) I mean in non-technical sense - qualities of the object. And fields are nothing but one of the mechanisms to implement properties. You can implement properties without using fields at all, or you can implement one property using multiple fields. I.e. there is no restriction how exactly you implement properties. Properties are accessed with accessor methods - getters and setters. It just happens that in majority of cases properties are implemented with a single backing field of the same type. That's the reason, why in java this field should be private - because it's an IMPLEMENTATION DETAILS of the property. So, all this hassle with fields vs getters/setters is nothing but a result of java's stupid initial design for properties. There are languages much better designed with regards to properties. For example, in dart there are no fields at all, only properties. This is a well-designed OOP. Here's a sample class Something { String rwProp; final String roProp; String get evaluatedRoProp { return "calculated string value"; } set writeOnlyProp(String newVal) { thow Exception("not implemented"); } String get anotherRwProp() { ... } set anotherRwProp(String newVal) { ... } void usage() { var obj = new Something("ro prop value"); obj.rwProp = "new str value"; var roProp = obj.roProp; roProp = obj.evaluatedRoProp; obj.writeOnlyProp = "yet another"; obj.anotherRwProp = "again another"; var rwVal = obj.anotherRwProp; } If after searching on the internet you are not sure why to use getters/setters in java instead of simple public fields, then my recommendation - just use public fields. If you are asking this question, then you simply have no experience yet, and you're asking a question about something you never tried. Just try it. You'll run into problems yourself, not just read about it from other people, and will understand better why getters/setters are important.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта