Похожие чаты

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

public properties directly?

3 ответов

4 просмотра

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.

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

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

я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
в сях есть множество как в питоне? для удаление дубликатов
Linus
25
Yeh scammer hai kya ? 🙄 mujhre bhi solana invest ke liye bol re thi kal
Amit
17
If arrr gets implemented on cake wallet. Would that mean arrr/xmr swaps possible in cake wallet like atomic swaps on komodowallet ?
Keks Meister 🏴‍☠️
19
читать файл максимально быстро? странный вопрос))
zamtmn
53
Уважаемые. Вопрос. Есть ПС (рязань 5600Г+4060Ти). Запускаю игры, через усб карту захвата сигнал "забираю" на ОБС, установленной на Эйр м1. Всё это добро передается на твич, в ...
UglyKoyote
8
Hi. Is there something wrong in koala wallet or is it just my new phone? Balance is 0.00
Jani Ginman
12
30 BCH videos in Hausa Language successfully delivered. In December 2023, I created a Flipstarter to produce Bitcoin Cash videos in the Hausa language for greater outreach an...
Munnir
5
@AMP_Whale do we earn anvil inu for staking it for 4 years?
Drunken Barbie
12
Hello team of Kadena, every day 100 000 coins more on market, no news no partnership, how do you think that someone will notice Kadena with so small volume and on the 247th pl...
jeronim jurakovic
8
Карта сайта