首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:用减法/加法构造类

Python:用减法/加法构造类
EN

Stack Overflow用户
提问于 2020-11-24 12:26:07
回答 1查看 109关注 0票数 0

我试图从双亲生成一个子类,这个类是作为它的和/减构建的。我将试着用一个简单的例子最好地解释。

图为我有以下父类。

代码语言:javascript
复制
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

我想通过来生成Human子类,添加这两个父类。这样的东西,

代码语言:javascript
复制
class Human(UpperBody, LowerBody):
    
    def __init__(self, name, age, max_lifting_weight_upper, max_lifting_weight_lower):
        self.name=name
        self.age=age
        # From here on, I have no clue how to follow. This does not load to "self", obviously.
        UpperBody.__init__(name, age, max_lifting_weight_upper) + LowerBody.__init__(name, age, max_lifting_weight_upper)

以及稍后访问在父类中定义的方法(如can_lift )。

我能想到的最好的方法就是通过一个函数,

代码语言:javascript
复制
def HumanConstructor(UpperBody, LowerBody):
    return UpperBody + LowerBody

但是,如果Human有额外的方法,这将不会有太大的帮助。有办法这样做吗?

事先谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-24 12:41:01

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

代码语言:javascript
复制
class Human(UpperBody, LowerBody):
    
    def __init__(self, name, age, max_lifting_weight_upper, max_lifting_weight_lower):
        self.name=name
        self.age=age

        # create the child class instances
        UpperBody.__init__(name, age, max_lifting_weight_upper)
        LowerBody.__init__(name, age, max_lifting_weight_upper)

    def do_something(self):
        UpperBody.can_lift()
        LowerBody.can_lift()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64986485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档