PHP中類與對象的深刻思考

來源:互聯網
上載者:User

標籤:des   c   style   class   a   ext   

PHP通過class關鍵字加類名來定義一個類的格式如:

<?php

  class lei{

  //......

}

?>    //上述lei就是一個最簡單的類

再來一個概念,類中的成員函數又稱為什麼呢?答:成員方法。成員方法就是在類中實現某個功能的函數,僅僅在這個類中實現~~

例如<?php

  class sport{

  function lanqiu()

  {

    //..........

  }

}

?>

上述類sport中定義的函數lanqiu()就是作用在類sport中的成員方法,你可以寫一些函數實現的功能。

那什麼是類的執行個體化?上述類已經定義了sport   類的成員方法也已經添加,假設這個成員方法的功能已經實現,那我們該如何使用這個類呢,我認為使用這個類的方法就是類的執行個體化,你不執行個體化你定義個類幹啥啊==

繼續,執行個體化是通過關鍵字new來聲明一個對象,然後使用如下格式:“對象名->成員方法”

看個執行個體,借鑒自(潘凱華.php從入門到精通)

<?php

  class SportObject

  {

    function beatBasketball($name,$height,$avoirdupois,$age,$sex)

    {

      if($height>180 and $avoirdupois<=100)

      {

        return $name.",符合打籃球的要求!";

      }

      else

      {

         return $name.",不符合打籃球的要求!";

      }

    }

  }

  $sport = new SportObject();    //這裡sport就是一個執行個體化後的對象

  echo $sport->beatBasketball(‘明日‘,‘185‘,‘80‘,‘20周歲‘,‘男‘);  //調用成員方法

?>

 

結果為:明日,符合打籃球的要求!

 

那麼什麼是成員變數?就是類中的變數 定義類成員變數的格式   關鍵字   成員變數名

訪問成員變數和訪問成員方法一樣   對象名—>成員變數

再來個執行個體

 

<?php
class SportObject
{
public $name;//定義成員變數
public $height;
public $avoirdupois;
public function bootFootBall($name,$height,$avoirdupois)  //定義成員方法
{
$this->name=$name;    //$this->作用是調用本類中的成員變數或成員方法
$this->height=$height;  //這裡注意,$this->後面的變數是沒有$符號的
$this->avoirdupois=$avoirdupois;
if($this->height<185 and $this->avoirdupois<85)    //這裡$this->height也可寫成$height
{
return $this->name.",符合踢足球的要求";
}
else
{
return $this->name.",不符合踢足球的要求";
}
}
}
$sport = new SportObject();
echo $sport->bootFootBall(‘明日‘,‘185‘,‘80‘);
?>

 接下來,類常量,定義類常量的關鍵字 const    例如  const PI=3.14159;

 

再來個例子

<?php
class SportObject
{
const BOOK_TYPE=‘電腦圖書‘;    //聲明常量,常量名前沒有$符號
public $object_name;
function setObjectName($name)
{
$this->object_name = $name;
}
function getObjectName()
{
return $this->object_name;
}
}


$c_book = new SportObject();
$c_book -> setObjectName("PHP類");
echo SportObject::BOOK_TYPE."->";  //類名和常量名之間的兩個冒號稱為範圍操作符

//使用這個操作符可以在不建立對象的情況下調用類的常量、變數和方法。
echo $c_book -> getObjectName();

?>

 

 

當執行個體化一個對象後,經常要初始化這個對象,即對這個對象賦初始值,這時候可以採用構造方法,構造方法就是產生對象時自動執行的成員方法(它就是一個成員方法,類中的函數,只是最先運行罷了),作用就是初始化對象。

執行個體:

<?php

class SportObject //類名

{

public $name; //成員變數

public $height;

public $avoirdupois;

public $age;

public $sex;

public function __construct($name,$height,$avoirdupois,$age,$sex) //建構函式

{

$this->name=$name; //為成員變數賦初值

$this->height=$height;

$this->avoirdupois=$avoirdupois;

$this->age=$age;

$this->sex=$sex;

}

 

public function boot() //成員方式

{

if($this->height<185 and $this->avoirdupois<85)

{

return $this->name.",符合踢足球的要求!";

}

else

{

return $this->name.",不符合踢足球的要求!";

}

}

}

$sport = new SportObject(‘明日‘,‘185‘,‘80‘,‘20‘,‘男‘); //一句話就初始化完了執行個體sport

echo $sport->boot();

?>

 

解構函式:與建構函式作用相反,是在最後才執行的成員方式。

執行個體:

<?php

class SportObject

{

public $name;

public $height;

public $avoirdupois;

public $age;

public $sex;

public function __construct($name,$height,$avoirdupois,$age,$sex)

{

$this->name=$name;

$this->height=$height;

$this->avoirdupois=$avoirdupois;

$this->age=$age;

$this->sex=$sex;

}

 

public function boot()

{

if($this->height<185 and $this->avoirdupois<85)

{

return $this->name.",符合踢足球的要求!";

}

else

{

return $this->name.",不符合踢足球的要求!";

}

}

 

function __destruct()

{

echo "<p><b>對象被銷毀,調用了解構函式。</b></p>";

}

}

$sport = new SportObject(‘明日‘,‘185‘,‘80‘,‘20‘,‘男‘);

echo $sport->boot();

?>

最後輸出:

明日,不符合踢足球的要求!

對象被銷毀,調用了解構函式。

 

 

繼承執行個體:

<?php
class SportObject
{
public $name;
public $age;
public $avoirdupois;
public $sex;
public function __contruct($name,$age,$avoirdupois,$sex)
{
$this->name=$name;
$this->age=$age;
$this->avoirdupois=$avoirdupois;
$this->sex=$sex;
}

function show()
{
echo ‘這句話不會顯示‘;
}
}

class BeatBasketBall extends SportObject
{
public $height;
function __construct($name,$height)
{
$this->name=$name;
$this->height=$height;
}

function show()
{
if($this->height>185)
{
return $this->name.",符合打籃球的要求!";
}
else
{
return $this->name.",不符合打籃球的要求";
}
}
}

class WeightLifting extends SportObject
{
function show()
{
if($this->avoirdupois<85)
{
return $this->name.",符合舉重的要求";
}
else
{
return $this->name.",不符合舉重的要求";
}
}
}

$beatbasketball = new BeatBasketBall("科丹",‘190‘);
$weightlifting = new WeightLifting("明日",‘185‘,‘80‘,‘20‘,‘男‘);
echo $beatbasketball->show()."<br>";
echo $weightlifting->show()."<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.