1.final关键字(不能修饰属性,即变量)

a.希望一个类不被其他类来继承(出于安全性)

案例1

结果:

Parse error: syntax error, unexpected T_ECHO, expecting T_FUNCTION in E:\Software_default\wamp_wwwroot\interface\interface05.phpon line 8

 

来自 <http://localhost/interface/interface05.php>

 

b.希望某个方法,不被子类改写,用final,对比案例2,案例3结果

 

案例2:没有重写

getrate(100);
?>

结果

100

 

来自 <http://localhost/interface/interface06.php>

 

 

案例3:覆盖

getrate(100);
?>

结果

10000

 

来自 <http://localhost/interface/interface07.php>

 

 

案例4:禁止重写

getrate(100);
?>

结果:

Fatal error: Cannot override final method A::getrate() in E:\Software_default\wamp_wwwroot\interface\interface08.phpon line 16

 

来自 <http://localhost/interface/interface08.php>