• 抽象类、扩展类、实例化类


    1.抽象类

    使用关键字:abstract

    没有为它所声明的所以方法定义实现的内容(即函数体),可将抽象类看作部分类。抽象类可以不实现所有方法,它具有定义抽象方法(即为,只有声明,没有函数体的方法)的能力。当抽象类被继承时,这些方法将会被实现。换言之,在继承抽象类的扩展类中,同名方法带有函数体。另外,抽象类中,也可以定义除抽象方法以外的、具有函数体的完整方法。

    //定义一个抽象类Car
    abstract class Car{
        abstract function getMaximumSpeed();
    }

    在类的声明中使用abstract修饰符,就可以将某个类声明为抽象的。

    说明:public function abc();即为方法声明,是方法的定义中,剔除了方法体之后的签名,这个声明称为方法的原型。

    2.继承类

    使用关键字:extends

    这里只讲继承抽象类的继承类。

    继承类,也就是扩展类。它能继承父类的方法和部分变量,并使用。

    继承抽象类的继承类(扩展类)必须将抽象类中的抽象方法具体完整地实现(即写出函数体)。

    //定义一个扩展抽象类的类FastCar
    class FastCar extends Car{
        public function getMaximumSpeed(){
            return 150;
        }
    }
    

    再定义一个类,使用抽象类提供的公共功能

    //使用抽象类提供的公共功能
    class Street{
        protected $speedLimit;
        protected $cars;
        
        public function __construct($speedLimit=200){
            $this->cars=array();
            $this->speedLimit= $speedLimit;
        }
        
        protected function isStreetLegal($car){
            if ($car->getMaximumSpeed()< $this->speedLimit ) {
                return true;
            }else{
                return false;
            }
        }
        
        public function addCar($car){
            if($this->isStreetLegal($car)){
            	echo 'The car was allowed on the road.';
                $this->cars[]=$car;
            }else{
            	echo 'The car is too fast and was not allowed on the road.';
            }
        }
    }

    3.实例化

    使用关键字:new

    实例化的形式

    $abc=new abc();

    调用的形式

    $abc->XXX();

    注意:抽象类不能被实例化,因为其中的抽象方法没有函数体。所以只有通过实例化抽象类的扩展类来使用抽象类中的方法。

        $street=new Street;
        $fastCar=new FastCar;
        $street->addCar($fastCar);
    

    ======完整代码=======

     1 <?php
     2 //定义一个抽象类Car
     3 abstract class Car{
     4     abstract function getMaximumSpeed();
     5 }
     6 
     7 //定义一个扩展抽象类的类FastCar
     8 class FastCar extends Car{
     9     public function getMaximumSpeed(){
    10         return 150;
    11     }
    12 }
    13 
    14 //使用抽象类提供的公共功能
    15 class Street{
    16     protected $speedLimit;
    17     protected $cars;
    18     
    19     public function __construct($speedLimit=200){
    20         $this->cars=array();
    21         $this->speedLimit= $speedLimit;
    22     }
    23     
    24     protected function isStreetLegal($car){
    25         if ($car->getMaximumSpeed()< $this->speedLimit ) {
    26             return true;
    27         }else{
    28             return false;
    29         }
    30     }
    31     
    32     public function addCar($car){
    33         if($this->isStreetLegal($car)){
    34             echo 'The car was allowed on the road.';
    35             $this->cars[]=$car;
    36         }else{
    37             echo 'The car is too fast and was not allowed on the road.';
    38         }
    39     }
    40 }
    41     $street=new Street;
    42     $fastCar=new FastCar;
    43     $street->addCar($fastCar);
    44 ?>
    View Code
  • 相关阅读:
    HDU 3605 Escape 最大流
    HDU 3416 Marriage Match IV (最短路径&&最大流)
    洛谷1508 简单记忆化搜索
    洛谷1880 区间dp+记忆化搜索 合并石子
    洛谷1063 +区间dp(经典问题)
    洛谷1074 靶状数独dfs 排序、记录、搜索
    hdu3368 dfs 下棋
    hdu1258 dfs 给一个指定的target数和一个数列,要求不重复选择其中的数使得和为target并打印,结果不可重复。
    hdu1181 dfs 字符串首尾可拼接,问是否可寻找到一条字串路径使得首尾分别是‘b’和‘m’,简单的搜索+回溯
    hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)
  • 原文地址:https://www.cnblogs.com/andy9468/p/4238129.html
Copyright © 2020-2023  润新知