标签:
设计一个类; 包含$a,$b,求和的方法,求乘积的方法, 可以对变量进行初始化, $a,$b必须大于0小于100
class suanshu
{
private $shuzi1;
private $shuzi2;
function __construct($a,$b)
{
$this->shuzi1 = $a;
$this->shuzi2 = $b;
}
function __set($n,$v)
{
if($v>0 and $v<100)
{
$this->$n = $v;
}
}
function __get($n)
{
return $this->$n;
}
function zh()
{
return $this->shuzi1 + $this->shuzi2;
}
function cj()
{
return $this->shuzi1 * $this->shuzi2;
}
}
$r = new suanshu(6,5);
$r->shuzi1=20;
$r->shuzi2=5;
echo "总和=".$r->zh();
echo "<br />";
echo "乘积=".$r->cj();
显示结果:
标签:
原文地址:http://www.cnblogs.com/chenchen0815/p/5565518.html