从其他子类重载父类

时间:2021-11-21 16:12:23

I am not sure if i get the point of OOP but this is my idea:

我不确定我是否明白了OOP,但这是我的想法:

<?php

class Car {
    public $color;

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

class FactoryAudi extends Car {
    public makeTheCar() {   
        //...
    }

    public getParent() {    
        return $this->parent;
    }
}

class Audi extends Car {

    public function setParent($parent) {    
        $this->parent = $parent;
    }
}

$newAudi = FactoryAudi();
$newAudi->makeCar();

$audi = new Audi();
$audi->setParent($newAudi->getParent());

I want to overload the parent of a new subclass with the parent ob a different subclass (same parent class). My idea is to preserved the data i have already worked on, but move it to an other class so i can use this data. I my head it just makes sense, but PHP will not let me do is this way. Is it possible to overload the parent with the same type of parent from an other class?

我想重载一个新子类的父类,父ob是一个不同的子类(相同的父类)。我的想法是保留我已经处理过的数据,但是将它移到另一个类,这样我就可以使用这些数据了。我的头脑它只是有道理,但PHP不会让我这样做。是否可以使用来自其他类的相同父类重载父类?

1 个解决方案

#1


0  

FactoryAudi should not extend car. Inheritance, while a means of Aggregation, is used for the ISA relationship. Thus, an "Audi" ISA "car", but an automobile factory fails the ISA test for being a car.

FactoryAudi不应该延长汽车。继承虽然是聚合的手段,但用于ISA关系。因此,一辆“奥迪”ISA“汽车”,但一家汽车厂未通过ISA测试成为一辆汽车。

Also, since inheritance does 'aggregate' you don't need the 'parent' relationships either.

此外,由于继承确实“聚合”,您也不需要“父”关系。

#1


0  

FactoryAudi should not extend car. Inheritance, while a means of Aggregation, is used for the ISA relationship. Thus, an "Audi" ISA "car", but an automobile factory fails the ISA test for being a car.

FactoryAudi不应该延长汽车。继承虽然是聚合的手段,但用于ISA关系。因此,一辆“奥迪”ISA“汽车”,但一家汽车厂未通过ISA测试成为一辆汽车。

Also, since inheritance does 'aggregate' you don't need the 'parent' relationships either.

此外,由于继承确实“聚合”,您也不需要“父”关系。