php学习笔记之封装练习题

练习题题目:

设计一个类:包含$a,$b,求和的方法,求乘积的方法,可以对变量进行初始化,$a,$b必须大于0小于100

<?php
class  shi
{
    private $a;
    private $b;
    //私有化
    function __construct($a,$b)
    {
        $this->a=$a;
        $this->b=$b;
    }
function __set($n,$s) { if($s>0 and $s<100) { $this->$n=$s; } } function __get($x) { return $this->$x; } function he() { return $he=$this->a + $this->b; } function cheng() { return $he=$this->a * $this->b; } } $r= new shi(3,2); echo $r->he(); echo "<br>"; echo $r->cheng(); ?>

这样的话还可以再添加上其他几个比如添加除减

原文地址:https://www.cnblogs.com/koker/p/5563279.html