final定义的类是不能被继承的,那么final定义的父类方法能不能使用呢?如果类可以被继承,只是定义final方法,该父类的方法子类是可以使用的,但是不能重载!
父类
Class Preson{
public $name;//姓名
public $age;//年龄
public $sex;//性别
public $height;//身高
public $work;
function __construct($name,$age,$sex,$height){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
$this->height=$height;
}
function say(){
echo "我叫[$this->name],性别[$this->sex],几年[$this->age]岁,身高[$this->height]";
}
final function work(){
echo "我的工作是:".$this->work;
}
public $name;//姓名
public $age;//年龄
public $sex;//性别
public $height;//身高
public $work;
function __construct($name,$age,$sex,$height){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
$this->height=$height;
}
function say(){
echo "我叫[$this->name],性别[$this->sex],几年[$this->age]岁,身高[$this->height]";
}
final function work(){
echo "我的工作是:".$this->work;
}
子类
require_once 'Preson.class.php';
class China extends Preson{
}
$zhangsan=new China("张三", 18, '男', '175cm');
$zhangsan->work="网络工程师";
$zhangsan->say();
echo "<hr/>";
$zhangsan->work();
class China extends Preson{
}
$zhangsan=new China("张三", 18, '男', '175cm');
$zhangsan->work="网络工程师";
$zhangsan->say();
echo "<hr/>";
$zhangsan->work();
这样是没有问题的。
如果
在子类重载work方法,那么就会报错
function work(){
echo $this->work;
}
echo $this->work;
}
错误:Fatal error: Cannot override final method Preson::work() in C:\wamp\www\class\china.php on line
7