您的位置:首页 > 编程语言 > PHP开发

PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)

2016-09-11 16:22 1106 查看

本文实例讲述了PHP面向对象的特性。分享给大家供大家参考,具体如下:

Demo1.php

<?php
header('Content-Type:text/html; charset=utf-8;');
//创建一个电脑类
class Computer {
//什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外。
//public 是对字段的公有化,这个字段类外即可访问,赋值和取值
public $_name = '联想';
}
$computer = new Computer();
$computer -> _name = 'Dell';
echo $computer->_name;
?>

Demo2.php

<?php
header('Content-Type:text/html; charset=utf-8;');
class Computer {
//private 是私有化,即对字段进行封装的操作,类外无法访问,取值和赋值都不能操作
private $_name = '联想';
}
$computer = new Computer();
echo $computer->_name;
?>

Demo3.php

<?php
header('Content-Type:text/html; charset=utf-8;');
class Computer {
private $_name = '联想';
//这个时候我采用一个公共对外的方法来访问私有字段
//因为私有字段只能在类内访问,而对外的公共方法是类内的。
//更而公共方法又是公共的,所以类外又可访问。
public function _run(){
//字段在类内调用的时候必须是类 -> 字段,而$_name只是一个普通变量而已。
//字段在类外调用的方法是对象 -> 字段,而类内就必须使用 Computer -> _name
//但是在本类中,可以使用一个关键字来代替字来代替 Computer ,那就是 $this
echo $this ->_name;
}
}
$computer = new Computer();
$computer -> _run();
?>

Demo4.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $name;
private $model;
private $cpu;
private $keyboard;
private $show;
private $zb;
//必须写个对外的入口,才可以取到
public function getName() {
return $this->name;
}
//必须写一个对内的入口,对私有字段进行赋值
public function setName($name) {
//这里的 $name 只是一个变量而已,参数而已
//$this->name 才是类的字段
$this->name = $name;
}
}
$computer = new Computer ();
echo $computer->getName();
$computer->setName('Dell');
echo $computer->getName();
?>

Demo5.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $_name;
private $_model;
private $_cpu;
//当类外的对象直接调用私有字段时,会跟着去检查是否有拦截器,
//如果直接对 $_name 进行赋值,那么__set 方法就会拦截住,就不会报错了。
//采用拦截器进行赋值和取值
//赋值
private function __set($_key,$_value){
//采用$_key = '_name',那么 $_value = '联想';
//$this ->_name = '联想';
$this ->$_key = $_value;
}
//取值
private function __get($_key){
return $this -> $_key;
//如果 $_key = '_name' 那么 $this -> _name;
//如果 $_key = '_cpu' 那么 $this -> _cpu;
//如果 $_key = '_model' 那么 $this -> _model;
}
}
$computer = new Computer ();
$computer->_name = '联想';
$computer->_cpu = '四核';
$computer->_model = 'i7';
echo $computer->_name;
echo $computer->_cpu;
echo $computer->_model;
?>

Demo6.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $_name;
private $_model;
private $_cpu;
//__set 和 __get 方法私有了,还是可以执行,是因为
//因为目前程序的指针已经在类内了。而类内可以执行封装的方法
//类内执行私有方法,不会出现任何错误。
//它只需要间接的拦截就可以了。拦截是在内类执行的。
//说白了,__set() 和 __get() 是 PHP 内置的方法,具有一定的特殊性
private function __set($_key, $_value) {
$this->$_key = $_value;
}
private function __get($_key) {
return $this->$_key;
}
}
$computer = new Computer ();
$computer->_name = '联想';
$computer->_cpu = '四核';
$computer->_model = 'i7';
echo $computer->_name;
echo $computer->_cpu;
echo $computer->_model;
?>

Demo7.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
const NAME = 'DELL';
}
//常量的输出方法 类::常量
echo Computer::NAME;    //DELL
?>

Demo8.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_count = 0;
public function _add(){
$this -> _count++;  //$_count = $_count+1 $_count++
}
}
//做一个累计的效果
$computer1 = new Computer();
$computer1 ->_add();
$computer1 ->_add();
$computer1 ->_add();
echo $computer1 -> _count;
echo '<br />';
$computer2 = new Computer();
$computer2 ->_add();
$computer2 ->_add();
$computer2 ->_add();
echo $computer2 -> _count;
?>

Demo9.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public static $_count = 0;
public function _add(){
//如果是静态成员字段,那么就应该用 self 来调用,而不是 $this
self::$_count++;
}
}
//做一个累计的效果
$computer1 = new Computer();
$computer1 ->_add();
echo Computer::$_count;
$computer1 ->_add();
echo Computer::$_count;
$computer1 ->_add();
echo Computer::$_count;
echo '<br />';
$computer2 = new Computer();
$computer2 ->_add();
echo Computer::$_count;
$computer2 ->_add();
echo Computer::$_count;
$computer2 ->_add();
echo Computer::$_count;
?>

Demo10.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public static $_count = 0;
public static function _add(){
self::$_count++;
}
}
Computer::_add();
Computer::_add();
Computer::_add();
echo Computer::$_count;
?>

Demo11.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
}
$computer = new Computer();
echo $computer instanceof Computer;
?>

Demo12.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
//这是父类,电脑类
class Computer {
public $_name = '联想';
public function _run(){
echo '联想在运行!';
}
}
//子类,笔记本电脑类
class NoteComputer extends Computer {
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
?>

Demo13.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_name = '联想';
public function _run(){
echo '联想在运行!';
}
}
class NoteComputer extends Computer {
//我不需要父类的字段和方法,那么可以采用重写的方法覆盖掉父类的字段和方法
public $_name = 'Dell';
public function _run(){
echo 'Dell在运行!';
}
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
?>

Demo14.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
//私有化,但是无法被子类继承,这个时候就应该用受保护的修饰符来封装
protected $_name = '联想';
protected function _run(){
return '联想在运行!';
}
}
class NoteComputer extends Computer {
public function getTop() {
echo $this->_name;
echo $this->_run();
}
}
$noteComputer = new NoteComputer();
$noteComputer -> getTop();
?>

Demo15.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_name = '联想';
public function _run(){
return '联想在运行!';
}
}
class NoteComputer extends Computer {
//我子类已经覆盖了父类的字段和方法,
//但是我又要调用父类的字段和方法,那怎么办呢?
public $_name = 'Dell';
public function _run(){
echo 'Dell在运行!';
echo parent :: _run();
}
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
//DellDell在运行!联想在运行!
?>

Demo16.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
//final 如果加在类前面,表示这个类不能被继承
// final class Computer {
// }
class Computer {
//final 如果加在方法前面,表示不能够重写些方法
final public function _run(){
}
}
class NoteComputer extends Computer {
public function _run(){
}
}
$noteComputer = new NoteComputer();
?>

Demo17.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
//创建一个抽象类,只要在 class 前面加上 abstract 就是抽象类了
//抽象类不能够被实例化,就是创建对象
//只在类里面有一个抽象方法,那么这个类必须是抽象类,类前面必须加上 abstract
abstract class Computer {
public $_name = '联想';
//抽象类里创建一个抽象方法
//抽象方法不能够实现方法体的内容
abstract public function _run();
//我在抽象类里能否创建一个普通方法
public function _run2(){
echo '我是父类的普通方法';
}
}
//类不能够实现多继承,只支持单继承。
//抽象类是给子类用来继承的,实现一种规范和资源的共享
class NoteComputer extends Computer {
//抽象类的抽象方法,子类必须重写,不然会报错。
//抽象类里的普通方法不需要重写,子类会直接继承下来
public function _run(){
echo '我是子类的方法';
}
}
$noteComputer = new NoteComputer();
$noteComputer -> _run();
$noteComputer -> _run2();
echo $noteComputer -> _name;
?>

Demo18.php

<?php
/*
* 到底应该用抽象类还是接口呢
* 如果你要继承多个接口的方法规范,那么就用接口好了。
* 如果你要共享一个方法体内容,那么就用抽象类。
* */
header ( 'Content-Type:text/html; charset=utf-8;' );
//创建一个接口
//接口也不能被实例化
//接口是为了规范实现它的子类,以达到统一的目的。也可以共享数据
interface Computer {
//成员字段必须是变量
const NAME = '成员 ';
//接口里的所有方法都是抽象方法,不能够写方法体
//并且接口的抽象方法不需要写 abstract
public function _run();
public function _run2();
}
interface Computer2 {
public function _run3();
}
//子类继承接口的说法,叫做实现,接口可以多实现
class NoteComputer implements Computer,Computer2 {
public function _run() {
echo '我重写了run';
}
public function _run3() {
echo '我重写了run3';
}
public function _run2() {
echo '我重写了run2';
}
}
$noteComputer = new NoteComputer();
$noteComputer -> _run();
$noteComputer -> _run2();
$noteComputer -> _run3();
echo NoteComputer::NAME;
//接口 :: 常量
//echo Computer::NAME;
?>

Demo19.php

<?php
header ( 'Content-Type:text/html; charset=utf-8;' );
//什么叫做多态,字面意思,多种形态
//一个动作由不同的人去执行,而产生不同的效果或者效果,即为多态。
//一个人通过不同的状态去执行同一种动作,形成不同的效果,也可以称作为多态。
//园丁    剪    修理花草
//理发师  剪    理发
//总裁    剪    裁员
//人   笔记本   运行 win7开机了
//人   台式机   运行 xp开机了
//创建一个接口,来规范运行的方法
interface Computer {
public function version(); //这个方法表示采用什么电脑
public function work();   //这台电脑是怎么运行的
}
//创建一个笔记本的类实现接口
class NoteComputer implements Computer {
public function version() {
echo '笔记本';
}
public function work() {
echo '可以便携式运行 win7';
}
}
//创建一个台式机的类实现接口
class DesktopComputer implements Computer {
public function version() {
echo '台式机';
}
public function work() {
echo '在工作站运行 XP';
}
}
//创建一个用户
class Person {
//创建一个方法来接受电脑(笔记本电脑,也可以是台式电脑)
//怎么接受,将他们的对象传进来就 OK 啦。
public function _run($type) {
echo '这个人的';
$type -> version();
$type ->work();
}
}
//多态的原理,就是类都写好了,不要去修改它,只要在类外的调用参数的更改
//而最后的结果也会得到更改,那么这个就是多态。
//有一个接口,两个类,一个是笔记本的类,一个是台式机的类
//创建了笔记本
$noteComputer = new NoteComputer();
//创建台式机
$desktopComputer = new DesktopComputer();
//创建一个人
$person = new Person();
//使用电脑
$person -> _run($noteComputer); //这种传递,叫做对象引用的传递
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP 面向对象 特性
相关文章推荐