super 有 2 个参数,
super(type, obj_of_type-or-subclass_of_type)
Run Code Online (Sandbox Code Playgroud)
我理解如何以及为什么使用 super ,第二个参数是 obj_of_type 。但我不明白第二个参数是子类的问题。
任何人都可以展示为什么以及如何?
在Python中,值进入作用域有三种方式:它们作为参数传入,它们可以从封闭作用域访问,最后封闭类和封闭函数的第一个参数可用于super(). 为什么不向开发人员公开此机制,以便将其扩展到其他情况?
__context__ ChainMap拥有一个随处可用、公开的东西不是更干净吗?默认情况下,输入类的定义会将类本身公开为__context__['__class__'],输入非静态、非类成员函数会将第一个参数公开为__context__['self']。但是,总的来说,我们可以使用像这样的结构做更多的事情
entercontext {'blah': 123}:
f(__context__['blah'])
Run Code Online (Sandbox Code Playgroud)
这会将字典推送到 ChainMap 上,并在退出范围时将其弹出。
然后,这可以用于实现许多其他设置情况,例如 numpy set_printoptions/get_printoptions等。您只需将它们推送到上下文中,而不是获取、设置和恢复。
由于我是 Android 编程新手,我遇到了另一个我不明白的小事情。为什么下面的 onCreateOptionsMenu 方法返回super.onCreateOptionsMenu 而不是仅仅调用 super.onCreateOptionsMenu (就像在 onCreate 方法中完成的那样)?
(这是来自Android 教程。)
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
(我在 StackOverflow 上没有发现重复的问题。我可能问了一个愚蠢的问题,或者我只是不擅长搜索。)
我正在使用Python 2.6.6。
我已将错误代码缩小到以下两类:
class Graph(object):
def __init__(self, name):
self.name = name
self.testme = 3
Run Code Online (Sandbox Code Playgroud)
和
class StepPlot(Graph):
def __init__(self, name):
print("fdasfdsf")
print(dir(super(Graph, self)))
super(Graph, self).__init__(name)
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我实例化StepPlotwith时StepPlot('fdsfa'),出现错误
TypeError: object.__init__() takes no parameters
难道它不能接受一个参数吗?
看着
什么时候调用Python的super().__init__()?
这个班级组织应该有效。
我从根本上错过了一些东西吗?任何帮助,将不胜感激。
这里是 Python 3,以防万一它很重要。
我试图正确理解如何在@property使用时实现继承,并且我已经搜索了 StackOverflow 并阅读了 20 个类似的问题,但无济于事,因为他们试图解决的问题略有不同。这是我用于测试的代码:
class Example:
def __init__(self):
self.__data = None
@property
def data(self):
return self.__data
@data.setter
def data(self, data):
self.__data = data
class Example2(Example):
def __init__(self):
super().__init__()
@property
def data(self):
return super().data # Works!
@data.setter
def data(self, data):
data = '2' + data
#Example.data = data # Works, but I want to avoid using the parent name explicitly
#super().data = data # Raises AttributeError: 'super' object has no attribute 'data'
#super().data.fset(self, data) # …Run Code Online (Sandbox Code Playgroud) 我目前面临关键字的理解问题super。我知道这super是对特定子类的下一个超类的引用。无论如何,我想告诉你我的问题:
public class Father{
int length;
Father(String s){
length = s.length();
}
}
public class Sub extends Father {
char c;
Sub(String s) {
super(s);
c = s.charAt(0);
}
}
Run Code Online (Sandbox Code Playgroud)
super(s)该行在子类中做什么?如果我将 Father 类从公共类更改为抽象类会发生什么?
在下面的代码中,当我有一个类实现两个具有相同默认方法签名的接口时,它要求我重写它。但在重写方法中为什么我必须使用 super 关键字来调用默认方法。
package practice;
interface interA{
public default void AImp(){
System.out.println("Calling Aimp from interA");
}
}
interface interB{
public default void AImp(){
System.out.println("Calling Aimp from interB");
}
}
public class Practice implements interA,interB {
public static void main(String[] args) {
Practice inter = new Practice();
inter.AImp();
}
@Override
public void AImp() {
interA.super.AImp();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用下面的代码执行相同的操作:
@Override
public void AImp() {
interA inter = new Practice();
inter.AImp();
}
Run Code Online (Sandbox Code Playgroud) 我已经在 python 3 中创建了自定义异常,并且所有代码都工作得很好。但有一件事我无法理解,为什么我需要将消息发送到 Exception 类的__init__()以及当我尝试打印异常时它如何将自定义异常转换为该字符串消息因为 Exception 甚至 BaseException 中的代码没有做太多事情。
不太明白为什么从自定义异常中调用super().__init__()?
我想构建一个包含多个方法的超类,因为我想从不同的类中调用它们。此外,我还有减少代码的好处。
但是,我收到错误消息“超级表达式必须为空或函数”
这是我想要super.interface()从 SuperScreen.js 文件调用函数的类之一:
import React from "react";
import { SuperScreen } from "./SuperScreen";
export default class HomeScreen extends SuperScreen {
constructor(props) {
super(props);
this.state = {
isLoading: true,
data: null,
key: 15
};
}
render() {
return super.interface();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 SuperScreen.js
import React, { Component } from "react";
export default class SuperScreen extends Component {
constructor() {}
interface() {...}
}
Run Code Online (Sandbox Code Playgroud)
不过,我还是收到了消息Super expression must either be null or a function。为什么以及如何解决它?
亲切的问候和谢谢
我无法理解这个错误。
在下面的代码中,当我使用时,tk.Frame一切都按预期工作。但是,如果我使用super(),我会抛出一个AttributeError(“应用程序对象没有属性 tk ”)。
class Application(tk.Frame):
def __init__(self,parent):
tk.Frame.__init__(self,parent) <----- This works
# super().__init__(self,parent) <------ This line throws an error
.
.
.
if __name__=='main':
root=tk.Tk()
Application(root).pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
据我了解,super(Application,self).__init__()将调用绑定__init__到实例 MRO 中的类的方法child,即tkinter.Frame我的情况下的类。
我通过打印Application.__mro__和检查来验证这一点。
所以我的问题是,如果 和super().__init__(self,parent)都tk.Frame.__init__(self,parent)引用相同的__init__class 方法tkinter.Frame,为什么一个会抛出错误,而另一个会正常工作?我怀疑我对 super() 的工作方式有一些误解。
super ×10
python ×5
java ×3
python-3.x ×3
class ×2
superclass ×2
android ×1
exception ×1
inheritance ×1
interface ×1
javascript ×1
oop ×1
react-native ×1
reactjs ×1
return ×1
subclass ×1
tkinter ×1