[英]Best Practice: Protected or Private Methods by Default and Test-Driven Development
Many people agree that protected methods should be used only when you have a reason to use them.许多人同意只有当你有理由使用受保护的方法时才应该使用它们。 How does a test-driven development model way into this?测试驱动开发 model 如何进入这个? (Especially with regard to faking objects.) I have a friend who is a big fan of TDD and now BDD and is a C# developer, and he told me he hardly ever uses the private
keyword. (特别是关于伪造对象。)我有一个朋友是 TDD 的忠实粉丝,现在是 BDD,他是一名 C# 开发人员,他告诉我他几乎不使用private
关键字。 After he said that, I kept using it for fields but starting defaulting all of my methods to protected
.在他这么说之后,我继续将它用于字段,但开始将我的所有方法默认为protected
。 Some people on StackOverflow also agree that protected
should be used by default—could some of you please weigh in on this question? StackOverflow 上的一些人也同意默认情况下应该使用protected
—— 你们中的一些人可以权衡一下这个问题吗? What is the best reason to use protected
by default (since the threads above explain reasons not to)?默认情况下使用protected
的最佳理由是什么(因为上面的线程解释了不这样做的原因)?
Edit : per Oded's comment, what about using protected
by default and the Open-Closed Principle (a class should be open for extension and closed for modification)?编辑:根据 Oded 的评论,默认情况下使用protected
和开闭原则(class 应该对扩展开放和对修改关闭)怎么样?
Here's what I consider a best practice, do with my developments and suggest to all of my clients:以下是我认为的最佳实践,与我的发展一起做,并向我的所有客户提出建议:
This should pretty much cover all of your cases.这应该几乎涵盖了您的所有情况。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.