[asp]代码库
<!-- 通过$this 传递对象
在这个例子中,我们写一个根据不同的年龄发不同工资的类.
我们设置处理年龄和工资的业务模型为一个独立的类.
-->
<?
class User{
private $age ;
private $sal ;
private $payoff ; //声明全局属性.
//构造函数,中创建Payoff的对象.
public function __construct(){
$this->payoff = new Payoff();
}
public function getAge(){
return $this->age;
}
public function setAge($age){
$this->age = $age;
}
// 获得工资.
public function getSal(){
$this->sal = $this->payoff->figure($this);
return $this->sal;
}
}
//这是对应工资与年龄关系的类.
class Payoff{
public function figure($a){
$sal =0;
$age = $a->getAge();
if($age >80 || $age <16 ){
$sal = 0;
}elseif ($age > 50){
$sal = 1000;
}else{
$sal = 800;
}
return $sal;
}
}
//实例化User
$user = new User();
$user->setAge(55);
echo $user->getAge()."age ,his sal is " . $user->getSal();
echo "<br>";
$user->setAge(20);
echo $user->getAge()."age , his sal is " . $user->getSal();
echo "<br>";
$user->setAge(-20);
echo $user->getAge()."age , his sal is " . $user->getSal();
echo "<br>";
$user->setAge(150);
echo $user->getAge()."age , his sal is " . $user->getSal();
?>
by: 发表于:2017-10-19 09:40:55 顶(0) | 踩(0) 回复
??
回复评论