批改状态:未批改
老师批语:
<?php
/**
* 类的声明与实力化
* Date: 2018/9/3
* Time: 22:38
*/
//声明类:关键字class
header('content-type:text/html;charset=utf-8');
class Student1{
//静态成员
static $country = 'CHINA';
//类常量
const course = 'php';
//声明成员属性:public公共的 类的内部外部都可以访问
public $className;
//private私有的只有类中可以访问
private $gender;
private $name;
private $age;
//protected受保护的只有自己类中和子类可以访问
protected $grade=88;
//构造方法
function __construct($name,$gender,$age)
{
//初始化对象的属性
$this->name=$name;
$this->gender=$gender;
$this->age=$age;
// $this->grade=$grade;
//对象实例化时自动调用方法
// echo $this->show();
}
//获取属性的重载
function __get($name)
{
// TODO: Implement __get() method.
$this->$name;
}
//更新属性
function __set($name, $value)
{
// TODO: Implement __set() method.
$this->$name=$value;
}
function show(){
return '我的名字是:'.$this->name.'性别:'.$this->gender.'我的'.Student1::course.'成绩是'.$this->grade.'分';
}
}
//类的实例化new
$stu1 = new Student1('andrew','男','25');
echo $stu1->show();
/**
* 类的继承和方法重写
*/
//Student2 继承 Student1
class Student2 extends Student1{
private $height;
function __construct($name, $gender, $age,$height)
{
parent::__construct($name, $gender, $age);
$this->height=$height;
}
//重写父类方法
function show()
{
echo '我身高'.$this->height;
return parent::show(); // TODO: Change the autogenerated stub
}
}
$stu2 = new Student2('a','male','18',180);
echo $stu2->show();
echo '国籍:'.Student1::$country;点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号