我试图在Ruby中定义一些具有继承层次结构的类,但我想在派生类的基类中使用其中一个方法.扭曲的是,我不想调用我所处的确切方法,我想要调用另一种方法.以下不起作用,但这是我想做的(基本上).
class A
def foo
puts 'A::foo'
end
end
class B < A
def foo
puts 'B::foo'
end
def bar
super.foo
end
end
Run Code Online (Sandbox Code Playgroud) 我在这里看了关于python的super()方法的其他问题,但我仍然觉得很难理解整个概念.
我也在查看pro python一书中的例子
引用的例子是
class A(object):
def test(self):
return 'A'
class B(A):
def test(self):
return 'B-->' + super(B, self).test()
class C(A):
def test(self):
return 'C'
class D(B, C):
pass
>>> A().test()
'A'
>>> B().test()
'B-->A'
>>> C().test()
'C'
>>> D().test()
'B-->C'
>>> A.__mro__
(__main__.A, object)
>>> B.__mro__
(__main__.B, __main__.A, object)
>>> C.__mro__
(__main__.C, __main__.A, object)
>>> D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, object)
Run Code Online (Sandbox Code Playgroud)
为什么要做D().test()我们得到'B - > C'而不是'B - > A'的输出
书中的解释是
在最常见的情况下,包括此处显示的用法,super()接受两个参数:类和该类的实例.正如我们在此处的示例所示,实例对象确定将使用哪个MRO来解析结果对象上的任何属性.提供的类确定该MRO的子集,因为super()仅使用在提供的类之后发生的MRO中的那些条目.
我仍然觉得解释有点难以理解.这可能是一个可能的重复,并且已经多次询问与此类似的问题,但如果我理解了这一点,我可能能够更好地理解其他问题.
我读到使用带有这样的超级通配符:
public class MyClass <T extends Comparable<? super T>> {
...
}
Run Code Online (Sandbox Code Playgroud)
代替:
public class MyClass <T extends Comparable<T>> {
...
}
Run Code Online (Sandbox Code Playgroud)
可以让这个班级"更通用",但我不明白为什么.
有人能提供一些具体的例子吗?
我有这些课程.
Person是父类,Student是子类:
class Person(object):
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, avr, name):
self.avr = avr
super(Student, self).__init__(self, name)
Run Code Online (Sandbox Code Playgroud)
当我尝试创建一个实例时,我收到此错误Student:
__init__() takes exactly 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
最终类用户可能想创建一个由Base和组成的类Mixin(Mixin在第三方库类上提供了其他通用功能)。
但是,Mixin.__init__当按以下方式使用时不会调用。仅Base.__init__称为:
>>> class Base(object): #3rd party library class
... def __init__(self): print "Base"
...
>>> class Mixin(object): #my features useful as addendum for a few classes
... def __init__(self): print "Mixin"
...
>>> class C(Base, Mixin): pass
...
>>> c = C()
Base
Run Code Online (Sandbox Code Playgroud)
如何执行调用都Mixin.__init__和Base.__init__在这种情况下,无需用户记住把一个构造函数super()在C级呼叫?
>>> class Base(object):
... def __init__(self): print "Base"
...
>>> class Mixin(object):
... def __init__(self): print "Mixin"
...
>>> …Run Code Online (Sandbox Code Playgroud) class Person {
public String name;
String id;
public Person() {
System.out.println("Parent default");
name = id = "";
}
public Person(String name, String id) {
System.out.println("Parent parameter");
this.name = name;
this.id = id;
}
void show() {
System.out.println(this.name + "\n" + this.id);
}
}
class Student extends Person {
Student() {}
Student(String a, String b) {
super(a, b);
}
}
class Main {
public static void main(String args[]) {
Person p = new Person("A", "AA");
Student s = new Student("b", …Run Code Online (Sandbox Code Playgroud) 在以下代码中,Parent#just_do覆盖GrandParent#just_do. 在Me课堂上,如何打电话GrandParent#just_do?
module GrandParent
def just_do
puts "GrandParent"
end
end
module Parent
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
include GrandParent
def just_do
puts "Parent"
end
end
end
class Me
include Parent
def self.just_do
super # this calls Parent#just_do
end
end
Run Code Online (Sandbox Code Playgroud) 因此,当我第一次打开该选项卡时,会显示来自 Socket.IO 的数据。但是当我更改选项卡并返回时,我无法从 Socket.IO 获取数据。
这是我的代码:
Map <String,dynamic> list;
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket']
});
@override
initState(){
socket.on('connect',(_){
socket.on('stockQuote',(jsonData){
setState(() {
list = jsonData;
isLoading = false;
});
});
});
super.initState();
}
dispose(){
super.dispose();
}
Run Code Online (Sandbox Code Playgroud)
我收到这些错误:
Unhandled Exception: setState() called after dispose(): _StocksState#0faab(lifecycle state: defunct, not mounted)
This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the …Run Code Online (Sandbox Code Playgroud) 我有一个自定义异常类,如下所示:
public abstract class AccountException extends BankServiceException {
private int accountNo;
public AccountException(String message, int accountNo) {
super(message);
this.accountNo = accountNo;
}
public int getAccountNo() {
return accountNo;
}
Run Code Online (Sandbox Code Playgroud)
现在我需要将这个抽象类扩展到一个更具体的异常:
public class OverdraftLimitReachedException extends AccountException {
private double amount;
private double overdraft;
public OverdraftLimitReachedException(int accountNo,double amount,double overdraft) {
super("The overdraft limit has been exceded",accountNo,amount,overdraft);
this.overdraft = overdraft;
this.amount = amount;
}
public double getAmount() {
return amount;
}
public double getOverdraft() {
return overdraft;
}
Run Code Online (Sandbox Code Playgroud)
现在这里的问题是超级构造函数,我知道我需要给它参数数量和透支,但是当我尝试编译它时,它说参数的长度不同。我如何正确调用超级构造函数,所以我可以给出错误信息。谢谢!
编辑:我添加了构造函数的实际外观(对不起,我有点笨拙)。稍后在代码中,当我实际抛出异常时,我需要提供构造函数的参数。
虽然由于我的经验不足,我没有错误或任何关于此事的代码,但super()在子类的构造函数中隐式调用 使我感到困惑。
如果我调用一个孩子的构造函数,他的父母也是一个孩子等等,会不会有一系列连续的 super()调用?就像在,因为父构造函数也有一个隐式调用,super()该调用会被进行吗?
如果是这样,那能走多深/走多深?
如果我一直困惑或问了一个明显的问题,我的导师没有提供令人满意的答案,我深表歉意。