PHP抽象类

2018-02-22 16:40 更新

PHP教程 - PHP抽象类

抽象类定义了抽象概念,例如数字是一个抽象概念而int,字节是具体的概念。 车辆是抽象概念,当汽车和船是具体概念时。

抽象类是继承创建一个新的,非抽象(具体)类。

通过使父类抽象,你定义规则什么子类必须包含的方法。

父类中的抽象方法实际上不具有方法中的任何代码。

它留给了孩子类。它指定了子类必须做什么,而不是如何做。



句法

要声明抽象方法,只需使用abstract关键字,如下所示:

abstract public function myMethod( $param1, $param2 );

我们可以选择指定方法必须包含的任何参数。

然而,你不包括实现该方法的任何代码,也不指定方法必须返回的值的类型。

如果你声明一个类的一个或多个方法是抽象的,你还必须声明整个类抽象:

abstract class MyClass { 
    abstract public function myMethod( $param1, $param2 ); 
}   


注意

你可以实例化一个抽象类,也就是说,直接从它创建一个对象:

// Generates an error: "Cannot instantiate abstract class MyClass" 
$myObj = new MyClass;   

实施例1


<?PHP
abstract class Book {
        private $Name;
        public function getName() {
            return $this->Name;
        }
}
?>

如前所述,你也可以使用abstract关键字和方法,但是如果一个类有至少一个抽象方法,则该类本身必须被声明抽象。

如果你试图在抽象中提供任何代码,你会得到错误方法,这使得这是非法的:

<?PHP
abstract class Book {
        abstract function say() {
                print "Book!";
        }
}
?>

这也是非法的:

<?PHP
abstract class Book {
        abstract function say() { }
}
?>

相反,一个适当的抽象方法应该是这样的:

<?PHP
abstract class Book {
        abstract function say();
}
?>

实施例2

下面的代码显示了如何提供抽象类的实现。


//  w  w w .  java  2  s  .  c  om
<?php

abstract class Shape {
  private $_color = "black";
  private $_filled = false;

  public function getColor() {
    return $this->_color;
  }

  public function setColor( $color ) {
    $this->_color = $color;
  }

  public function isFilled() {
    return $this->_filled;
  }

  public function fill() {
    $this->_filled = true;
  }

  public function makeHollow() {
    $this->_filled = false;
  }

  abstract public function getArea();
}

class Circle extends Shape {
  private $_radius = 0;

  public function getRadius() {
    return $this->_radius;
  }

  public function setRadius( $radius ) {
    $this->_radius = $radius;
  }

  public function getArea() {
    return M_PI * pow( $this->_radius, 2 );
  }
}

class Square extends Shape {
  private $_sideLength = 0;

  public function getSideLength() {
    return $this->_sideLength;
  }

  public function setSideLength( $length ) {
    $this->_sideLength = $length;
  }

  public function getArea() {
    return pow( $this->_sideLength, 2 );
  }
}

class Rectangle extends Shape {
  private $_width = 0;
  private $_height = 0;

  public function getWidth() {
    return $this->_width;
  }

  public function getHeight() {
    return $this->_height;
  }

  public function setWidth( $width ) {
    $this->_width = $width;
  }

  public function setHeight( $height ) {
    $this->_height = $height;
  }

  public function getArea() {
    return $this->_width * $this->_height;
  }
}


class ShapeInfo {
  private $_shape;

  public function setShape( $shape ) {
    $this->_shape = $shape;
  }

  public function showInfo( ) {
    echo "<p>The shape"s color is " . $this->_shape->getColor();
    echo ", and its area is " . $this->_shape->getArea() .".</p>";
  }
}


$myCircle = new Circle;
$myCircle->setColor( "red" );
$myCircle->fill();
$myCircle->setRadius( 4 );

$mySquare = new Square;
$mySquare->setColor( "green" );
$mySquare->makeHollow();
$mySquare->setSideLength( 3 );

$info = new ShapeInfo();

$info->setShape( $myCircle );
$info->showInfo();
$info->setShape( $mySquare );
$info->showInfo();

$myRect = new Rectangle;
$myRect->setColor( "yellow" );
$myRect->fill();
$myRect->setWidth( 4 );
$myRect->setHeight( 5 );

$info->setShape( $myRect );
$info->showInfo();

?>

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号