Home  >  Article  >  Backend Development  >  Inheritance, polymorphism, interface, abstract class, abstract method example tutorial in PHP object-oriented

Inheritance, polymorphism, interface, abstract class, abstract method example tutorial in PHP object-oriented

伊谢尔伦
伊谢尔伦Original
2017-06-29 09:29:511144browse

This article mainly introduces the object-oriented features of the PHP introductory tutorial, and analyzes the inheritance, polymorphism, interface, ## involved in php object-oriented in combination with examples. #Abstract class and abstract methods, etc. Friends who need it can refer to the following

The examples in this article describe the object-oriented characteristics of PHP. Share it with everyone for your reference, the details are as follows:

Demo1.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  //创建一个电脑类
  class Computer {
    //什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外。
    //public 是对字段的公有化,这个字段类外即可访问,赋值和取值
    public $_name = &#39;联想&#39;;
  }
  $computer = new Computer();
  $computer -> _name = &#39;Dell&#39;;
  echo $computer->_name;
?>

Demo2.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    //private 是私有化,即对字段进行封装的操作,类外无法访问,取值和赋值都不能操作
    private $_name = &#39;联想&#39;;
  }
  $computer = new Computer();
  echo $computer->_name;
?>

Demo3.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    private $_name = &#39;联想&#39;;
    //这个时候我采用一个公共对外的方法来访问私有字段
    //因为私有字段只能在类内访问,而对外的公共方法是类内的。
    //更而公共方法又是公共的,所以类外又可访问。
    public function _run(){
      //字段在类内调用的时候必须是类 -> 字段,而$_name只是一个普通变量而已。
      //字段在类外调用的方法是对象 -> 字段,而类内就必须使用 Computer -> _name
      //但是在本类中,可以使用一个关键字来代替字来代替 Computer ,那就是 $this
      echo $this ->_name;
    }
  }
  $computer = new Computer();
  $computer -> _run();
?>

Demo4.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $name;
    private $model;
    private $cpu;
    private $keyboard;
    private $show;
    private $zb;
    //必须写个对外的入口,才可以取到
    public function getName() {
      return $this->name;
    }
    //必须写一个对内的入口,对私有字段进行赋值
    public function setName($name) {
      //这里的 $name 只是一个变量而已,参数而已
      //$this->name 才是类的字段
      $this->name = $name;
    }
  }
  $computer = new Computer ();
  echo $computer->getName();
  $computer->setName(&#39;Dell&#39;);
  echo $computer->getName();
?>

Demo5.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $_name;
    private $_model;
    private $_cpu;
    //当类外的对象直接调用私有字段时,会跟着去检查是否有拦截器,
    //如果直接对 $_name 进行赋值,那么set 方法就会拦截住,就不会报错了。
    //采用拦截器进行赋值和取值
    //赋值
    private function set($_key,$_value){
      //采用$_key = &#39;_name&#39;,那么 $_value = &#39;联想&#39;;
      //$this ->_name = &#39;联想&#39;;
      $this ->$_key = $_value;
    }
    //取值
    private function get($_key){
      return $this -> $_key;
      //如果 $_key = &#39;_name&#39; 那么 $this -> _name;
      //如果 $_key = &#39;_cpu&#39; 那么 $this -> _cpu;
      //如果 $_key = &#39;_model&#39; 那么 $this -> _model;
    }
  }
  $computer = new Computer ();
  $computer->_name = &#39;联想&#39;;
  $computer->_cpu = &#39;四核&#39;;
  $computer->_model = &#39;i7&#39;;
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
?>

Demo6.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $_name;
    private $_model;
    private $_cpu;
    //set 和 get 方法私有了,还是可以执行,是因为
    //因为目前程序的指针已经在类内了。而类内可以执行封装的方法
    //类内执行私有方法,不会出现任何错误。
    //它只需要间接的拦截就可以了。拦截是在内类执行的。
    //说白了,set() 和 get() 是 PHP 内置的方法,具有一定的特殊性
    private function set($_key, $_value) {
      $this->$_key = $_value;
    }
    private function get($_key) {
      return $this->$_key;
    }
  }
  $computer = new Computer ();
  $computer->_name = &#39;联想&#39;;
  $computer->_cpu = &#39;四核&#39;;
  $computer->_model = &#39;i7&#39;;
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
?>

Demo7.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    const NAME = &#39;DELL&#39;;
  }
  //常量的输出方法 类::常量
  echo Computer::NAME;    //DELL
?>

Demo8.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_count = 0;
    public function _add(){
      $this -> _count++;  //$_count = $_count+1 $_count++
    }
  }
  //做一个累计的效果
  $computer1 = new Computer();
  $computer1 ->_add();
  $computer1 ->_add();
  $computer1 ->_add();
  echo $computer1 -> _count;
  echo &#39;<br />&#39;;
  $computer2 = new Computer();
  $computer2 ->_add();
  $computer2 ->_add();
  $computer2 ->_add();
  echo $computer2 -> _count;
?>

Demo9.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public static $_count = 0;
    public function _add(){
      //如果是静态成员字段,那么就应该用 self 来调用,而不是 $this
      self::$_count++;
    }
  }
  //做一个累计的效果
  $computer1 = new Computer();
  $computer1 ->_add();
  echo Computer::$_count;
  $computer1 ->_add();
  echo Computer::$_count;
  $computer1 ->_add();
  echo Computer::$_count;
  echo &#39;<br />&#39;;
  $computer2 = new Computer();
  $computer2 ->_add();
  echo Computer::$_count;
  $computer2 ->_add();
  echo Computer::$_count;
  $computer2 ->_add();
  echo Computer::$_count;
?>

Demo10.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public static $_count = 0;
    public static function _add(){
      self::$_count++;
    }
  }
  Computer::_add();
  Computer::_add();
  Computer::_add();
  echo Computer::$_count;
?>

Demo11.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
  }
  $computer = new Computer();
  echo $computer instanceof Computer;
?>

Demo12.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //这是父类,电脑类
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      echo &#39;联想在运行!&#39;;
    }
  }
  //子类,笔记本电脑类
  class NoteComputer extends Computer {
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
?>

Demo13.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      echo &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    //我不需要父类的字段和方法,那么可以采用重写的方法覆盖掉父类的字段和方法
    public $_name = &#39;Dell&#39;;
    public function _run(){
      echo &#39;Dell在运行!&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
?>

Demo14.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    //私有化,但是无法被子类继承,这个时候就应该用受保护的修饰符来封装
    protected $_name = &#39;联想&#39;;
    protected function _run(){
      return &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    public function getTop() {
      echo $this->_name;
      echo $this->_run();
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> getTop();
?>

Demo15.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      return &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    //我子类已经覆盖了父类的字段和方法,
    //但是我又要调用父类的字段和方法,那怎么办呢?
    public $_name = &#39;Dell&#39;;
    public function _run(){
      echo &#39;Dell在运行!&#39;;
      echo parent :: _run();
    }
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
  //DellDell在运行!联想在运行!
?>

Demo16.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //final 如果加在类前面,表示这个类不能被继承
// final class Computer {
// }
  class Computer {
    //final 如果加在方法前面,表示不能够重写些方法
    final public function _run(){
    }
  }
  class NoteComputer extends Computer {
    public function _run(){
    }
  }
  $noteComputer = new NoteComputer();
?>

Demo17.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //创建一个抽象类,只要在 class 前面加上 abstract 就是抽象类了
  //抽象类不能够被实例化,就是创建对象
  //只在类里面有一个抽象方法,那么这个类必须是抽象类,类前面必须加上 abstract
  abstract class Computer {
    public $_name = &#39;联想&#39;;
    //抽象类里创建一个抽象方法
    //抽象方法不能够实现方法体的内容
    abstract public function _run();
    //我在抽象类里能否创建一个普通方法
    public function _run2(){
      echo &#39;我是父类的普通方法&#39;;
    }
  }
  //类不能够实现多继承,只支持单继承。
  //抽象类是给子类用来继承的,实现一种规范和资源的共享
  class NoteComputer extends Computer {
    //抽象类的抽象方法,子类必须重写,不然会报错。
    //抽象类里的普通方法不需要重写,子类会直接继承下来
    public function _run(){
      echo &#39;我是子类的方法&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> _run();
  $noteComputer -> _run2();
  echo $noteComputer -> _name;
?>

Demo18.php

<?php
  /*
   * 到底应该用抽象类还是接口呢
   * 如果你要继承多个接口的方法规范,那么就用接口好了。
   * 如果你要共享一个方法体内容,那么就用抽象类。
   * */
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //创建一个接口
  //接口也不能被实例化
  //接口是为了规范实现它的子类,以达到统一的目的。也可以共享数据
  interface Computer {
    //成员字段必须是变量
    const NAME = &#39;成员 &#39;;
    //接口里的所有方法都是抽象方法,不能够写方法体
    //并且接口的抽象方法不需要写 abstract
    public function _run();
    public function _run2();
  }
  interface Computer2 {
    public function _run3();
  }
  //子类继承接口的说法,叫做实现,接口可以多实现
  class NoteComputer implements Computer,Computer2 {
    public function _run() {
      echo &#39;我重写了run&#39;;
    }
    public function _run3() {
      echo &#39;我重写了run3&#39;;
    }
    public function _run2() {
      echo &#39;我重写了run2&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> _run();
  $noteComputer -> _run2();
  $noteComputer -> _run3();
  echo NoteComputer::NAME;
  //接口 :: 常量
  //echo Computer::NAME;
?>

Demo19.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //什么叫做多态,字面意思,多种形态
  //一个动作由不同的人去执行,而产生不同的效果或者效果,即为多态。
  //一个人通过不同的状态去执行同一种动作,形成不同的效果,也可以称作为多态。
  //园丁    剪    修理花草
  //理发师  剪    理发
  //总裁    剪    裁员
  //人   笔记本   运行 win7开机了
  //人   台式机   运行 xp开机了
  //创建一个接口,来规范运行的方法
  interface Computer {
    public function version(); //这个方法表示采用什么电脑
    public function work();   //这台电脑是怎么运行的
  }
  //创建一个笔记本的类实现接口
  class NoteComputer implements Computer {
    public function version() {
      echo &#39;笔记本&#39;;
    }
    public function work() {
      echo &#39;可以便携式运行 win7&#39;;
    }
  }
  //创建一个台式机的类实现接口
  class DesktopComputer implements Computer {
    public function version() {
      echo &#39;台式机&#39;;
    }
    public function work() {
      echo &#39;在工作站运行 XP&#39;;
    }
  }
  //创建一个用户
  class Person {
    //创建一个方法来接受电脑(笔记本电脑,也可以是台式电脑)
    //怎么接受,将他们的对象传进来就 OK 啦。
    public function _run($type) {
      echo &#39;这个人的&#39;;
      $type -> version();
      $type ->work();
    }
  }
  //多态的原理,就是类都写好了,不要去修改它,只要在类外的调用参数的更改
  //而最后的结果也会得到更改,那么这个就是多态。
  //有一个接口,两个类,一个是笔记本的类,一个是台式机的类
  //创建了笔记本
  $noteComputer = new NoteComputer();
  //创建台式机
  $desktopComputer = new DesktopComputer();
  //创建一个人
  $person = new Person();
  //使用电脑
  $person -> _run($noteComputer); //这种传递,叫做对象引用的传递
?>

The above is the detailed content of Inheritance, polymorphism, interface, abstract class, abstract method example tutorial in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn