最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

PHP面向对象开发之类的多态详解

时间:2014-06-09 16:40来源: 作者: 点击: 次
类的多态 1.多态的介绍和优势。 2.运算符:instanceof。 3.多态的简单应用。 1.多态的介绍和优势 介绍:多态性是继承抽象和继承后,面向对象语言的第三特征。 例子:USB接口,插上不同的东西

类的多态

1.多态的介绍和优势。

2.运算符:instanceof。

3.多态的简单应用。

1.多态的介绍和优势

介绍:多态性是继承抽象和继承后,面向对象语言的第三特征。

例子:USB接口,插上不同的东西会使用不同的功能。

优势:OOP并不仅仅是把很多函数和功能集合起来,目的而是使用类,继承,多态的方式描述我们生活中的一种情况。

2.运算符:instanceof

PHP一个类型运算符,用来测定一个给定的对象是否来自指定的对象

格式代码如下:

  1. class A {} 
  2. class B {} 
  3. $thing = new A; 
  4. if ($thing instanceof A) { 
  5. echo "A"; 
  6. } 
  7. if ($thing instanceof B) { 
  8. echo "B"; 
  9. } 

3.多态的简单应用

实例1代码如下:

  1. <?php 
  2. class A { 
  3. } 
  4. class B { 
  5. } 
  6. $new = new A; 
  7. if ($new instanceof A) { 
  8. echo "A"; 
  9. } 
  10. if ($new instanceof B) { 
  11. echo "B"; 
  12. } 
  13. ?> 

实例2代码如下:

  1. <?php 
  2. interface MyUsb { 
  3.  function type(); 
  4.  function alert(); 
  5. } 
  6. class Zip implements MyUsb { 
  7.  function type() { 
  8.   echo "2.0"; 
  9.  } 
  10.  function alert() { 
  11.   echo "U盘驱动正在检测……<br />"; 
  12.  } 
  13. } 
  14. class Mp3 implements MyUsb { 
  15.  function type() { 
  16.   echo "1.0"; 
  17.  } 
  18.  function alert() { 
  19.   echo "MP3驱动正在检测……"; 
  20.  } 
  21. } 
  22. class MyPc { 
  23.  function Add_Usb($what) { 
  24.   $what->type(); 
  25.   $what->alert(); 
  26.  } 
  27. } 
  28. $p = new MyPc(); 
  29. $zip = new Zip(); 
  30. $mp3 = new Mp3(); 
  31. $p->Add_Usb($zip); 
  32. $p->Add_Usb($mp3); 
  33. ?> 

补充一个实例代码:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  4. <title>继承和多态</title> 
  5. </head> 
  6. <body> 
  7. <?php 
  8. /*  父类  */ 
  9. class MyObject{ 
  10.  public $object_name;         //图书名称 
  11.  public $object_price;          //图书价格 
  12.  public $object_num;          //图书数量 
  13.  public $object_agio;          //图书折扣 
  14.  function __construct($name,$price,$num,$agio){    //构造函数 
  15.   $this -> object_name = $name; 
  16.   $this -> object_price = $price; 
  17.   $this -> object_num = $num; 
  18.   $this -> object_agio = $agio; 
  19.  } 
  20.  function showMe(){          //输出函数 
  21.   echo '这句话不会显示。'; 
  22.  } 
  23. } 
  24. /*  子类Book  */ 
  25. class Book extends MyObject{         //MyObject的子类。 
  26.  public $book_type;          //类别 
  27.  function __construct($type,$num){       //声明构造方法 
  28.   $this -> book_type = $type; 
  29.   $this -> object_num = $num; 
  30.  } 
  31.  function showMe(){          //重写父类中的showMe方法  
  32.   return '本次新进'.$this -> book_type.'图书'.$this->object_num.'本<br>'; 
  33.  } 
  34. } 
  35. /*  子类Elec  */ 
  36. class Elec extends MyObject{         //MyObject的另一个子类 
  37.  function showMe(){          //重写父类中的showMe方法 
  38.   return '热卖图书:'.$this -> object_name.'<br>原价:'.$this -> object_price.'<br>特价:'.$this -> object_price * $this -> object_agio; 
  39.  } 
  40. } 
  41. /*  实例化对象  */ 
  42. $c_book = new Book('计算机类',1000);       //声明一个Book子类对象 
  43. $h_elec = new Elec('PHP函数参考大全',98,3,0.8);     //声明一个Elec子类对象 
  44. echo $c_book->showMe()."<br>";        //输出Book子类的showMe()方法 
  45. echo $h_elec->showMe();         //输出Elec子类的是showMe()方法 
  46. ?> 
  47. </body> 
  48. </html> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容