PHP中interface与 implements 关键字
类中接口的应用
1.关键字:interface
2.关键字:implements
1. 接口的介绍与创建
接口: 一种成员属性全部为抽象或常量的特殊抽象类。
规则:
1.类中全部为抽象方法。
2.抽象方法前不用加abstract。
3.接口抽象方法属性为public。
4.成员属性必须为常量。
格式代码如下:
1 2 3 4 5 6
| interface demo { const NAME = "常量对象属性"; function fun1(); function fun2(); }
|
2. 接口的应用与规范
接口引用区别于类继承关键字 extends,继承只能只是单一性,而接口可以使用关键字 implements 多个引用并用逗号分开。
1. 格式:普通类引用接口
1 2 3 4
| class MyPc implements demo , demo2 , demo3 { ... }
|
2. 格式:抽象类应用接口例子
1 2 3
| abstract class MyPc implements demo , demo2 , demo3 { ... }
|
3. 格式:继承父类引用接口并存
1 2 3 4
| class MyPc extends Root implements demo , demo2 , demo3 { ... }
|
先继承后接口,单继承多接口。
4. 格式:接口与接口的继承
1 2 3
| interface demo3 extends demo { ... }
|
实例一:例,接口使用关键字 interface 来定义,并使用关键字 implements 来实现接口中的方法,且必须完全实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?php interface demo { const NAME = "名称"; function fun1(); function fun2(); } interface demo2 { function fun3(); function fun4(); } interface demo3 { const TEST = "Test"; function fun5(); } class MyPc implements demo, demo2 { function fun1() { echo "++++++++++<br />"; } function fun2() { echo "----------<br />"; } function fun3() { echo "1111111111<br />"; } function fun4() { echo "2222222222<br />"; } } class MyPs extends MyPc implements demo3 { function fun5() { echo "继承类后引用接口"; } } $p = new MyPs; $p->fun1(); $p->fun2(); $p->fun3(); $p->fun4(); $p->fun5(); ?>
|
实例二:该例子演示了一个 PHP 接口的简单应用。该例子中,User 接口实现用户的折扣,而在 VipUser 类里面实现了具体的折扣系数。最后商品类 Goods 根据 User 接口来实现不同的用户报价。该例子仅限于演示 PHP 接口的用法,不涉及其科学与否。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?php
interface User { function getDiscount(); function getUserType(); }
class VipUser implements User { private $discount = 0.8; function getDiscount() { return $this->discount; } function getUserType() { return "VIP用户"; } } class Goods { var $price = 100; var $vc; function run(User $vc) { $this->vc = $vc; $discount = $this->vc->getDiscount(); $usertype = $this->vc->getUserType(); echo $usertype."商品价格:".$this->price*$discount; } } $display = new Goods(); $display->run(new VipUser);
?>
|
运行该例子,输出:VIP用户商品价格:80 元
实现多个接口
PHP也可以在继承一个类的时候同时实现多个接口:
1 2 3 4
| class 子类 extends 父类 implemtns 接口1, 接口2, ... { ...... }
|
抽象类和接口的区别
接口是特殊的抽象类,也可以看做是一个模型的规范。接口与抽象类大致区别如下:
1.一个子类如果 implements 一个接口,就必须实现接口中的所有方法(不管是否需要);如果是继承一个抽象类,只需要实现需要的方法即可。
2.如果一个接口中定义的方法名改变了,那么所有实现此接口的子类需要同步更新方法名;而抽象类中如果方法名改变了,其子类对应的方法名将不受影响,只是变成了一个新的方法而已(相对老的方法实现)。
3.抽象类只能单继承,当一个子类需要实现的功能需要继承自多个父类时,就必须使用接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| <?php
interface UserInterface { function getname(); } interface TeacherInterface { function getLengthofService(); } class User implements UserInterface { private $name = "nostop"; public function getName() { return $this->name; } } class Teacher implements TeacherInterface { private $age = 23; public function getLengthofService() { return $this->age; } } $user = new User(); echo $user->getName().'<br />';
$teacher = new Teacher(); echo $teacher->getLengthofService().'<br />';
class GraduResultudent extends User implements TeacherInterface { private $teacher; public function __construct() { $this->teacher = new Teacher(); } public function getLengthOfService() { return $this->teacher->getLengthOfService(); } } class Result { public static function getUserName(UserInterface $_user) { echo "Name is ".$_user->getName().'<br />'; } public static function getLengthOfService(TeacherInterface $_teacher) { echo "age is ".$_teacher->getLengthOfService(); } } $object = new GraduResultudent(); Result::getUserName($object);
Result::getLengthOfService($object);
echo "<br />";
interface People { function getUserType(); function getCount(); } class VipUser implements People { private $discount = 0.8; function getUserType() { return "VIP用户"; } function getCount() { return $this->discount; } } $vip = new VipUser();
echo $vip->getUserType().'商品价格:'.$vip->getCount()*100;
class Goods { var $price = 100; var $member; function run(People $member) { $this->member = $member; $discount = $this->member->getCount(); $usertype = $this->member->getUserType(); echo $usertype."商品价格:".$this->price*$discount; } } $display = new Goods(); $display->run(new VipUser);
?>
|
本文早期发布于个人 CSDN 博客:查看原文