Php 如何使用抽象来安排多个类?
如何使用抽象来安排多个类 所以,将有一个基本抽象类,其他三个类将扩展基本抽象类 其思想是创建一个包含所有产品公共逻辑的抽象类, 如getTitle、setTitle等,然后为其创建子产品类 每个产品类型存储产品类型特定的逻辑,如家具 大小、CD大小、书籍重量等 基类-Main.phpPhp 如何使用抽象来安排多个类?,php,oop,pdo,abstract-class,Php,Oop,Pdo,Abstract Class,如何使用抽象来安排多个类 所以,将有一个基本抽象类,其他三个类将扩展基本抽象类 其思想是创建一个包含所有产品公共逻辑的抽象类, 如getTitle、setTitle等,然后为其创建子产品类 每个产品类型存储产品类型特定的逻辑,如家具 大小、CD大小、书籍重量等 基类-Main.php <?php include "classes/DB.php"; abstract class Main { protected $table; private $barcode;
<?php
include "classes/DB.php";
abstract class Main
{
protected $table;
private $barcode;
private $name;
private $price;
protected $size;
protected $height;
protected $width;
protected $length;
protected $weight;
private $image;
// SET Parametres
public function setBarcode($barcode)
{
$this->barcode = $barcode;
}
public function setName($name)
{
$this->name = $name;
}
public function setPrice($price)
{
$this->price = $price;
}
abstract function setSize($size);
abstract function setHeight($height);
abstract function setWidth($width);
abstract function setLength($length);
abstract function setWeight($weight);
public function setImage($image)
{
$this->image = $image;
}
// Create Data
public function insert()
{
$sql = "INSERT INTO $this->table(barcode, name, price, size, height, width, length, weight, image)VALUES(:barcode, :name, :price, :size, :height, :width, :length, :weight, :image)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':barcode', $this->barcode);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':size', $this->size);
$stmt->bindParam(':height', $this->height);
$stmt->bindParam(':width', $this->width);
$stmt->bindParam(':length', $this->length);
$stmt->bindParam(':weight', $this->weight);
$stmt->bindParam(':image', $this->image);
return $stmt->execute();
}
// Read Data
public function readAll()
{
$sql = "SELECT * FROM $this->table";
$stmt = DB::prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
// Delete Data
public function delete(array $id)
{
$placeholders = trim(str_repeat('?,', count($id)), ',');
$sql = "DELETE FROM $this->table WHERE id IN ($placeholders)";
$stmt = DB::prepare($sql);
return $stmt->execute($id);
}
}
?>
Disk.php
<?php
include "classes/Main.php";
class Disk extends Book
{
protected $table = 'products';
protected $size;
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
}
?>
<?php
include "classes/Main.php";
class Book extends Main
{
protected $table = 'products';
protected $weight;
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
}
?>
<?php
include "classes/Main.php";
class Furniture extends Disc
{
protected $table = 'products';
protected $height;
protected $width;
protected $length;
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
}
?>
Book.php
<?php
include "classes/Main.php";
class Disk extends Book
{
protected $table = 'products';
protected $size;
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
}
?>
<?php
include "classes/Main.php";
class Book extends Main
{
protected $table = 'products';
protected $weight;
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
}
?>
<?php
include "classes/Main.php";
class Furniture extends Disc
{
protected $table = 'products';
protected $height;
protected $width;
protected $length;
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
}
?>
Furniture.php
<?php
include "classes/Main.php";
class Disk extends Book
{
protected $table = 'products';
protected $size;
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
}
?>
<?php
include "classes/Main.php";
class Book extends Main
{
protected $table = 'products';
protected $weight;
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
}
?>
<?php
include "classes/Main.php";
class Furniture extends Disc
{
protected $table = 'products';
protected $height;
protected $width;
protected $length;
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
}
?>
在面向对象设计(OOD)中,首先是细节,然后是概括
换句话说---
步骤1:创建解决方案所需的类,在此阶段不考虑继承
步骤2:识别并将类的公共属性(“此处使用的一般意义上的属性”)从步骤1移动到基类中
步骤3:从步骤2中创建的基类继承步骤1中的类
这种方法可能有点困难,因为我们被训练以相反的方式设计类,但根据我的经验,它产生了更好的抽象。在面向对象设计(OOD)中,我们从细节开始,然后进行概括
换句话说---
步骤1:创建解决方案所需的类,在此阶段不考虑继承
步骤2:识别并将类的公共属性(“此处使用的一般意义上的属性”)从步骤1移动到基类中
步骤3:从步骤2中创建的基类继承步骤1中的类
这种方法可能有点困难,因为我们接受过相反的训练来设计类,但根据我的经验,它产生了更好的抽象。@DevelopmentStudio您的两个类中的整个继承思想,这个答案是有缺陷的。不能仅仅因为两本书都有一个标题就从书中扩展磁盘。通过这种方式,您将从蒸汽船扩展人类,因为两者都有一个名称。@DevelopmentStudio在您的两个类中都有完整的继承思想,这个答案是有缺陷的。不能仅仅因为两本书都有一个标题就从书中扩展磁盘。这样你就可以把一个人从蒸汽船上引申出来,因为两者都有一个名字。