0

首先,原谅我的英语。

我正在尝试在 python 中执行一个简单的类应用程序。这个概念是这样的。

    class Auth:
            def __init__(self, username, password):
                self.username = username
                self.password = password
            def connect(self):
                """here is the code for connecting into X server"""
                pass

    class Manager(Auth):
        """used for search auctions, for example"""
        def __init__(self, username, password):
            Auth.__init__(self, username, password)
        def pile(self):
            y = self.some_search_into_a_server()
            for x in y:
                yield Auction(x)
        def buy(self):
            """buy method for using by auction"""
            pass

    class Auction:
        def __init__(self, auction):
            self.id = auction["id"]
            self.some_data = auction["some_data"]

好吧,这里的重点是我想做的是这样的:

manager = Manager('user@example.com', 'a_very_secure_password')
for auction in manager.pile():
    auction.buy()

这里的问题是:我怎样才能实现 Manager 从 Auction 继承并且 Aucion 使用方法 buy?

4

1 回答 1

2

如果 XY ,那么您需要继承,例如动物,或者贵宾犬

如果 XY,那么您需要会员资格,例如主人,或者商店顾客

基于拍卖经理这一事实。您可能想要成员资格而不是继承

class Auction:
    def __init__(self, manager,auction):
        self.id = auction["id"]
        self.manager = manager
        self.some_data = auction["some_data"]
    def buy(self):
          return self.manager.buy()

class Manager(Auth):
    """used for search auctions, for example"""
    def __init__(self, username, password):
        Auth.__init__(self, username, password)
    def pile(self):
        y = self.some_search_into_a_server()
        for x in y:
            yield Auction(self,x)
    def buy(self):
        """buy method for using by auction"""
        pass

manager = Manager('user@example.com', 'a_very_secure_password')
for auction in manager.pile():
    auction.buy()
于 2012-10-04T17:29:44.450 回答