Python祖父母方法(基因)

2024-04-18 07:01:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在制作一个简单的脚本,让我通过一个访问服务器控制多个(32)交换机和路由器。我已经创建了一个类来启动串行连接

不管怎样,我的问题是如何使用继承的方法?我有一个祖父母的设备,有2个孩子(父亲)路由器和交换机。这两个孩子成为了几个孩子的父亲让我们用SwitchA SwitchB&;卢特拉。现在在Cisco设备中,有些配置是“标准的”,但不是全部。假设我想通过串口进入“配置终端”

焦点:

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' 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"]

当变量a和x指向同一个对象时,我希望它们能够保持私有。 我从OOP和C++中知道,最小化重复代码,我在C++中似乎没有祖辈继承问题,但我知道Python工作方式不同。我也读了一些问答;但是我真的不明白他们的意思。我是一个初级Python脚本编写者

事先谢谢你,请原谅我的英语


Tags: self脚本sendobjectinitdef孩子路由器
1条回答
网友
1楼 · 发布于 2024-04-18 07:01:59

找到了答案。在init中使用super()可以从父级初始化

这是有益的,因为我往往阅读速度非常快,全球。 https://www.python-course.eu/python3_inheritance.php

相关问题 更多 >