Php/keyword implements

来自菜鸟教程
跳转至:导航、​搜索

PHP实现关键字

Keywords PHP关键字

实现一个接口:

<?php

  interface Machine {
  public function activate();
  public 
  function deactivate();
  public function isActive();
}

class Kettle 
  implements Machine {
  private $isOn = false;

  public 
  function activate() {
    $this->isOn = true;
  }


  public function deactivate() {
    $this->isOn = 
  false;
  }

  public function isActive() {
    
  return $this->isOn;
  }
}

$machine = new Kettle();


  $machine->activate();
if($machine->isActive()) {
  echo "The 
  machine is on";
} else {
  echo "The machine is off";
}

echo "<br>";

  $machine->deactivate();
if($machine->isActive()) {
  echo "The 
  machine is on";
} else {
  echo "The machine is off";
}

?>

定义和用法

The implements 关键字用于声明类必须具有指定接口中描述的方法。这称为多态。多态性使得以相同的方式使用各种不同的对象变得容易。

相关页面

The interface 关键词

The else 关键词

在我们的网站中阅读有关对象,类和接口的更多信息 PHP OOP教程

.

Keywords PHP关键字