final关键字的应用

来源:互联网 发布:java的基础知识 编辑:程序博客网 时间:2024/06/03 20:00
 这个关键字只能用来定义类和定义方法, 不能使用final这个关键字来定义成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数,所以不能使用final来定义成员属性。使用final关键标记的类不能被继承;
01<?php
02final class Person
03{
04    function say()
05    {
06 
07    }
08}
09 
10class Student extends Person
11{
12    function say()
13    {
14 
15    }
16}
17?>
会出现下面错误:
1Fatal error: Class Student may not inherit from final class (Person)
使用final关键标记的方法不能被子类覆盖,是最终版本;
01<?php
02class Person
03{
04    final function say()
05    {
06 
07    }
08 
09}
10 
11class Student extends Person
12{
13    function say()
14    {
15 
16    }
17}
18?>
会出现下面错误:
view source
?
1Fatal error: Cannot override final method Person::say()
0 0