加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP 面向对象 final类与final办法

发布时间:2023-05-20 12:51:10 所属栏目:PHP教程 来源:
导读:PHP 面向对象 final类与final办法:Fatal error: Class SuperMath may not inherit from final class (Math) in E:PHPProjects est.php on line 14<?php //声明一个final类Math class Math{ public static $pi = 3

PHP 面向对象 final类与final办法:

Fatal error: Class SuperMath may not inherit from final class (Math) in E:PHPProjects est.php on line 14

<?php 

//声明一个final类Math  

class Math{  

public static $pi = 3.14;  

public function __toString(){  

return "这是Math类。";  

}  

public final function max($a,$b){  

return $a > $b ? $a : $b ;  

}  

}  

//声明类SuperMath 继承自 Math类  

class SuperMath extends Math {  

public final function max($a,$b){}  

}  

//执行会出错,final方法不能被重写。 

?>  

<?php 

//声明一个final类Math  

final class Math{  

public static $pi = 3.14; 

public function __toString(){  

return "这是Math类。";  

}  

}  

$math = new Math();  

echo $math; 

//声明类SuperMath 继承自 Math类  

class SuperMath extends Math {  

} //开源代码Cuoxin.com 

//执行会出错,final类不能被继承。 

?> 

Fatal error: Class SuperMath may not inherit from final class (Math) in E:PHPProjects est.php on line 16

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!