PHP中的类抽象和对象接口之间的区别?
PHP中的
Class Abstraction和
Object Interfaces有什么区别?我问,因为,我并没有真正看到他们两个人的意思,他们都做同样的事情!那么,使用两者之间的劣势有什么优势呢?
班级抽象: abstract class aClass { // Force extending class to define these methods abstract public function setVariable($name,$var); abstract public function getHtml($template); } 对象接口: interface iClass { // Force impementing class to define these methods public function setVariable($name,$var); public function getHtml($template); }
您可以实现多个接口,但只能扩展一个类.
可能: class MyClass extends MyAbstract implements MyInterface1,MyInterface2,MyInterface3 { } 不可能: class MyClass extends MyAbstract1,MyAbstract2 implements MyInterface { } 此外,接口不能实现. 可能: abstract class MyClass { function doSomething() { echo "I can give this method an implementation as this is an abstract class"; } } 不可能: interface MyClass { function doSomething() { echo "I can NOT give this method an implementation as this is an interface"; } } 从Java Glossary:
您还应该看一看this question – 达米恩关于界面如何成为合同的重要观点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |