海纳百川

登录 | 登录并检查站内短信 | 个人设置 网站首页 |  论坛首页 |  博客 |  搜索 |  收藏夹 |  帮助 |  团队  | 注册  | RSS
主题: this kind of study is the holy way...
回复主题   printer-friendly view    海纳百川首页 -> 寒山小径
阅读上一个主题 :: 阅读下一个主题  
作者 this kind of study is the holy way...   
所跟贴 Don't get it. -- 秃公 - (261 Byte) 2007-2-19 周一, 上午12:00 (183 reads)
nunia
[个人文集]






加入时间: 2005/11/04
文章: 2184

经验值: 5079


文章标题: Object Oriented PHP (220 reads)      时间: 2007-2-19 周一, 上午5:26

作者: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
  • 返回顶端
    阅读会员资料 nunia离线  发送站内短信 浏览发表者的主页
    显示文章:     
    回复主题   printer-friendly view    海纳百川首页 -> 寒山小径 所有的时间均为 北京时间


     
    论坛转跳:   
    不能在本论坛发表新主题
    不能在本论坛回复主题
    不能在本论坛编辑自己的文章
    不能在本论坛删除自己的文章
    不能在本论坛发表投票
    不能在这个论坛添加附件
    不能在这个论坛下载文件


    based on phpbb, All rights reserved.
    [ Page generation time: 0.547094 seconds ] :: [ 23 queries excuted ] :: [ GZIP compression enabled ]