批改状态:未批改
老师批语:
php类的属性设置和访问
/class/Animal.php
<?php
/**
*
*/
class Animal
{
private $height;
private $weight;
private $age;
public function __construct($height,$weight,$age)
{
$this->height=$height;
$this->weight=$weight;
$this->age=$age;
}
public function __get($name)
{
if (isset($this->$name)){
echo "Getting ".$name.':'.$this->$name.'<br>';
} else {
echo '没有初始值';
}
}
public function __set($name, $value)
{
if (isset($this->$name)) {
$this->$name=$value;
} else {
echo $name.'not set';
}
}
}点击 "运行实例" 按钮查看在线实例
<?php /** * */ require './class/Animal.php'; $animal=new Animal(110,70,5); $animal->height; $animal->weight; $animal->age; echo '<hr>'; $animal->height=120; $animal->weight=80; $animal->age=7; $animal->height; $animal->weight; $animal->age;
点击 "运行实例" 按钮查看在线实例

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号