这篇文章主要介绍了php入门教程之面向对象的特性,结合实例形式分析了php面向对象所涉及的继承、多态、接口、抽象类及抽象方法等,需要的朋友可以参考下
本文实例讲述了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面向对象中的继承,多态,接口,抽象类,抽象方法实例教程的详细内容。