Похожие чаты

Hello Guys, as a college student who was drilled to use

encapsulation in every class where variables are set/reset

What do you think of it? Personally I see little reason in hiding fields behind getter/setter methods if no additional checks are in place.

Am I missing something? When coding alone what benefit do I get from hiding fields behind set methods?

5 ответов

12 просмотров

You are right, there is no real benefit of get and set if you just want to have a public field. But in case you have a code associated with get or set, like if you get something and you want your class to go and get something from internet or database it is worth it... Or if you set something and your class will change color of something that is where get / set works good, otherwise use public field... it is as good.

Christoph- Автор вопроса
Talula
You are right, there is no real benefit of get and...

At my university it was solely used to change private fields, with no checks happening whatsoever Thanks for the heads up :)

You can also think of scenario where you want to increment or multiply value of variable when you set it or get it. Most of unit conversion and all related things.

If you decide to add those checks later to a public field you’ll need to recompile all of its clients and if it already had a getter/setter you only need to recompile the library itself

Christoph- Автор вопроса

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

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

Какой-то там пердун в 90-х решил, что есть какая-то разная типизация. Кого вообще это волнует?
КТ315
49
Hi. Do we have a raid bot? Why nobody doing raids on X? Even RH mentioned this and nobody paying attention...whats the channel for hex memes? If mods cant run raids just insta...
H
31
Подскажите, а есть vault lite или ченить такое?) А то нужен вольт для похода в вольт, но весит он ~500 мб) как-то многовато для парочки запросов ))
Alexandr Orloff
17
блеать, почему так?? где в роутере это исправляется?
Арсен Маньяков 🇦🇲
16
void terminal_scroll() { memmove(terminal_buffer, terminal_buffer + VGA_WIDTH, buffer_size - VGA_WIDTH); memset(terminal_buffer + buffer_size - VGA_WIDTH, 0, VGA_WIDTH); ...
Егор
47
🌊 Ocean Nodes Dashboard Update 🚀 Hey, Oceaners! First off, a massive round of applause 👏to all of you for the amazing engagement since we launched Ocean Nodes. In just a few ...
KreigDK | Never DM first🌊
3
Всем привет! Подскажите, пожалуйста, в чем ошибка? Настраиваю подключение к MySQL. Либы лежат рядом с exe. Все как по "учебнику"
Евгений
16
А можете как-то проверить меня по знаниям по ассемблеру?
A A
132
Здравствуйте! У меня появилась возможность купить книгу "Изучай Haskell во имя добра!". Но я где-то слышал, что эта книга устарела. Насколько это правда??
E
22
люди, которые используют flameshot, к вам вопрос. Можно-ли поставить хоткей на создание скриншота? В программе есть отдел "горячие клавиши", но там все для редактирования, скр...
ThunDer104
11
Карта сайта