Home > Backend Development > PHP Tutorial > PHP中的final关键字

PHP中的final关键字

WBOY
Release: 2016-06-23 13:27:52
Original
1276 people have browsed it

final顾名思义就是“最终的,最后的”。

final修饰方法可以得到“最后的方法”,即不能被子类重写的方法。

class NbaPlayer{    final public function eat($food){        echo "food is ".$food;    }}
Copy after login


final修饰类可以得到“最后的类”,即不能被继承的类。


final class NbaPlayer(){}
Copy after login


例子:


<?php //子类的方法名和父类的方法名相同时,会重写(覆盖)父类的方法//对于不想被任何子类重写(修改)的方法,可以在class之前添加final关键字//对于不想被子类重写(修改)的方法,可以在方法定义的前面添加final关键字class BaseClass{	public function test($temp="123"){		echo "BaseClass::test called".$temp."\n";	}	final public function test1(){		echo "BaseClass::test1 called\n";	}}class ChildClass extends BaseClass{	public function  test($temp="123"){		echo "ChildClass::test called ";	}}$obj = new ChildClass();$obj->test("456");
Copy after login


版权声明:本文为博主原创文章,未经博主允许不得转载。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template