Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:写得很不错, 复习也很重要
<?php
//创建类
class Demo1{
//添加类成员
//属性
public $name = '33703259';
//方法
public function GET(){
$obj = new Demo1();
return '名称:' . $obj->name . '<hr>';
}
}
//访问类成员
$obj =new Demo1();
echo $obj->GET();
echo $obj->name;
<?php
//创建类
class Demo2{
//添加类成员
public $name = 'WJF';
public $qq = '33703259';
public function getname(){
// self当前类名
$obj = new self();
return '用户名:' . $obj->name . '<hr>';
}
public function getqq(){
// $this 当前类的实例引用
return 'QQ:' . $this->qq;
}
}
//访问类成员
$obj = new Demo2();
echo $obj->getname();
echo $obj->getqq();
<?php
class Demo3{
public $name;
public $qq;
public function get(){
return '用户名:' . $this->name . 'QQ:' . $this->qq;
}
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
echo $this->get();
}
}
new Demo3('用户名','QQ');
<?php
//创建类
class d4{
//添加类成员
public $name;
protected $qq;
public function get(){
return '用户名:' . $this->name .'QQ:' . $this->qq;
}
// 构造方法
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
}
//魔术方法 __get($name),属性重载
public function __get($name)
{
$id = $_GET['id'] ?? '';
if (isset($id) and $id==='1'){
return isset($this->qq) ? $this->qq : '当前暂无该用户QQ';
}else{
return '您不符合当前操作权限要求';
}
}
}
//访问类成员
$obj = new d4('wjf','33703259');
echo $obj->qq;
<?php
//类的继承
class d5{
public $name;
public $qq;
public function get(){
return '用户名:' . $this->name .'QQ:' . $this->qq;
}
// 构造方法
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
}
}
class d5_1 extends d5{
private $zt;
public function __construct($name, $qq,$zt)
{
parent::__construct($name, $qq);
$this->zt = $zt;
}
public function get()
{
return parent::get() . '状态:' . $this->zt;
}
}
$obj = new d5_1('WJF','33703259','在线');
echo $obj->get();
<?php
//trait:代码复用
trait T{
public function get(){
return '用户名:' . $this->name .'QQ:' . $this->qq;
}
}
class d6{
use T;
public $name;
public $qq;
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
}
}
class d6_1 extends d6{
private $zt;
public function __construct($name, $qq,$zt)
{
parent::__construct($name, $qq);
$this->zt = $zt;
}
public function get()
{
return parent::get() . '状态:' . $this->zt;
}
}
$obj = new d6_1('WJF','33703259','离线');
echo $obj->get();
<?php
//接口
interface idemo{
public function get();
}
class d7 implements idemo{
public $name;
public $qq;
public function get(){
return '用户名:' . $this->name .'QQ:' . $this->qq;
}
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
}
}
$obj = new d7('WJF','33703259');
echo $obj->get();
<?php
//抽象类
abstract class cx{
//抽象方法
abstract public function get();
//已实现的方法
public function h(){
return '已实现方法';
}
}
class d8 extends cx {
public $name;
public $qq;
public function get(){
return '用户名:' . $this->name .'QQ:' . $this->qq;
}
public function __construct($name,$qq)
{
$this->name = $name;
$this->qq = $qq;
}
}
$obj = new d8('WJF','33703259');
echo $obj->get();
echo $obj->h();