forecho

把生命浪费在美好的事物上

面向对象编程思想(二)

2013年02月17日

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
//声明一个笔记本的类
class NoteBook{
    //成员属性
    private $name;
    private $type;
    private $os;
    private $work;
   
    //构造函数:创建和初始化对象成员属性
    function __construct($name,$type,$os,$work){
        $this->name = $name;
        $this->type = $type;
        $this->os = $os;
        $this->work = $work;
    }
   
    function __get($proName){
        return "======>".$this->$proName."<======<br/>";
    }
   
    function __set($proName,$proValue){
        echo "======>".$this->$proName = $proValue."<======<br/>";
    }
   
    function __isset($proName){
        return isset($this->$proName);
    }
   
    function __unset($proName){
        unset($this->$proName);
    }
   
    //成员方法
    function say(){
        return "<p>应用于".$this->work." - ".
               $this->name." - ".
               $this->type." - ".
               $this->os."</p>";
    }
}
   
$macbook = new NoteBook("MacBook Pro","MC374","Mac OS X Snow Leopard","设计");
echo $macbook->say();
echo $macbook->name;
echo $macbook->type;
echo $macbook->os;
echo $macbook->work;
   
echo "<hr/>";
   
$macbook->name = "SONY";
$macbook->type = "ZT288";
$macbook->os = "Windows 7";
$macbook->work = "娱乐";
   
unset($macbook->name);
   
echo "<hr/>";
   
if(isset($macbook->name)){
    echo "成员属性name存在于NoteBook类中";
}else{
    echo "成员属性name不存在于NoteBook类中";
}
   
?>