新浦京81707con > 注册购买 > 奥门新浦京:PHP面向对象程序设计OOP继承用法入

原标题:奥门新浦京:PHP面向对象程序设计OOP继承用法入

浏览次数:179 时间:2019-08-07

本文实例呈报了PHP面向对象承袭用法。分享给大家供咱们参照他事他说加以考察,具体如下:

正文实例陈说了PHP面向对象程序设计高端天性。分享给大家供大家仿效,具体如下:

本文实例呈报了PHP面向对象程序设计OOP承继用法。分享给我们供我们参考,具体如下:

继承

静态属性

<?php
class Person {
  var $name;//protected
  var $sex;
  var $age;
  function __construct($name = "", $sex = "男", $age = 22) {
    $this->name = $name;
    $this->sex = $sex;
    $this->age = $age;
  }
  function say() {
    echo $this->name . "在说话<br/>";
  }
  function run() {
    echo "在走路·<br/>";
  }
}
class Student extends Person {
  var $school;
  function __construct($name = "", $sex = "男", $age = 22,$school="") {
    parent::__construct($name,$sex,$age);
    $this->school = $school;
  }
  function study() {
    echo $this->name."正在".$this->school."学习<br/>";
  }
}
class Teacher extends Student {
  var $wage;
  function teaching() {
    echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."<br/>";
  }
}
$teacher1 = new Teacher("kaifu","男",22);
$teacher1->school = "edu";
$teacher1->wage = 4000;
$teacher1->say();
$teacher1->study();
$teacher1->teaching();
?>

先看八个类

<?php
class StaticExample {
  static public $aNum = 0; // 静态共有属性
  static public function sayHello() { // 静态共有方法
    print "hello";
  }
}
print StaticExample::$aNum;
StaticExample::sayHello();
?>

结果:

<?php
class CdProduct {
  public $playLength; // 播放时间
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $playLength ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->playLength    = $playLength;
  }
  function getPlayLength() {
    return $this->playLength;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
}
class BookProduct {
  public $numPages; // 看的页数
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $numPages ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->numPages     = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "n";
?>

输出:0    hello

kaifu在说话
kaifu正在edu学习
kaifu正在edu教学,每月工资为4000

输出:

点评:静态属性和格局,能够透过类间接调用。

愈来愈多关于PHP相关内容感兴趣的读者可查看本站专项论题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法计算》、《PHP互连网编制程序技巧计算》、《PHP数组(Array)操作能力大全》、《php字符串(string)用法计算》、《php mysql数据库操作入门教程》及《php常见数据库操作才能汇总》

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

SELF

企望本文所述对大家PHP程序设计有所扶助。

点评:那三个类,代码重复性太高,有同样性,也可以有差距性。不比用持续来简化管理。

<?php
class StaticExample {
  static public $aNum = 0;
  static public function sayHello() { // 这里的static 和 public的顺序可以颠倒
    self::$aNum  ;
    print "hello (".self::$aNum.")n"; // self 指向当前类, $this指向当前对象。
  }
}
StaticExample::sayHello();
StaticExample::sayHello();
StaticExample::sayHello();
?>

你恐怕感兴趣的文章:

  • PHP面向对象程序设计高档天性详解(接口,承接,抽象类,析构,克隆等)
  • PHP入门教程之面向对象的特色解析(继承,多态,接口,抽象类,抽象方法等)
  • php面向对象全战术 (十四) php5接口技巧
  • PHP面向对象程序设计之接口用法
  • PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编制程序
  • PHP面向对象的进级学习(抽像类、接口、final、类常量)
  • php学习笔记 面向对象中[接口]与[多态性]的应用
  • PHP面向对象三大特点学习(足够领悟抽象、封装、承袭、多态)
  • php面向对象全计策 (七) 承继性
  • PHP面向对象承接用法详解(优化与减弱代码重复)
  • PHP接口承袭及接口多接二连三原理与贯彻格局详解

接纳承接来拍卖

输出:

<?php
class ShopProduct {
  public $numPages;
  public $playLength;
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $numPages=0, $playLength=0 ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->numPages     = $numPages;
    $this->playLength    = $playLength;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "$this->title ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  function getPlayLength() { // 增加属于自己的方法
    return $this->playLength;
  }
  function getSummaryLine() { // 改造了父类的方法
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, null, 50 );
print $product1->getSummaryLine();
print "n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "n";
?>
hello (1)
hello (2)
hello (3)

输出:

点评:self 指向当前类, this指向当前指标。self能够调用当前类的静态属性和情势。this指向当前指标。self能够调用当前类的静态属性和措施。this能够调用当前类的例行属性和方式。

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

常量属性

点评:承接管理很好的缓慢解决了差距性,相通性难题。

<?php
class ShopProduct {
  const AVAILABLE   = 0; // 只能用大写字母命名常量
  const OUT_OF_STOCK  = 1;
  public $status;
}
print ShopProduct::AVAILABLE;
?>

进而优化管理

输出:0

<?php
class ShopProduct {
  // 抽离出共有属性
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  // 抽离出属于自己特有的属性
  public $playLength;
  function __construct(  $title, $firstName,
              $mainName, $price, $playLength ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price ); // 继承父类的构造函数
    $this->playLength = $playLength; // 处理自己专有的属性
  }
  function getPlayLength() {
    return $this->playLength;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  public $numPages;
  function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "$this->title ( $this->producerMainName, ";
    $base .= "$this->producerFirstName )";
    $base .= ": page count - $this->numPages";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "n";
?>

点评:常量只可以用大写字母,况兼能够经过类直接调用。

输出:

接口

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

<?php
interface Chargeable { // 接口,抽象类是介于基类与接口之间的东西
  public function getPrice();
}
class ShopProduct implements Chargeable {
  // ...
  protected $price;
  // ...
  public function getPrice() {
    return $this->price;
  }
  // ...
}
$product = new ShopProduct();
?>

点评:这里把共有属性在父类中,其余性格属性放在自身的类中拍卖。并设置自个儿的构造方法,承继父类的构造方法。

借使未有实现getPrice方法,将会报错。

一发承袭父类的点子

Fatal error: Class ShopProduct contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Chargeable::getPrice)

<?php
class ShopProduct {
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  public $playLength;
  function __construct(  $title, $firstName,
              $mainName, $price, $playLength ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->playLength = $playLength;
  }
  function getPlayLength() {
    return $this->playLength;
  }
  function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  public $numPages;
  function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "n";
?>

承袭类与接口

本文由新浦京81707con发布于注册购买,转载请注明出处:奥门新浦京:PHP面向对象程序设计OOP继承用法入

关键词: 新浦京81707con

上一篇:js制作简单爬虫教程,利用cheerio制作简单的网页

下一篇:没有了