大多数重载方法都需要[super theMethod]调用.
(例如[super viewDidLoad];,[super viewWillAppear];和[super dealloc];)
我没有想过我是否需要[super touchesBegan:withEvent:]打电话,但它似乎在某种程度上发挥了作用.
什么时候需要它,当没有我需要它?
我正在尝试以编程方式取消触摸事件,这似乎与我提出的问题有关.
Class Child扩展Parent.父实现协议C,它具有可选方法,包括-(void)d.孩子有执行-d; 应该调用[super d]吗?
换句话说,[super d]当且仅当某些内容会响应时,我会编写什么代码来调用?假设我不控制Parent的实现; 它可能随时改变.
这是我想到的所有方法.我目前正在使用4号.
显然是明智的答案1:
[super d]; // Delete this line if a runtime exception occurs when you try it
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为Parent可能会动态实现-d,因此当您测试它而不是在字段中时,它会起作用.或者Parent的实现可能会更改,以便此测试的结果不再正确.
显然是明智的回答2:
if ([super respondsToSelector:_cmd])
[super d];
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为NSObject的-respondsToSelector实现将在Child中找到实现并在所有情况下返回YES.
显然是明智的答案3:
if ([[self superclass] instancesRespondToSelector:_cmd])
[super d];
Run Code Online (Sandbox Code Playgroud)
当且仅当超类知道它总是实现-d时,这是有效的; 如果实例动态确定是否存在此方法,则此方法将不起作用.优于1,因为它将在运行时获取对Parent实现的静态更改.
显然是明智的回答4:
@try
{
[super d];
}
@catch (NSException *exception)
{
NSString *templateReason = [NSString stringWithFormat:
@"-[%@ %@]: unrecognized selector sent to instance %p"
,NSStringFromClass([self superclass])
,NSStringFromSelector(_cmd)
,self];
if (![exception.reason isEqualToString:templateReason])
@throw …Run Code Online (Sandbox Code Playgroud) 为此做一个简洁的标题很难.
无论如何,想象一下我有一个父类:
public class Shape {
public Dimensions getDimensions() {
// Does some generic stuff.
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个派生类来覆盖getDimensions方法:
public class Circle extends Shape {
public Dimensions getDimensions() {
// Does some stuff.
super.getDimensions();
}
}
Run Code Online (Sandbox Code Playgroud)
当我创建一个带有切入点的方面时Shape.getDimensions,切入点Circle.getDimensions被调用两次:一次用于Circle.getDimensions,然后一次调用super.getDimensions.
切入点看起来像这样: @Pointcut("execution(* Shape.getDimensions(..))")
我在建议中添加了一个黑客来检查声明类型的名称(使用JoinPoint.getSignature().getDeclaringType().getName()),但我发现它相当粗糙,感觉有点像黑客.我认为必须有一个更好的方法来做到这一点.
在那儿?
如果格式不是很好,请道歉.第一次在这里问一个问题(我通常可以找到答案).
这个问题来自以下问题,我们说class B延伸class A
class A(object):
def do_work(self):
print 123
class B(A):
def do_work(self):
super(B,self).do_work() # versus the next statement
super(A,self).do_work() # what's the difference?
Run Code Online (Sandbox Code Playgroud) 我有一个方法,每次调用时都会记录一条消息.我希望此日志消息指示该方法是直接调用还是super在子类中使用调用.
class DoerOfWork {
public function doWork():void {
var calledWithSuper:Boolean;
calledWithSuper = ???;
trace("doWork called" + (calledWithSuper ? " (with super)." : "."));
}
}
class SlowerDoerOfWork extends DoerOfWork {
public override function doWork():void {
for (var i:Number = 0; i < 321684; i++) {
// wait a moment
}
super.doWork();
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这将是可以确定的类是否this已经覆盖的实施doWork,通过比较this.doWork来DoerOfWork.prototype.doWork.
不幸的是,这是不可能的.在ActionScript中的任何地方都无法访问未绑定的方法(规范列出了两种类型的函数:函数闭包和绑定方法).在实例上甚至没有任何属性MethodClosure可以识别两个是否是同一方法的绑定副本.
如何检查方法是否已被覆盖或使用任何其他方法来确定当前正在执行的ActionScript方法是super直接调用还是直接调用?
repr(super(class, thing))不同super(class, thing).__repr__()?我遇到过一些我认为很奇怪的事情,如果有人解释为什么它的工作方式会很好,那就太棒了.
我的问题是如何repr运作
class MyInt(int):
pass
one = MyInt(1)
sup = super(MyInt, one)
Run Code Online (Sandbox Code Playgroud)
>>> repr(sup)
"<super: <class 'MyInt'>, <MyInt object>>"
而
>>> sup.__repr__()
'1'
第二个是我期望从第一个开始的(是的,引用在每种情况下都不同).为什么这些案件有所repr不同?我以为repr(x)刚刚打过电话x.__repr__.
据我所知,super为超类的方法提供了一个API,而不是超类本身.所以在我看来,我sup有两个__repr__方法,一个由名称调用repr,一个名称在名称之下__repr__.非常感谢能够深入了解这里发生的事情.
我已经在 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() 的工作方式有一些误解。
我有个问题。在使用 python 的 oops 中, super 可以访问方法或构造函数,但不能访问属性。这是为什么??
class Phone:
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
def return_phone(self):
print ("Returning a phone")
class FeaturePhone(Phone):
pass
class SmartPhone(Phone):
def __init__(self, price, brand, camera, os, ram):
super().__init__(price, brand, camera)
self.os = os
self.ram = ram
print ("Inside smartphone constructor")
def buy(self):
print(super().camera) #Error
print ("Buying a smartphone")
s=SmartPhone(20000, "Samsung", 12, "Android", 2)
print(s.buy()) #error
print(s.brand) …Run Code Online (Sandbox Code Playgroud) super ×10
python ×3
python-3.x ×3
aspect ×1
aspectj ×1
class ×1
exception ×1
inheritance ×1
internals ×1
iphone ×1
java ×1
javascript ×1
objective-c ×1
oop ×1
parent ×1
pointcut ×1
python-2.7 ×1
react-native ×1
reactjs ×1
repr ×1
superclass ×1
tkinter ×1
touch ×1