zoukankan      html  css  js  c++  java
  • PHP -- 类和对象基础入门

         本文目录:   

    1. 创建简单类和对象
    2. 继承
    3. 抽象类
    4. 接口
    5. 构造方法
    6. 析构函数

        1. 创建简单类   

    创建一个People的类,在这里有四个要点说明一下:

    第一个是在PHP中,访问属性(或者方法)不是用我们所常用的点运算符(.),而是用->

    第二个是在PHP中,方法需要用function在标识,这点于Javascript很类似。

    第三点是当我们声明一个变量时,需要用var,这点也与Javascript非常类似。

    第四点是在PHP中,也有着public ,protected,private三个与C#同样的访问修饰符,不再赘述。

    可以用$p直接访问$name属性,那么我们可以用get()和set()方法对其进行控制

    简单例子如下:

    <?php 
        class People
        {
            var $name;
            public function GetName()
            {
                return $this->name;
            }
            public function SetName($name)
            {
                $this->name=$name;
            }
        }
        
        $p=new People();
        $p->SetName("kym");
        echo($p->GetName());
    ?>

        2.继承    

    在PHP中,继承和Java是一样的,都使用extends关键字。

    我们访问父类在C# 中用base,在Java中用super,但是在PHP中,我们用parent关键字。

    在php中,如果我们要访问自身的方法,那么可以用this,也可以用self

     简单示范如下:

    class People
    {
        private $name;
        public function GetName()
        {
            return $this->name;
        }
        public function SetName($name)
        {
            $this->name=$name;
        }
    }
    class Student extends People
    {
        public function GetName()
        {
            return "kym";
        }
        private $grade;
        public function SayHello()
        {
            echo("Good Morning,".self::GetName());
            //echo("Good Morning,".$this->GetName());
        }
    }

        3. 抽象类      

    简单例子:

    <?php 
        abstract class People
        {
            private $name;
            public function GetName()
            {
                return $this->name;
            }
            public function SetName($name)
            {
                $this->name=$name;
            }
            abstract function SayHello();
        }
        class Student extends People
        {
            public function SayHello()
            {
                echo("Good Morning,".parent::GetName());
            }
        }
        $s=new Student();
        $s->SetName("kym");
        $s->SayHello();
    ?>

        4. 接口      

    简单例子:

    <?php 
        abstract class People
        {
            private $name;
            public function GetName()
            {
                return $this->name;
            }
            public function SetName($name)
            {
                $this->name=$name;
            }
            abstract function SayHello();
        }
        interface IRun
        {
            function Run();
        }
        class Student extends People implements IRun
        {
            public function SayHello()
            {
                echo("Good Morning,".parent::GetName());
            }
            public function Run()
            {
                echo("两条腿跑");
            }
        }
        $s=new Student();
        $s->SetName("kym");
        $s->SayHello();
        $s->Run();
    ?>

        5. 构造方法     

    简单例子:

    <?php 
        class Person
        {
            private $name;
            private $age;
            public function Person($name,$age)
            {
                $this->name=$name;
                $this->age=$age;
            }
            public function SayHello()
            {
                echo("Hello,My name is ".$this->name.".I'm ".$this->age);    
            }
        }
        $p=new Person("kym",22);
        $p->SayHello();
    ?>

     注意:在PHP中并不支持构造函数链,也就是说,在你初始化子类的时候,他不会自动去调用父类的构造方法。

    <?php 
        class Person
        {
            private $name;
            private $age;
            public function Person($name,$age)
            {
                $this->name=$name;
                $this->age=$age;
            }
            public function SayHello()
            {
                echo("Hello,My name is ".$this->name.".I'm ".$this->age);    
            }
        }
        class Student extends Person
        {
            private $score;
            public function Student($name,$age,$score)
            {
                $this->Person($name,$age);
                $this->score=$score;
            }
            public function Introduce()
            {
                parent::SayHello();
                echo(".In this exam,I got ".$this->score);
            }
        }
        
        $s=new Student("kym",22,120);
        $s->Introduce();
    ?>

        6. 析构函数     

    析构函数和C#和C++中不同,在PHP中,析构函数的名称是__destructor()。

    class Student extends Person
    {
        private $score;
        public function Student($name,$age,$score)
        {
            $this->Person($name,$age);
            $this->score=$score;
        }
        public function Introduce()
        {
            parent::SayHello();
            echo(".In this exam,I got ".$this->score);
        }
        function __destruct()
        {
            echo("卸载啦。。。");
        }
    }

    FROM : http://www.cnblogs.com/kym/archive/2010/02/16/1668651.html

               http://www.cnblogs.com/kym/archive/2010/02/16/1668657.html

  • 相关阅读:
    有向强连通分支Tarjan算法
    让理科生沉默,让文科生落泪的题
    迷宫求解(数据结构栈应用)
    编程中无穷大常量的设定技巧
    poj 3159 差分约束+spfa
    codevs 1690 线段树
    uvalive 2756 环形排列颠倒的次数
    poj 2182 树状数组
    uva 10943 数学
    uva 10780 分解质因数
  • 原文地址:https://www.cnblogs.com/lmei/p/3352361.html
Copyright © 2011-2022 走看看