OOP复习笔记

 /*OOP相关的代名词不做讲解*/

OOP的三大特征:

封装 - 继承 - 多态

-----------------------------------目录----------------------------------------

00x1 继承 extends

00x2 封装 private

00x3 静态关键词 static

00x4 Final关键词

00x5 接口

00x6 构造方法&构析方法

00x7 抽象类  详细地址:http://www.cnblogs.com/xishaonian/p/6165884.html

00x8 魔术方法(__set,__get,__isset,__unset,) 详细地址:http://www.cnblogs.com/xishaonian/p/6170459.html

00x9 switch http://www.cnblogs.com/xishaonian/p/6204061.html

0Ax1 多态 http://www.cnblogs.com/xishaonian/p/6206710.html

-------------------------------------------------------------------------------

 

STATIC


 00x1 extends继承 案例一

在第二类声明中使用extends即可对第一个类进行继承。如下所示。

<?php 
/**
* OOP学习小结
*/class Human
{
    public $name;
    public $var;
    public $age;

    function run(){
        echo "走路";
    }
    function eat(){
        echo "吃饭";
    }
    function play(){
        echo "运动";
    }
}
/**
* 
*/
class Student extends Human         使用extends对Human进行继承
{
    public $school;
    public $class;

    function study(){
        echo "学习";
    }
    function readbook(){
        echo "看书";
    }
}
$a = new Student();
$a -> eat();
 ?>

  00x2 private封装 案例二

与之相关的还有很多相似的修饰符,比如public var等等。

封装一个属性或者类,在其前面直接加private即可。

调用一个类要用$this->,需要是在类的里面进行调用

<?php 
/**
* 封装性
*/
class Human
{
    private $name;
    public $sex;
    private $age;

    public function test(){
        echo "我的名字是".$this->name."<hr />"; #在这里使用$this->进行调用name属性。
    }
    private function test2(){
        echo "hello world";
    }

}
$a = new Human();
$a -> test();
$a -> test2(); #该条语句会出错。经过private封装的属性或者类都只能够在类里面自己调用。就好比自己家的老婆只有自己家才可以用一样。
 ?>

  00x3 static静态 案例三

 使用static效率会高一些,另外static是定义全局变量。

使用方法:

类名::属性或方法

<?php 
class Human
{
    var $name;
    var $sex;
    var $age;

    static function test(){
        echo "hello world";
    }
}
Human::test();      #调用Human类下的test方法。
 ?>
输出效果如下所示:

hello world

 00x4 Final关键词 案例四

final的英译是“最终的”的意思,顾名思义其作用就是不允许子类对父类进行重写。

<?php 
class Human
{
    public $name;
    public $sex;
    public $age;
 
    final function test(){            #使用final关键词对test方法进行修饰,不允许子类修改这个方法。
        echo "我很喜欢网络安全";
    }
}
class Student extends Human
{
    public function test(){          #修改test方法,顾名思义会出错。
        echo "我相信我的努力会让我成为大牛!";
    }
}
$a = new Student("张大牛","",12);
$a -> test();
 ?>

会提示如下错误(找不到test方法):

( ! ) Fatal error: Cannot override final method Human::test() in D:wampwww1.php on line 17

00x5 接口

接口的声明使用interface

继承接口使用implements

<?php 
/**
* 接口inface
*/
interface Human #定义一个接口
{
    public function test(); #接口中不需要实现的方法
}

class Student implements Human #接口的继承要用implements
{
    function test(){
        echo "hope me become IT killer";
    }
}
$a = new Student("王大牛","",12);
$a -> test();
 ?>

00x6 构造方法&构析方法

构造方法

在程序开始的时候自动调用

<?php 
class Student
{
    function __construct()
    {
        echo "hello world <br />";
    }
    function test()
    {
        echo "this is a test";
    }
}
$a = new Student();
$a ->test();
 ?>
输出效果如下所示:
hello world 
this is a test

构析方法,与构造相反,在程序最后的时候调用。

<?php 
class Student
{
    
    function __destruct()
    {
        echo "hello world";
    }
    function test()
    {
        echo "this is a test<br />";
    }
}
$a = new Student();
$a ->test();
 ?>

00x7 抽象类

抽象类的声明使用:abstract

抽象类的作用可当做全局变量。在抽象类当中不需要有实现的方法,

抽象类还不能实例化,只能够被重写!

<?php 
abstract class test
{
    abstract function hello();
}
/**
* 
*/
class class1 extends test
{
    
    function hello(){
        echo "hello world";
    }
}
$a = new class1();
$a ->hello();
 ?>

 00x8 魔术方法

__get:在对象访问私有成员的时候自动被调用 #切记一定要有一个参数!

__set:要修改一个封装属性的时候自动调用

__isset:检测对象是否有具有某个私有成员的时候就会被自动调用!

__unset

第一个魔术和方法:__get

<?php 
abstract class Date{
    private $name;
    private $sex;
    private $age;
}

class Student extends Date
{
    public function __get($x){
        echo "对不起,您无权查看学生隐私!";
    }
}
$a = new Student();
echo $a -> name;
 ?>

第二个魔术方法:__set

<?php 
class test
{
    private $name;
    private $sex;
    private $age;

    function __set($x,$y) #在修改一个封装属性的时候该方法会自动调用!
    {
        echo "hello world";
    }
}
$a = new test("王大牛","男",12);
$a -> name=("王大神");
 ?>
输出效果如下所示:
hello world

第三个魔术方法:__isset

<?php 
class Human
{
    public $name;
    public $sex;
    public $age;
    
    public function __construct($name,$sex,$age)
    {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    public function __isset($x) #在使用了isset判断属性存不存在的时候会自动调用该方法
    {
        echo "你想判断我的".$x."属性存在不存在<br />";
    }
}
$a = new Human("王大牛","男",12);
if(isset($a -> sex))
{
    echo "存在";
}else{
    echo "不存在";
}
 ?>

第四个魔术方法:__unset

<?php 
class Human
{
    private $name;
    private $sex;
    private $age;
    
    function __construct($name,$sex,$age)
    {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    function __unset($x)
    {
        echo "成功删除该属性";
    }
}
$a = new Human("郑大牛","男",12);
unset($a -> name);
 ?>

 00x9 switch

<?php 
$a = 123;
switch ($a) {
    case $a < 100:
        echo "{$a}小于100";
        break;
    case $a > 100:
        echo "{$a}大于100";
        break;
    default:
        echo "抱歉,你输入的可能不是数字!";
        break;
}
 ?>

 0Ax1 多态

多态是OOP里三个重要的特征。其他两个是:继承、封装

<?php 

interface test
{
    public function zhouchang();
    public function mianji();
}
class juxing implements test
{
    public $wide;
    public $high;

    function __construct($wide,$high)
    {
        $this->wide = $wide;
        $this->high = $high;
    }
    public function zhouchang()
    {
        echo "矩形的周长是:".($this->wide + $this->high)."<br />";
    }
    public function mianji()
    {
        echo "矩形的面积是:".($this->wide * $this->high)."<br />";
    }
}
/**
* 
*/
class yuanxing implements test
{

    private $r;

    function __construct($r)
    {
        $this->r = $r;
    }
    public function zhouchang()
    {
        echo "圆形的周长是:".(2 * 3.14 * $this->r."<br />");
    }
    public function mianji()
    {
        echo "圆形的面积是:".(3.14 * $this->r * $this->r."<br />");
    }
}
$a = new juxing(12,2);
$a -> zhouchang();
$a -> mianji();
$b = new yuanxing(999,666);
$b -> zhouchang();
$b -> mianji();
 ?>
原文地址:https://www.cnblogs.com/xishaonian/p/6198207.html