nunia [个人文集]
加入时间: 2005/11/04 文章: 2184
经验值: 5079
|
|
|
作者:nunia 在 寒山小径 发贴, 来自 http://www.hjclub.org
This is strictly from text book, most enlightening under the new light:
Abstract Classes:
If a derived class does not implement all the methods of an interface, then it must be declared abstract.
abstract class Hole {
protected $plumage;
protected $migratory;
abstract public function __construct();
abstract public function fsck();
abstract public function sing();
abstract public function eat();
abstract public function setPlumage($plumage);
abstract public function getPlumage();
abstract public function setMigratory($migratory);
abstract public function getMigratory();
Private Methods Can't be Abstract
methods identified as abstract cannot be private; they must be either public or protected. The reason is that an abstract private method is a contradiction in terms. Because an abstract class has undefined methods it cannot be instantiated ( it only exists to be the parent of a derived class). A class with abstract private methods could never be implemented because private methods cannot be inherited. The same reasoning would apply to a final abstract methods.
Note: recall that a final method cannot be changed in a derived class. An abstract method cannot be final because it must be overridden - i.e., changed.
How does a pure abstract class, with no defined methods, differ from an interface? An interface may not have data members or a constructor.
Interface or Pure Abstract Class?
there are only syntactic differences between interfaces and pure abstract classes, but when should you use one rather than the other? In general, it's probably better to use an interface than a pure abstract class because of the flexibility of interfaces. PHP doesn't allow multiple inheritance for classes; a child class may have only one parent class. However, you can implement any number of interfaces.
It makes more sense to use abstract classes when there is a mix of concrete and abstract methods. You can provide an implementation where identical, derived class behavior is expected, and you can provide an abstract method where behavior will differ. You could, of course, ignore methods for which you expect the behavior of derived classes to diverge, but by declaring a method abstract you ensure that it will be implemented in any derived class.
作者:nunia 在 寒山小径 发贴, 来自 http://www.hjclub.org |
|
|