公共,私有和受保護的有什麼區別? - What is the difference between public, private, and protected?

問題:

When and why should I use public , private , and protected functions and variables inside a class? 何時以及爲何應該在類內使用publicprivateprotected函數和變量? What is the difference between them? 它們之間有什麼區別? ide

Examples: 例子: 函數

// Public
public $variable;
public function doSomething() {
  // ...
}

// Private
private $variable;
private function doSomething() {
  // ...
}

// Protected
protected $variable;
protected function doSomething() {
  // ...
}

解決方案:

參考一: https://stackoom.com/question/IIdd/公共-私有和受保護的有什麼區別
參考二: https://oldbug.net/q/IIdd/What-is-the-difference-between-public-private-and-protected
相關文章
相關標籤/搜索