Python-祖父母方法(基因)
我目前正在制作一个简单的脚本,让我通过访问服务器控制多个(32)交换机和路由器。我已经创建了一个类来启动串行连接 无论如何,我的问题是如何使用继承的方法?我有一个祖父母设备,有两个孩子(父亲)路由器和交换机。这两个孩子成为了几个孩子的父亲,让我们用SwitchA SwitchB&RouterA简单地说吧。现在在Cisco设备中,一些配置是“标准”的,但不是全部。假设我想通过串口进入“配置终端” 重点:Python-祖父母方法(基因),python,class,oop,object,Python,Class,Oop,Object,我目前正在制作一个简单的脚本,让我通过访问服务器控制多个(32)交换机和路由器。我已经创建了一个类来启动串行连接 无论如何,我的问题是如何使用继承的方法?我有一个祖父母设备,有两个孩子(父亲)路由器和交换机。这两个孩子成为了几个孩子的父亲,让我们用SwitchA SwitchB&RouterA简单地说吧。现在在Cisco设备中,一些配置是“标准”的,但不是全部。假设我想通过串口进入“配置终端” 重点: class Devices(object): 'Grandparent Class for C
class Devices(object):
'Grandparent Class for Cisco Devices'
def __init__(self, a):
self.__a = a
def enterConfT(self):
self.__a.send( "\r" )
self.__a.send("enable\r")
print("enabled")
self.__a.send( "config terminal\r" )
print("Entered global configuration mode.")
class Switches(Devices):
'Switches Parent?'
def __init__(self):
pass
def do_nothing_yet(self):
pass
class switchA(Switches):
'Catalyst 3850 Teletest'
def __init__(self, x):
self.__x = x
在另一个文件中,我得到:
y = TClasses.cisco.test.switchA(serial1)
y.enterConfT()
这会产生以下异常/错误(我取出了文件目录):
“switchA”对象没有属性“\u Devices\u\a”
['Traceback(最近一次调用):\n','File'/sorry\u privacy/test.py',第30行,in\n y.enterConfT()\n','File'/sorry\u hehe/TClasses.py',第24行,in enterConfT\n self.\u a.send(\\r”)\n','AttributeError:'switch a'对象没有属性'\u Devices\u a'\n']
我希望能够在变量a和x指向同一对象时保持它们的私有性。
我从OOP和C++中知道,最小化重复代码,我在C++中似乎没有祖辈继承问题,但我知道Python工作方式不同。我也读了一些问答,但不能真正理解它们的意思。我是Python的初学者
提前谢谢你,请原谅我的英语。找到了答案。在init中使用super()可以从父级初始化 这是很有帮助的,因为我倾向于读得非常快和全面。
'switchA' object has no attribute '_Devices__a'
['Traceback (most recent call last):\n', ' File "/sorry_privacy/test.py", line 30, in <module>\n y.enterConfT()\n', ' File "/sorry_hehe/TClasses.py", line 24, in enterConfT\n self.__a.send( "\\r" )\n', "AttributeError: 'switchA' object has no attribute '_Devices__a'\n"]