继承与将一个类的类对象用作另一个类的字段(Python 3) [英] Inheritance vs using a class object of one class as a field of another class (Python 3)

查看:186
本文介绍了继承与将一个类的类对象用作另一个类的字段(Python 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个产品,该产品可以通过ID号和质量字段进行识别,而质量字段又包含多个值,其中一个是2d维值。这是继承还是仅将对象用作另一个类的字段

Say 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. Is this a case of inheritance or just using an object as another classes' field?

在尝试使用OOP和我想到了以下类:

In trying to implement the above in Python using OOP and classes I thought of the following:

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

我上面想要做的事情是通过继承实现的吗?

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

例如 class Product(Quality):

如果是这样,在上面的Python中定义类和我将如何传递父类的特定实例的字段(假设 Dimensions Quality 的父类,等等)

If so, how it would look like in defining the classes above in Python and how would I be passing the fields of a specific instance of a parent class (assuming Dimensions is the parent of Quality etc.)?

也许我的问题很幼稚,但我试图理解Python的Class继承和结构方法;在网上搜索了Python中的示例,但找不到一个可以帮助我理解的示例。如果这在其他地方有答案,请共享链接,或者指向我正确的方向。

Perhaps my question is naive but I am trying to understand Python's approach of Class inheritance and structure; searched online for examples in Python but could not find one to help me understand. If this has an answer elsewhere, please share the link or, instead, point me to the right direction.

通过定义类并实例化每个类来完成上面的代码示例

Completing the example above in code from defining the classes and instantiating each of them would be much helpful.

推荐答案


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

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.

这篇关于继承与将一个类的类对象用作另一个类的字段(Python 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆