七行笔记

导航

php面向对象(OOP)编程完全教程:14.final关键字的应用

这个关键字只能用来定义和定义方法, 不能使用final这个关键字来定义成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数,所以不能使用final来定义成员属性。

使用final关键标记的类不能被继承;

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->final class Person
{
     function say()
    {

    }
}

class Student extends Person
{
    function say()
    {

    } 

}

会出现下面错误:

 

Fatal error: Class Student may not inherit from final class (Person) 
使用final关键标记的方法不能被子类覆盖,是最终版本;
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->class Person 

{ 

    final function say()   
    { 
    } 

} 

class Student extends Person 

{ 

    function say()   
    { 
    } 

}

会出现下面错误:

Fatal error: Cannot override final method Person::say()

posted on 2015-03-02 14:34  七行笔记  阅读(89)  评论(0编辑  收藏  举报