最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在PHP中implement什么意思,php 接口,extends,implement,implements 作用及区别收集整理

运维笔记admin23浏览0评论

在PHP中implement什么意思,php 接口,extends,implement,implements 作用及区别收集整理

在PHP中implement什么意思,php 接口,extends,implement,implements 作用及区别收集整理

标签:

extends 是继承某个类 ,继承之后可以使用父类的方法 ,也可以重写父类的方法,继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承

implements 是实现多个接口,接口的方法一般为空的,必须重写才能使用,可以有效的对,实现的类 方法名,及参数进行约束。可以另类的实现多继承。

class A extends B implements C,D,E{}  class中实现方法体。一个interface可以extends多个其他interface。

interface abc{

const bbq = 321;

public function a1(show $acc);

}

class test implements abc{

public function a1(show $acc){  //必须函数 and 参数

return 123;

}

}

$n = new abc();

echo $n->a1();

interface Comparable {

function compare(self $compare);

}

class String implements Comparable {

private $string;

function __construct($string) {

$this->string = $string;

}

function compare(self $compare) {

if($this->string == $compare->string){

return $this->string."==".$compare->string."
";

}else{

return $this->string."!=".$compare->string."
";

}

}

}

class Integer implements Comparable {

private $integer;

function __construct($int) {

$this->integer = $int;

}

function compare(self $compare) {

if($this->integer == $compare->integer){

return $this->integer."==".$compare->integer."
";

}else{

return $this->integer."!=".$compare->integer."
";

}

}

}

$first_int = new Integer(3);

$second_int = new Integer(4);

$first_string = new String("foo");

$second_string = new String("bar");

echo $first_int->compare($second_int);              // 3!=4

echo $first_int->compare($first_int);               // 3==3

echo $first_string->compare($second_string);        // foo!=bar

echo $first_string->compare($second_int);           // 严重错误

implement 用来拓展方法使用。

//扩展新的Function

var a=function(){};

var b=function(){};

Function.implement({

alert:function(msg){//直接alert输出内容

alert(msg);

},

output:function(msg){//firebug的控制台会输出内容,IE会报错

console.log(msg);

}

});

a.alert(‘1‘);

a.output(‘2‘);

b.output(‘3‘);

标签:

发布评论

评论列表(0)

  1. 暂无评论