首页 > php教程 > php手册 > 第十八章 面向对象的特性

第十八章 面向对象的特性

WBOY
发布: 2016-06-13 09:34:46
原创
1324 人浏览过

学习要点:
1.OOP 的封装
2.OOP 的继承
3.OOP 的多态

 

面向对象的三个主要特性是封装、继承和多态。

一.OOP的封装

隐藏对象的字段和实现细节,仅对外公开接口,控制在程序中字段的读和修改的访问级
别;将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与
操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。

 

字段的作用域
1.public 公共的(类外可以访问)
2.private 私有的(类内可以访问)
3.protected 受保护的(类内和子类可以访问,类外不可访问)

 

创建使用了私有的字段,这样外部就无法访问了

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>$_model</span> = 'LX'<span>;
}</span>
登录后复制

通过一个公共方法作为入口,访问私有字段,而必须使用$this关键字。

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>$_model</span> = 'LX'<span>;
    </span><span>//</span><span>通过公共方法来访问私有字段</span>
    <span>function</span><span> run() {
        </span><span>echo</span> <span>$this</span>-><span>_name;
    }
}
</span><span>$computer</span>->run ();
登录后复制

属性操作(私有字段的赋值与取值)

可以设计两个公共方法,一个方法为setName(),用于赋值;一个方法为getName(),
用于取值。

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>赋值</span>
    <span>function</span> setName(<span>$_name</span><span>) {
        </span><span>$this</span>->_name = <span>$_name</span><span>;
    }
    </span><span>//</span><span>取值</span>
    <span>function</span><span> getName() {
        </span><span>return</span> <span>$this</span>-><span>_name;
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->setName ( 'IBM'<span> );
</span><span>echo</span> <span>$computer</span>->getName ();
登录后复制

如果有十个字段那么就必须要二十个方法才能够赋值和取值,那么有没有更简便的方法
呢?PHP内置两个方法(拦截器)专门用于取值与赋值:__set(),__get()。

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>所有字段的赋值都在这里进行</span>
    <span>function</span> __set(<span>$_key</span>, <span>$_value</span><span>) {
        </span><span>$this</span>-><span>$_key</span> = <span>$_value</span><span>;
    }
    </span><span>//</span><span>所有字段的取值都在这里进行</span>
    <span>function</span> __get(<span>$_key</span><span>) {
        </span><span>return</span> <span>$this</span>-><span>$_key</span><span>;
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->_model = 'LX'<span>;
</span><span>echo</span> <span>$computer</span>->_model;
登录后复制

方法私有:有些使用类里面的方法并不需要对外公开,只是里面运作的一部分,这个时
候可以将方法也封装起来。

<span>class</span><span> Computer {
    </span><span>//</span><span>类的字段(成员)</span>
    <span>private</span> <span>$_name</span><span>;
    </span><span>private</span> <span>$_model</span><span>;
    </span><span>//</span><span>私有方法</span>
    <span>private</span> <span>function</span><span> getEcho() {
        </span><span>echo</span> '我是私有化的方法'<span>;
    }
    </span><span>//</span><span>公共方法一般是对外的入口</span>
    <span>public</span> <span>function</span><span> run() {
        </span><span>$this</span>-><span>getEcho ();
    }
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>$computer</span>->run ();
登录后复制

建议:方法前面如果没有修饰符,那么就是外部可访问的公共方法,但为了让程序更加
的清晰,建议在前面加上public。

常量(constant)
在类中可以定义常量,用来表示不会改变的值。对于从该类实例化的任何对象来说,常
量值在这些对象的整个生命周期中都保持不变。

<span>class</span><span> Computer {
    </span><span>const</span> <span>PI</span> = 3.1415926<span>;
}
</span><span>echo</span> Computer::<span>PI</span>;
登录后复制

静态类成员
有时候,可能需要创建供所有类实例共享的字段和方法,这些字段和方法与所有的类实
例有关,但不能由任何特定对象调用。

<span>class</span><span> Computer {
    </span><span>public</span> <span>static</span> <span>$_count</span> = 0<span>;
}
</span><span>echo</span> Computer::<span>$_count</span>;
登录后复制

一般来说,必须将字段做成私有化。所以可能需要这么做:

<span>class</span><span> Computer {
    </span><span>private</span> <span>static</span> <span>$_count</span> = 0<span>;
    </span><span>public</span> <span>static</span> <span>function</span><span> setRun() {
        self</span>::<span>$_count</span> ++<span>;
    }
    </span><span>public</span> <span>static</span> <span>function</span><span> getRun() {
        </span><span>return</span> self::<span>$_count</span><span>;
    }
}
Computer</span>::<span>setRun ();
</span><span>echo</span> Computer::getRun ();
登录后复制

Instanceof关键字
PHP5有一个instanceof关键字,使用这个关键字可以确定一个对象是类的实例、类的
子类,还是实现了某个特定接口,并进行相应的操作。

<span>class</span><span> Computer {
    
}
</span><span>$computer</span> = <span>new</span><span> Computer ();
</span><span>echo</span> (<span>$computer</span> instanceof Computer);
登录后复制

二.OOP继承

继承是从一个基类得到一个或多个类的机制。
继承自另一个类的类被称为该类的子类。这种关系通常用父类和孩子来比喻。子类将继
承父类的特性。这些特性由属性和方法组成。子类可以增加父类之外的新功能,因此子类也
被称为父类的“扩展”。

在PHP中,类继承通过extends关键字实现。继承自其他类的类成为子类或派生类,子
类所继承的类成为父类或基类。(PHP只支持单继承,PHP不支持方法重载)。

<span>class</span><span> Computer {
    </span><span>private</span> <span>$_name</span> = '联想120'<span>;
    </span><span>private</span> <span>function</span> __get(<span>$_key</span><span>) {
        </span><span>return</span> <span>$this</span>-><span>$_key</span><span>;
    }
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}
</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
}
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$notebookcomputer</span>-><span>run ();
</span><span>echo</span> <span>$notebookcomputer</span>->_name;
登录后复制

字段和方法的重写(覆盖)
有些时候,并不是特别需要父类的字段和方法,那么可以通过子类的重写来修改父类的
字段和方法。

<span>class</span><span> Computer {
    </span><span>public</span> <span>$_name</span> = '联想120'<span>;
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>$_name</span> = 'IBM'<span>;
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是子类'<span>;
    }
}</span>
登录后复制

子类调用父类的字段或方法
为了安全,我们一般将父类的方法封装了起来,这样,外部就无法调用,只能被继承它
的子类所看到。这个时候,就需要通过子类操作来调用父类了。

<span>class</span><span> Computer {
    </span><span>protected</span> <span>$_name</span> = '联想120'<span>;
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>function</span><span> getName() {
        </span><span>echo</span> <span>$this</span>-><span>_name;
    }
    </span><span>public</span> <span>function</span><span> getRun() {
        </span><span>echo</span> <span>$this</span>-><span>run ();
    }
}</span>
登录后复制

通过重写调用父类的方法
有的时候,我们需要通过重写的方法里能够调用父类的方法内容,这个时候就必须使用
语法:父类名::方法() 或者parent::方法()即可调用。

<span>class</span><span> Computer {
    </span><span>protected</span> <span>function</span><span> run() {
        </span><span>echo</span> '我是父类'<span>;
    }
}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> Computer::<span>run ();
    }
}</span>
登录后复制

final关键字可以防止类被继承,有些时候只想做个独立的类,不想被其他类继承使用,
那么就必须使用这个关键字。建议只要是单独的类,都加上这个关键字。

<span>final</span> <span>class</span><span> Computer {
    </span><span>//</span><span>无法继承的类</span>
    <span>final</span> <span>public</span> <span>function</span><span> run() {
    } </span><span>//</span><span>无法被继承的方法</span>
<span>}

</span><span>class</span> NoteBookComputer <span>extends</span><span> Computer {
    </span><span>//</span><span>会报错</span>
}
登录后复制

抽象类和方法(abstract)
抽象方法很特殊,只在父类中声明,但在子类中实现。只有声明为abstract的类可以声
明抽象方法。

规则:
1.抽象类不能被实例化,只能被继承。
2.抽象方法必须被子类方法重写。

<span>abstract</span> <span>class</span><span> Computer {
    </span><span>abstract</span> <span>function</span><span> run();
}

</span><span>final</span> <span>class</span> NotebookComputer <span>extends</span><span> Computer {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '我实现了'<span>;
    }
}</span>
登录后复制

接口(interface)
接口定义了实现某种服务的一般规范,声明了所需的函数和常量,但不指定如何实现。
之所以不给出实现的细节,是因为不同的实体可能需要用不同的方式来实现公共的方法定
义。关键是要建立必须实现的一组一般原则,只要满足了这些原则才能说实现了这个接口。

规则:
1.类全部为抽象方法(不需要声明abstract)
2.接口抽象方法必须是public
3.成员(字段)必须是常量

<span>interface</span><span> Computer {
    </span><span>const</span> NAME = '联想120'<span>;
    </span><span>public</span> <span>function</span><span> run();
}

</span><span>final</span> <span>class</span> NotebookComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
}
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$notebookcomputer</span>-><span>run ();
</span><span>echo</span> Computer::NAME;
登录后复制

子类可以实现多个接口

<span>interface</span><span> Computer {
    </span><span>const</span> NAME = '联想120'<span>;
    </span><span>public</span> <span>function</span><span> run();
}

</span><span>interface</span><span> Notebook {
    </span><span>public</span> <span>function</span><span> book();
}

</span><span>final</span> <span>class</span> NotebookComputer <span>implements</span> Computer,<span> Notebook {
    </span><span>public</span> <span>function</span><span> run() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
    </span><span>public</span> <span>function</span><span> book() {
        </span><span>echo</span> '实现了接口的方法'<span>;
    }
}</span>
登录后复制

三.多态

多态是指OOP 能够根据使用类的上下文来重新定义或改变类的性质或行为,或者说接
口的多种不同的实现方式即为多态。把不同的子类对象都当作父类来看,可以屏蔽不同子类
对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。

<span>interface</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version();
    </span><span>public</span> <span>function</span><span> work();
}

</span><span>class</span> NotebookComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version() {
        
        </span><span>echo</span> '联想120'<span>;
    }
    </span><span>public</span> <span>function</span><span> work() {
        </span><span>echo</span> '笔记本正在随时携带运行!'<span>;
    }
}

</span><span>class</span> desktopComputer <span>implements</span><span> Computer {
    </span><span>public</span> <span>function</span><span> version() {
        </span><span>echo</span> 'IBM'<span>;
    }
    </span><span>public</span> <span>function</span><span> work() {
        </span><span>echo</span> '台式电脑正在工作站运行!'<span>;
    }
}

</span><span>class</span><span> Person {
    </span><span>public</span> <span>function</span> run(<span>$type</span><span>) {
        </span><span>$type</span>-><span>version ();
        </span><span>$type</span>-><span>work ();
    }
}

</span><span>$person</span> = <span>new</span><span> Person ();
</span><span>$desktopcomputer</span> = <span>new</span><span> desktopComputer ();
</span><span>$notebookcomputer</span> = <span>new</span><span> NoteBookComputer ();
</span><span>$person</span>->run ( <span>$notebookcomputer</span> );
登录后复制

 注:文章出自李炎恢PHP视频教程,本文仅限交流使用,不得用于商业用途,否则后果自负。

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板