问题描述
是否有很好的理由,没有很好的理由来解决使用面向对象语言的getter和setter的所有麻烦?仅使用直接引用属性或方法有什么问题?人们不想在礼貌的公司中谈论某种语义掩盖"吗?当有人走出去说你应该写大量的代码来获取吸气剂和二传手"时,我是否太累了,睡着了?
一年后的跟进:
在Java中这似乎很常见,而在Python中则少见. 我开始怀疑这是否更像是一种文化现象(与语言的局限性有关),而不是明智的建议".由于我不使用Java编程(目前是通过选择),因此无法进行评估.
就我而言,当前(当前截至2010年3月22日)-1问题得分是完整的.有趣的是,有些特定的问题被否决了,不是因为它们是坏问题",而是因为它们打了某人的原始神经.
因此,让我们开始讨论这件事.我重复一遍:
What's wrong with just using a direct reference to a property or method?
这是未成文的推论:
Are we so undisciplined as programmers that we can't keep our hands off of things that are clearly marked "no touchy"?
这里是Allen Holub(他是出色的人)此问题.他在 Holub on Patterns .有些事情需要公共获取者和设置者,例如序列化和模式,例如数据传输对象模式.总的来说,我认为这是必要的邪恶,因为当您不使用它们时,您的应用程序就会变得混乱.
Is there a good, no, a very good reason, to go through all the trouble of using getters and setters for object-oriented languages? What's wrong with just using a direct reference to a property or method? Is there some kind of "semantical coverup" that people don't want to talk about in polite company? Was I just too tired and fell asleep when someone walked out and said "Thou Shalt Write Copious Amounts of Code to Obtain Getters and Setters"?
Follow-up after a year:
It seems to be a common occurrence with Java, less so with Python. I'm beginning to wonder if this is more of a cultural phenomena (related to the limitations of the language) rather than "sage advice". As I do not program in Java (currently by choice) I cannot make that assessment.
The current (current being as of this writing 2010-03-22) -1 question score is complete for-the-lulz as far as I am concerned. It's interesting that there are specific questions that are downvoted, not because they are "bad questions", but rather, because they hit someone's raw nerve.
So let's get to the nut of the matter. I repeat myself:
What's wrong with just using a direct reference to a property or method?
And here's the unwritten corollary:
Are we so undisciplined as programmers that we can't keep our hands off of things that are clearly marked "no touchy"?
Here is Allen Holub (who is brilliant) on the matter. He goes into much more detail on this subject in Holub on Patterns. Some things require public getters and setters like serialization and patterns like the Data Transfer Object pattern. In general, I think necessary evil since your application becomes convoluted when you don't use them.
这篇关于吸气剂和吸气剂:代码异味,必不可少的恶魔,或者没有它们就无法生存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!