【面向对象编程】14.final关键字的应用

这个关键字只能用来定义和定义方法, 不能使用final这个关键字来定义成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数,所以不能使用final来定义成员属性。
使用final关键标记的类不能被继承;
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
final class Person {
    function say() {
 
    }
}
 
class Student extends Person {
    function say() {
 
    }
}
?>
会出现下面错误:
1
Fatal error: Class Student may not inherit from final class (Person)
使用final关键标记的方法不能被子类覆盖,是最终版本;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
class Person {
    final function say() {
 
    }
 
}
 
class Student extends Person {
    function say() {
 
    }
}
?>
会出现下面错误:
1
Fatal error: Cannot override final method Person::say()

评论

此博客中的热门博文

PHPMailer配置163邮箱

vue webstrom报错 Expected indentation of 2 spaces but found 4

php判断今天是星期几的方法