找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 php教程 查看内容

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

作者:模板之家 2014-6-17 17:33 1106人关注

类的多态1多态的介绍和优势。2运算符:instanceof。3多态的简单应用。1多态的介绍和优势介绍:多态性是继承抽象和继承后,面向对象语言,PHP面向对象开发之类的多态详解

类的多态

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. if ($thing instanceof B) { 
  7. echo "B"

3.多态的简单应用

实例1代码如下:

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

实例2代码如下:

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

补充一个实例代码:

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

路过

雷人

握手

鲜花

鸡蛋
原作者: 互联网 来自: 网络收集

全部回复(0)