封装练习

<?php
//作业:设计一个类,包含$a,$b,求和的方法,求乘积的方法,可以有初始值,要求$a和$b要大于零小于100

class YunSuan
{
 private $a;
 private $b;
 
 function __construct($a,$b)
 {
  $this->a=$a;
  $this->b=$b;
 }
 function __set($n,$v)
 {
  if($v>0 && $v<100)
  {
   $this->$n=$v;
  }
 }
 function __get($n)
 {
  return $this->$n;
 }
 function He()
 {
  return $this->a+$this->b;
 }
 function Ji()
 {
  return $this->a*$this->b;
 }
}
$r=new YunSuan(5,5);
$r->a = 1;
$r->b = 2;
echo $r->He();
echo "<br>";
echo $r->Ji();
?>

原文地址:https://www.cnblogs.com/panyiquan/p/5565134.html