第77讲 继承与实现比较 final const
来源:互联网 发布:网络剧投资 编辑:程序博客网 时间:2024/06/08 14:47
这个具体自己上网查了。。
php 跟 java 一样是单继承
1、implement 接口 可以看做是对类extends的补充,而且接口感觉起来更方便
2、implement 接口可以在不打破类继承的前提下对某个类进行功能性的扩展。
final
- 1、不希望某个方法被子类重载时,可以用final关键词修饰
- 2、不希望类被继承时候,可以用final
<?php/** final class A{ } class B extends A{ } print "t"; //运行会提示:Class B may not inherit from final class (A) */ class A{ public $money = -1; public function setMoney($money){ return $this->money = $money*0.8; } final public function getMoney(){ //假如 某个方法加了final 那么 B类就无法继承了。否则会报错提示 Cannot override final method A::getMoney() return $this->money; } } class B extends A{ /** public function setAge(){ } public function getMoney(){ } */ } $b = new B(); $b->setMoney(10); print $b->getMoney();?>
final 使用场景:
- 1、安全考虑,类的某个方法不允许修改
- 2、不希望某个类被其他的类继承
final 不能修饰成员属性(变量)
const
<?php class TestConst{ const SHUILV = 0.08; public function getRat($money){ return $money*self::SHUILV; //const 访问必须 用 类名::常量名 或者 parent::xx也行 } //public function getConst(){//访问常量不能这么访问 需要使用如上方getRat注释 // return $this->SHUILV; //} } class test extends TestConst{ public function getRat($money){ return $money*TestConst::SHUILV; //const 访问必须 用 类名::常量名 或者 parent::xx也行 } } $test = new test(); print $test->getRat(100); //print $test->getConst();?>
阅读全文
0 0
- 第77讲 继承与实现比较 final const
- PHP 继承与实现比较 final和const修饰符说明
- const与继承
- 比较const与#define
- const与#define比较
- define与const 比较
- 第十二讲:继承与派生
- PHP自学之路-----接口VS继承、final、const
- 继承+关键字(static+const+instanceof+final+)+protected
- 【Java】Final 与 C++ Const的区别
- 【Java】Final 与 C++ Const的差异
- Final 与 C++ Const的区别
- Java中的Final 与 C++中的const
- const T 与T const的比较
- const ptr与ptr const比较
- const & final
- const 与 #define 的比较
- const 与 #define的比较
- js动态添加的按钮,其响应事件的添加
- Uncaught TypeError: Cannot read property 'msie' of undefined
- window下安装mongoDB与设置数据库路径失败解决
- Visual Studio下建立并隐式调用自己的动态链接库dll
- 30、C#里面的列表框:ListBox的使用
- 第77讲 继承与实现比较 final const
- 通过js sdk 来进行第三方登录及授权(微博,微信,qq)
- [LeetCode]--Container With Most Water
- 关于listview中是用多个布局 并使用holderView 缓存机制的问题
- 针对json的查询--alibaba的开源项目jsonq
- Spring源码分析的几篇博客
- 解决NPM使用下载慢或者无法下载的问题error
- Tablayout的使用
- socket 并发服务器 代码案例 (多进程模式)