且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

继承与将一个类的类对象用作另一个类的字段(Python 3)

更新时间:2023-01-08 14:37:11


这就是我上面通过继承实现的目标?

Is what I am trying to do above achieved via inheritance?

继承描述了是关系,因此问题是:1 /产品是质量吗?和2 /产品是维度吗?

Inheritance describes a "is a" relationship so the question are: 1/ is a Product a Quality ? and 2/ is a Product a Dimension ?

给出您自己的话:


我们有一个可以识别的产品由一个ID编号和一个由多个值组成的Quality字段组成,其中一个值为2d维值

we have a product which is identifiable by an ID number and a Quality field which is in turn comprised of several values, one of which is 2d dimensions values

这两个问题都是明确的否:您的产品不是质量,而是具有质量。这不是维度,而是质量具有维度。 IOW,您想要合成(您的代码段中已有的内容),而不是继承。

the answer to both questions is a clear "NO" : your product "is" not a quality, it "has" a quality. And it's not a dimension, it's the quality which has a dimension. IOW, you want composition (what you already have in your snippet), not inheritance.


也许我的问题很幼稚,但我正在尝试了解Python的类继承和结构方法

Perhaps my question is naive but I am trying to understand Python's approach of Class inheritance and structure

这与python无关,只是基本的OO设计。 著名的设计模式书的第一部分(原始的)可能是我所了解的有关OO设计的***的文章之一。

This is nothing python-specific, just basic OO design. The first part of the famous "Design patterns" book (the original one) is possibly one of the best texts I know about OO design.


如果您还可以完成我的代码段来说明类的组成方式(在本示例中可以说明定义?例如,当实例化Product实例时,我将提供其组成的其他类的值吗?

If you could also complete my code snippet to illustrate how class composition (definition could be illustrated in this example? e.g. when instantiating a Product instance, will I provide the values for the other classes it is composed from?

这里没有一个万能的规则,它取决于上下文,尤其取决于各种对象的生命周期,以最简单的形式,您明确实例化每个对象并将它们彼此传递,即:

There's no one-size-fits-all rule here, it depends on the context, and specially on the various objects lifecycle. In it's simplest form, you explicitely instanciate each objects and pass them to each other, ie:

class Dimensions:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Quality:
    def __init__(self, val1, val2, dimensions):
        self.val1 = val1
        self.val2 = val2
        self.dimensions = dimensions

class Product:
    def __init__(self, id, quality):
        self.id = id
        self.quality = quality


dimensions = Dimensions(1, 2)
quality = Quality("A", "B", dimensions)
product = Product(42, quality)

在频谱的另一端,您将所有原始值传递给产品,从而创建产品的质量并定义其尺寸:

At the other end of the spectrum, you pass all "raw" values to the product, which creates it's quality, which defines it's dimensions:

class Dimensions:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Quality:
    def __init__(self, val1, val2, x, y):
        self.val1 = val1
        self.val2 = val2
        self.dimensions = Dimensions(x, y)

class Product:
    def __init__(self, id, val1, val2, x, y):
        self.id = id
        self.quality = Quality(val1, val2, x, y)


product = Product(42, "A", "B", 1, 2)

使用任何变体,甚至都使用它们来提供备用构造函数。这里的主要力量是尺寸和/或质量是否应在产品外部具有生命,以及是否应在产品之间共享。

and you can of course use any variant, and even use them all providing alternate constructors. The main forces here is are whether dimensions and/or quality should have a life outside products, and whether they should or not be shared amongst products.