php (十三) 物件導向 封裝

來源:互聯網
上載者:User
物件導向的封裝性:1,就是把對象的成員(屬性,方法)結合成一個獨立的相同單位,並儘可能隱藏對象的內部細節public protectedprivate 私人的,用這個關鍵字修飾的成員,只能在對象內部訪問(只有用$this訪問),不能在對象外部使用 樣本: 
class Person{  private $name;  private $age;  private $sex;  function __construct($name="",$age=20,$sex="male"){  $this->name=$name;  $this->age=$age;  $this->sex=$sex;  }  function getPro($name){  return $this->$name;  }  function setAge($age){  if($age>100$age<0){  return;  }  $this->age=$age;  }  function getAge(){  if($this->age<30){  return $this->age;  }elseif($this->age<40){  return $this->age-5;  }elseif($this->age<50){  return $this->age-10;  }else{  return $this->age-15;  }  }  function say(){  echo "我的名字是:".$this->name.",年齡是:".$this->age.",性別是:".$this->sex.'<br>';  }  function __destruct(){  echo $this->name.",再見"."<br>";  }  }  $p1=new Person("rayhooo",26,"male");  $p1->say();  echo $p1->getPro("name").'<br>';  $p1->setAge(45);  echo $p1->getAge().'<br>';  
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.