4.3 对象的继承
视频正在上传
先粘贴代码和知识点。
知识点
4.3类的继承
1.关键字:extends
2.为什么要使用继承?
3.方法的覆盖。
4.私有变量和公共变量的使用。变量关键字的使用。
代码
<meta charset="utf-8">
<?php
class Person {
private $name; //名字
private $age; //年龄
private $role; //职业,角色
function __construct( $name,$age,$role ){
$this->name = $name;
$this->age = $age;
$this->role = $role;
}
public function act() { //方法
return "正常人类活动";
}
public function _print(){
echo "年龄为".$this->age."岁,身为".$this->role."的".$this->getName()."正在从事".$this->act()."。";
echo "<br>";
}
public function getName(){
return $this->name;
}
public function setName( $input_name ){
$this->name = $input_name;
}
public function setRole( $input_role ){
$this->role = $input_role;
}
public function getRole(){
return $this->role;
}
}
$person1 = new Person('张三',30,'it工程师');
$person2 = new Person('李四',19,'在校大学生');
$person3 = new Person('王二',13,'中学学生');
$person1->setName('张三丰');
$person1->_print();
$person2->_print();
$person3->_print();
class Student extends Person{
function __construct( $name,$age ){
parent::__construct( $name,$age,$role="学生" );
}
public function act() { //方法
return "学校的学业学习";
}
}
$person4 = new Student('王五',16);
$person4->setName("王小五");
$person4->setRole('高中生');
$person4->_print();
?>多学多练,才是根本,知识没有止境。
犯错没有关系,只有不断地虚心纠正,才能获得成功。
发布时间:2017-10-31,10:16:24
本节课主要讲解了面向对象的PHP类的继承。
