【问题标题】:How can I arrange multiple classes with abstraction?如何安排多个具有抽象的类?
【发布时间】:2020-02-03 12:10:19
【问题描述】:

如何用抽象来安排多个类?

因此,将有一个 Base 抽象类,其他三个类将扩展 Base 抽象类。

这个想法是创建一个具有所有产品通用逻辑的抽象类, 像 getTitle、setTitle 等。然后创建子产品类 每个产品类型来存储产品类型特定的逻辑,比如家具 尺寸、CD 尺寸、书重等。

基类 - Main.php

<?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;
    }
}

?>

Book.php

<?php
include "classes/Main.php";

class Book extends Main
{
    protected $table = 'products';

    protected $weight;

    // SET Parametre
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
}

?>

Furniture.php

<?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;
    }

}

?>

【问题讨论】:

    标签: php oop pdo abstract-class


    【解决方案1】:

    在面向对象设计 (OOD) 中,从细节开始,然后进行概括。

    换句话说---

    第 1 步:创建解决方案所需的类,在此阶段无需考虑继承问题。

    第 2 步:识别第 1 步中的类的通用属性(此处使用的一般意义上的“属性”)并将其移动到基类中。

    步骤 3:从步骤 2 中创建的基类继承步骤 1 中的类。

    这种方法可能有点困难,因为我们接受过反过来设计类的训练,但根据我的经验,它产生了更好的抽象。

    【讨论】:

    • @DevelopmentStudio 两个类中的整个继承思想,这个答案是有缺陷的。您不会仅仅因为一本书都有一个标题而从一本书中扩展磁盘。这样你就可以从蒸汽船上扩展一个人,因为两者都有一个名字。
    猜你喜欢
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2011-03-20
    相关资源
    最近更新 更多