使用super从方法传递参数

问题描述 投票:0回答:2

我正在尝试在名为getphone的方法下设置一个参数role ='r'。它在init下使用super工作正常,但我无法弄清楚如何在另一种方法下完成它

角色是为正在运行的api设置权限级别

这段代码正在运行

PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'

class Credential:
    def __init__(self, *, path, platform, role='', **kwargs):
        super().__init__(**kwargs)
        self.role = role
        self.username_file = path + platform + role


class AXL(Credential):
    def __init__(self, *, item, **kwargs):
        super().__init__(role='rw', **kwargs)
        self.item = item

    def getphone(self):
        self.role = 'r'
        return self.username_file + self.item

    def writephone(self):
        self.role = 'rw'
        return self.username_file + self.item

    def statusphone(self):
        self.role = 'rwx'
        return self.username_file + self.item

reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)

print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())

在AXL类下,我想在方法getphone下移动role ='r'

我试过这个并且它正在工作,但我不明白为什么我需要把路径和平台。

PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'

class Credential:
    def __init__(self, *, path, platform, role='', **kwargs):
        super().__init__(**kwargs)
        self.role = role
        self.username_file = path + platform + role


class AXL(Credential):
    def __init__(self, *, item, **kwargs):
        super().__init__(**kwargs)
        self.item = item

    def getphone(self):
        super().__init__(path=PATH, platform=PLATFORM, role='r')
        return self.username_file + self.item

    def writephone(self):
        super().__init__(path=PATH, platform=PLATFORM, role='rw')
        return self.username_file + self.item

    def statusphone(self):
        super().__init__(path=PATH, platform=PLATFORM, role='rwx')
        return self.username_file + self.item


reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)
print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())

一个沙箱可以找到here

python oop superclass
2个回答
1
投票

没有一个与实例关联的角色;相反,该角色与实例调用的方法相关联。试试这个。

PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'

class Credential:
    def __init__(self, *, path, platform, **kwargs):
        super().__init__(**kwargs)
        self.username_file = path + platform


class AXL(Credential):
    def __init__(self, *, item, **kwargs):
        super().__init__(**kwargs)
        self.item = item

    # "Private" method used to implement the other phone methods
    # You could inline this if you want.
    def _phone(self, role):
        return self.username_file + role + self.item

    def getphone(self):
        return self._phone('r')

    def writephone(self):
        return self._phone('rw')

    def statusphone(self):
        return self._phone('rwx')

reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)

print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())

0
投票

super()将允许您访问父类范围。你可以在python3中以super().method(args)的形式访问它的任何方法,python 2的格式是super(YourClass,self).method(args)

从OOP的角度来看,在每个方法的父级上调用__init__似乎并不正确。您将在其生命周期内重新初始化父对象。如果你想从父母设置角色,你可以简单地使用self.role = ...。父访问self.role的任何方法都将看到修改。

© www.soinside.com 2019 - 2024. All rights reserved.