admin管理员组文章数量:1794759
在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‘);
标签:
本文标签: 在PHP中implement什么意思php 接口extendsimplementimplements 作用及区别收集整理
版权声明:本文标题:在PHP中implement什么意思,php 接口,extends,implement,implements 作用及区别收集整理 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1706315789a523227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论