我们何时应考虑使用私有或受保护的? [英] When should we consider using private or protected?
本文介绍了我们何时应考虑使用私有或受保护的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
想知道,我们何时应该为模型中的某些方法实际使用 private
或 protected
? p>
有时我不会被我的方法归类为 private
或 protected
。我只是保持原样。但是我知道这一定是一个坏习惯,否则这两个分组将不会在编程中创建。
谢谢。
解决方案
- 如果您打算从外部调用方法,请
record.method()
,然后 public - 如果仅在内部使用,则
self.method()
,然后是 private - 如果打算在内部使用它,也要在子孙中使用它,请在子类中使用
self.method()#
,然后对其进行保护
Just wondering, when should we actually must use private
or protected
for some methods in the model?
Sometimes I can't not be bothered to group my methods in private
nor protected
. I just leave it as it is. But I know it must be a bad practice, otherwise these 2 groupings won't be created in programming.
Thanks.
解决方案
- If you plan to call a method externally,
record.method()
, then "public" - If it will be used only internally,
self.method()
, then "private" - If you plan to use it internally, but also in descendants,
self.method() # in subclass
, then "protected"
这篇关于我们何时应考虑使用私有或受保护的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文