super(): no arguments
时间: 2024-05-19 11:11:30 浏览: 13
When calling the `super()` function with no arguments, it is equivalent to calling `super(__class__, self)` which returns a temporary object of the superclass, allowing you to call its methods. This is known as zero argument form of `super()`.
This is commonly used in Python when inheriting from a superclass and wanting to call its methods in the subclass. By using `super()`, you ensure that the correct method is called, even if the method has been overridden in the subclass.
相关问题
no arguments
"no arguments"是指在使用super()调用父类初始化方法时没有传递任何参数。在继承类中,如果没有定义初始化方法,Python会自动调用父类的初始化方法。但是如果使用super()调用父类初始化方法时没有传递任何参数,就会出现"no arguments"的错误。这是因为父类初始化方法需要至少一个参数self,而没有传递任何参数导致出现错误。
frameworks/base/media/java/android/media/MediaPlayer.java:670: error: constructor PlayerBase in class PlayerBase cannot be applied to given types; public MediaPlayer() { ^ required: AudioAttributes,int found: no arguments reason: actual and formal argument lists differ in length frameworks/base/media/java/android/media/MediaPlayer.java:672: error: call to this must be first statement in constructor this(AudioSystem.AUDIO_SESSION_ALLOCATE); ^ 2 errors怎么办
这是一个编译错误,错误信息显示 `PlayerBase` 类的构造函数参数个数不匹配。这是因为 `MediaPlayer` 类的构造函数调用了 `PlayerBase` 类的构造函数,并传递了错误的参数个数或类型。解决这个问题的方法是修改 `MediaPlayer` 类的构造函数,使其调用正确的 `PlayerBase` 类的构造函数,并传递正确的参数。
具体来说,可以在 `MediaPlayer` 类的构造函数中调用 `PlayerBase` 类的构造函数,并传递一个 `AudioAttributes` 对象和一个整数值作为参数。这样就可以解决上述编译错误。修改后的代码如下:
```
public MediaPlayer() {
super(new AudioAttributes.Builder().build(), 0);
this(AudioSystem.AUDIO_SESSION_ALLOCATE);
}
```
在这个修改后的构造函数中,首先调用了 `PlayerBase` 类的构造函数,并传递了一个 `AudioAttributes` 对象和一个整数值作为参数,然后再调用了 `MediaPlayer` 类的另一个构造函数,并传递了一个整数值作为参数。这样就可以正确地创建一个 `MediaPlayer` 对象了。