Python:从减法/加法构造类

Python:从减法/加法构造类,python,python-3.x,class,inheritance,constructor,Python,Python 3.x,Class,Inheritance,Constructor,我试图从两个父对象生成一个子类,它是作为其和/减运算构建的。我将尝试用一个简单的例子来最好地解释 图片我有以下父类 class UpperBody(): def __init__(self, owner='John', age=25, max_lifting_weight=100): self.owner=owner # Identifier, who "owns" this upper body self.age=age #

我试图从两个父对象生成一个子类,它是作为其和/减运算构建的。我将尝试用一个简单的例子来最好地解释

图片我有以下父类

class UpperBody():
    
    def __init__(self, owner='John', age=25, max_lifting_weight=100):
        self.owner=owner # Identifier, who "owns" this upper body
        self.age=age # Second identifier
        self.max_lifting_weight=max_lifting_weight
    
    def __add__(self, other):
        #__sub__ would be equivalent
        if other.age!=self.age:
            raise AttributeError('You cannot add body parts with different ages!')
        if other.owner!=self.owner:
            print('This does not seem to fit here, but alright...')
            return UpperBody(self.owner + '_' + other.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
        
        return UpperBody(self.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
            
    def can_lift(self, weight):
        # Returns boolean
        return weight < self.max_lifting_weight


class LowerBody():
    
    def __init__(self, owner='John', age=25, max_lifting_weight=100):
        self.owner=owner # Identifier, who "owns" this lower body
        self.age=age # Second identifier
        self.max_lifting_weight=max_lifting_weight
    
    def __add__(self, other):
        #__sub__ would be equivalent
        if other.age!=self.age:
            raise AttributeError('You cannot add body parts with different ages!')
        if other.owner!=self.owner:
            print('This does not seem to fit here, but alright...')
            return UpperBody(self.owner + '_' + other.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
        
        return UpperBody(self.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
            
    def can_lift(self, weight):
        # Legs are stronger
        return weight < self.max_lifting_weight * 1.2

然后访问在父类中定义的方法,如can_lift

class UpperBody():
    
    def __init__(self, owner='John', age=25, max_lifting_weight=100):
        self.owner=owner # Identifier, who "owns" this upper body
        self.age=age # Second identifier
        self.max_lifting_weight=max_lifting_weight
    
    def __add__(self, other):
        #__sub__ would be equivalent
        if other.age!=self.age:
            raise AttributeError('You cannot add body parts with different ages!')
        if other.owner!=self.owner:
            print('This does not seem to fit here, but alright...')
            return UpperBody(self.owner + '_' + other.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
        
        return UpperBody(self.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
            
    def can_lift(self, weight):
        # Returns boolean
        return weight < self.max_lifting_weight


class LowerBody():
    
    def __init__(self, owner='John', age=25, max_lifting_weight=100):
        self.owner=owner # Identifier, who "owns" this lower body
        self.age=age # Second identifier
        self.max_lifting_weight=max_lifting_weight
    
    def __add__(self, other):
        #__sub__ would be equivalent
        if other.age!=self.age:
            raise AttributeError('You cannot add body parts with different ages!')
        if other.owner!=self.owner:
            print('This does not seem to fit here, but alright...')
            return UpperBody(self.owner + '_' + other.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
        
        return UpperBody(self.owner, self.age, self.max_lifting_weight + other.max_lifting_weight)
            
    def can_lift(self, weight):
        # Legs are stronger
        return weight < self.max_lifting_weight * 1.2

我能想出的最好办法就是通过函数

def HumanConstructor(UpperBody, LowerBody):
    return UpperBody + LowerBody
然而,如果Human类有额外的方法,这将不会有多大帮助。有没有办法做到这一点


事先谢谢

您基本上只需要正确初始化Human类中的子类:

类人(上身、下身):
定义初始(自我、姓名、年龄、最大起重重量上限、最大起重重量下限):
self.name=name
自我。年龄=年龄
#创建子类实例
上半身。uuu初始(姓名、年龄、最大起重量u上半身)
下部底座。初始(姓名、年龄、最大提升重量、上部)
def do_某事(自我):
上半身。可以抬起吗
下基座。可提升()

我会读一读这个伟大的多重继承文档-哇,这么简单,但它却在我脑海中闪过,我已经在制作超级复杂的结构了。谢谢!