def __init__(self): super(Main, self).__init__()
时间: 2023-08-07 18:34:56 浏览: 52
在这个代码中,`Main` 类继承自另一个类,并在其构造函数中调用了父类的构造函数。`super(Main, self)` 实际上是在调用 `Main` 类的父类的构造函数,也就是在调用 `Parent.__init__()`。
通过调用父类的构造函数,`Main` 类可以继承父类的属性和方法,并在其自己的构造函数中添加自己的属性和方法。这种方式可以帮助我们避免代码重复,并使代码更加简洁和易于维护。
需要注意的是,在使用 `super()` 函数调用父类的构造函数时,你需要确保父类的构造函数已经被正确地实现。如果父类的构造函数需要一些参数,你需要在调用 `super()` 函数时将这些参数传递进去。例如,如果父类的构造函数需要一个字符串参数,你可以这样调用:
```python
super(Main, self).__init__("hello")
```
这样就会将字符串 `"hello"` 传递给父类的构造函数。
相关问题
def __init__(self): super(self).__init__()
这段代码中的 `super(self).__init__()` 是错误的用法。在调用 `super()` 函数时,第一个参数应该是当前类的类名,而不是实例对象 `self`。
正确的用法应该是 `super(ClassName, self).__init__()`,其中 `ClassName` 是当前类的类名。例如:
```python
class Main(Parent):
def __init__(self):
super(Main, self).__init__()
```
在这个例子中,`Main` 类继承自 `Parent` 类,并在其构造函数中调用了父类的构造函数。通过调用 `super(Main, self).__init__()`,`Main` 类可以继承父类的属性和方法,并在其自己的构造函数中添加自己的属性和方法。
需要注意的是,Python 3 中的 `super()` 函数可以省略第一个参数,如果省略了第一个参数,Python 会自动根据当前类和实例对象来确定要调用的父类。例如:
```python
class Main(Parent):
def __init__(self):
super().__init__()
```
在这个例子中,我们省略了第一个参数,Python 会自动根据 `Main` 类和实例对象来确定要调用的父类。
class Faceshow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(Faceshow, self).__init__(parent) self.setupUi(self) self.main_button.clicked.connect(start) self.main_exit.clicked.connect(self.quit) def quit(self): self.close() class RESUshow()(QWidget,Ui_Form): def __int__(self, parent=None): super(RESUshow, self).__init__(parent) self.setupUi(self) 上述代码怎么改才正确
您的代码存在两个问题:
1. 在 `Faceshow` 类中,`start` 函数没有定义,无法连接到 `main_button` 的 `clicked` 信号上。
2. 在 `RESUshow` 类中,`__int__` 函数应该是 `__init__`。
以下是修改后的代码:
```
class Faceshow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Faceshow, self).__init__(parent)
self.setupUi(self)
self.main_button.clicked.connect(self.start) # 连接到 self.start 函数
self.main_exit.clicked.connect(self.quit)
def start(self):
# 实现按钮点击后的功能
pass
def quit(self):
self.close()
class RESUshow(QWidget, Ui_Form):
def __init__(self, parent=None):
super(RESUshow, self).__init__(parent)
self.setupUi(self)
```
注意,我在 `Faceshow` 类中添加了一个空的 `start` 函数作为示例,您需要把它替换为您实际的功能实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)