高级特性
包括:
1.静态方法和属性(通过类而不是对象来访问数据和功能)
2.抽象类和接口(设计,实现分离)
3.错误处理(异常)
4.Final类和方法(限制继承)
5.拦截器(自动委托)
6.析构方法(对象销毁前的清理工作)
7.克隆对象(创建对象的副本)
8.把对象解析成字符串

PS,学会从内存的角度看代码。想象计算机的微观世界。

静态方法的小例子

<?php
class StaticExample{
static public $aNum = 10;
static public function sayHello(){
print "hello";
}
}
print StaticExample::$aNum."<br/>";
StaticExample::sayHello();

tips:
1.静态方法不能访问类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性。
2.我们不能再对象中调用静态方法,因此不能再静态方法中使用伪变量$this。

静态方法的大例子

<?php
class ShopProduct{
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
private $id = 0;
function __construct($title,$firstName,$mainName,$price){
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
} public function setID($id){
$this->id = $id;
}
public static function getInstance($id,PDO $pdo){
$query = "select * from products where id= '$id'";
$stmt = $pdo->query($query);
$row = $stmt->fetch();
if(empty($row)){
return null;
}
if($row['type'] == "book"){
$product = new BookProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['numpages']
);
}else if($row['type'] == "cd"){
$product = new CdProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['playLength']
);
}else{
$product = new ShopProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price']
);
}
$product->setId($row['id']);
$product->setDiscount($row['discount']);
return $product;
} public function getProducerFirstName(){
return $this->producerFirstName;
} public function getProducerMainName(){
return $this->producerMainName;
} public function setDiscount($num){
$this->discount = $num;
} public function getDiscount(){
return $this->discount;
} public function getTitle(){
return $this->title;
} public function getPrice(){
return ($this->price - $this->discount);
} function getProducer(){
return $this->producerFirstName." ".$this->producerMainName;
} function getSummaryLine(){
$base = "$this->title({$this->producerMainName},";
$base .= "{$this->producerFirstName})";
return $base;
}
} class CdProduct extends ShopProduct{
private $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{
private $numPages = 0;
function __construct($title,$firstName,$mainName,$price,$numPages){
parent::__construct($title,$firstName,$mainName,$price);
$this->numPages = $numPages;
}
function getnumPages(){
return $this->numPages;
} function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":page count {$this->numPages}";
return $base;
}
} $dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db";
$pdo = new PDO($dsn,null,null);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$obj = ShopProduct::getInstance(1,$pdo);
echo $obj->getSummaryLine();

最新文章

  1. NancyFX 简介
  2. 1. ReactNative 基础
  3. android native crash 分析
  4. ubuntu16.4下用jexus部署asp.net core rtm
  5. Wireshark找不到网络接口问题
  6. [转]使用ant让Android自动打包的build.xml,自动生成签名的apk文件(支持android4.0以上的版本)
  7. Spring MVC中注解 @ModelAttribute
  8. JS逻辑运算大于小于比较
  9. springmvc.xml或spring.xml 能运行配置文件总是出现错误
  10. Archive for required library:xxxxx/spring-beans-3.2.4.RELEASE.jar in project XXXXX cannot be read or is not a valid ZIP file
  11. js 循环 常用方法
  12. 2.supervisor实时监控程序存活状态
  13. 2017-06-27(useradd usermod userdel 禁止普通用户登录)
  14. 【java-console】如何双击运行可执行jar包及遇到依赖dll报错问题的解决办法
  15. 二进制样式的字符串与byte数组互转函数示例
  16. MySQL常用查询语句积累
  17. FIDDLER的使用方法及技巧总结(连载三)FIDDLER使用技巧及方法
  18. Qt封装QTcpServer参考资料--QTcpServer多线程实现
  19. SourceTree跳过注册安装使用
  20. Yarn架构

热门文章

  1. CSS打造固定表头
  2. spring3: AOP 之切面实例化模型 ——跟我学spring3
  3. 【LABVIEW到C#】4》String的操作之Search and Replace.vi
  4. linux中的kill命令
  5. 三个大数据处理框架:Storm,Spark和Samza 介绍比较
  6. 转:走近NoSQL数据库的四大家族
  7. XML的两种解析方式
  8. LeNet-5网络结构及训练参数计算
  9. (四)java基本语法
  10. Android中的service