公共,私人和受保护的有什么区别? [英] What is the difference between public, private, and protected?
本文介绍了公共,私人和受保护的有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
何时以及为何在类内使用public
,private
和protected
函数和变量?它们之间有什么区别?
When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?
示例:
// Public
public $variable;
public function doSomething() {
// ...
}
// Private
private $variable;
private function doSomething() {
// ...
}
// Protected
protected $variable;
protected function doSomething() {
// ...
}
推荐答案
您使用:
-
public
范围使该属性/方法可从任何位置,对象的其他类和实例使用.
public
scope to make that property/method available from anywhere, other classes and instances of the object.
private
范围.
protected
范围.
更多 :(有关全面信息)
More: (For comprehensive information)
这篇关于公共,私人和受保护的有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文