批改状态:未批改
老师批语:
<meta charset="UTF-8">
<?php
class GirlFriend
{
//创建私有成员
private $name;
private $age;
//构造器,用于实例化初始化
public function __construct($name,$age)
{
$this->name = $name;
$this->age = $age;
}
//getname方法
public function getname()
{
return $this->name;
}
//getage方法
public function getage()
{
return $this->age;
}
//没有魔术方法就要这样
// public function setname($value)
// {
// return $this->name = $value;
// }
//魔术方法,用于获取方法
public function __set($name, $value)
{
return $this->$name = $value;
}
//魔术方法,用于获取私方法
public function __get($name)
{
return $this->$name;
}
}
//实例化
$demo1 = new GirlFriend('叶筱葵',27);
echo $demo1->getname();
echo $demo1->getage();
$demo1->name = '雪贞';
echo $demo1->name;点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号