假设我有以下静态方法和接口(List是java.util.List).请注意,静态方法对列表的通配符类型强制执行"super Foo".
public class StaticMethod {
public static void doSomething(List<? super Foo> fooList) {
...
}
}
public interface MyInterface<T> {
public void aMethod(List<T> aList);
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用静态方法添加一个实现接口的类,如下所示:
public class MyClass<T> implements MyInterface<T> {
public void aMethod(List<T> aList) {
StaticMethod.doSomething(aList);
}
}
Run Code Online (Sandbox Code Playgroud)
这显然不会编译,因为T没有"超级Foo"约束.但是,我看不到任何添加"超级Foo"约束的方法.例如 - 以下内容不合法:
public class MyClass<T super Foo> implements MyInterface<T> {
public void aMethod(List<T> aList) {
StaticMethod.doSomething(aList);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题 - 理想情况下没有改变StaticMethod或MyInterface?
class Foo
a: ->
x.call =>
super
Run Code Online (Sandbox Code Playgroud)
不会编译,因为我不能从匿名类调用super.但是我的意图是将超类方法称为'a'.这是coffeescript中缺少的功能吗?
请注意,我将代码更改为
class Foo
a: ->
x.call =>
return Foo.__super__.a.apply(this, arguments)
Run Code Online (Sandbox Code Playgroud)
让它工作,但这不是coffeescript正确!
为什么这样设计?
Ruby的设计倾向于尽可能多地实现方法; 关键字通常保留用于具有自己的语法规则的语言功能.super但是,外观和行为就像一个方法调用.
(我知道super在纯Ruby中实现它会很麻烦,因为它必须解析方法名称caller,或者使用trace_func.仅此一项不会阻止它成为一种方法,因为很多内核的方法都是没有在纯Ruby中实现.)
我正在玩python中的多重继承,我遇到了一个我无法理解它是如何发生的情况.
这是继承布局:
A F
/ \ |
B C |
\ | /
\ | /
D
Run Code Online (Sandbox Code Playgroud)
每个人都熟悉的ABCD钻石.加上额外的"F"级别,我把它扔进去玩.
这是代码:
class A(object):
def foo(self, call_from):
print "foo from A, call from %s" % call_from
super(A, self).foo("A")
class B(A):
def foo(self, call_from):
print "foo from B, call from %s" % call_from
super(B, self).foo("B")
class C(A):
def foo(self, call_from):
print "foo from C, call from %s" % call_from
super(C, self).foo("C")
class F(object):
def foo(self, call_from):
print "foo from F, call from %s" % …Run Code Online (Sandbox Code Playgroud) 我有这个演示代码:
class Test2 extends Test {
public int number = 0;
@Override
public void set(){
number = 1;
info();
}
@Override
public void info(){
System.out.println(number);
}
}
public class Test {
public Test(){
set();
}
public void set(){
}
public void info(){
}
public static void main(String[] args){
Test2 object = new Test2();
object.info();
}
}
Run Code Online (Sandbox Code Playgroud)
代码给出了这个输出:
1
0
Run Code Online (Sandbox Code Playgroud)
为什么?我期待这个输出:
1
1
Run Code Online (Sandbox Code Playgroud)
在我的opionion中,main函数调用Test2类的构造函数来创建一个对象.构造函数自动调用超类'构造函数.此构造函数调用被覆盖的方法set().因此调用类Test2的方法set().此方法设置字段并调用写入数字的info()方法.然后main函数再次调用创建的对象的info()方法.
正确设置数字字段,因为第一行输出为"1".但为什么第二行包含0?看来这个领域根本没有设定.你能解释一下吗?
我该怎么做才能得到我期望的行为?提前致谢!
目前(Android API 17),片段super Android的参考中唯一提到的是随便通过一些代码示例(不同于Android 上的Activity Reference,它仔细记录了super需要的位置).
因此,建议根据需要搜索网络,或等待崩溃,以确定super需要呼叫的位置.我要求SO用户分享他们对哪些Fragment生命周期方法需要调用的知识super.
superonAttach()onCreate()- 大概是的,因为Activity版本需要它onCreateView() - 无论有没有好看onActivityCreated()onViewStateRestored()onStart()- 大概是的,因为Activity版本需要它onResume()- 大概是的,因为Activity版本需要它
onPause()- 大概是的,因为Activity版本需要它
onStop()- 大概是的,因为Activity版本需要它onDestroyView()onDestroy()- 大概是的,因为Activity版本需要它onDetach()
onSaveInstanceState()- 大概是的,因为Activity版本需要它
有人可以解释我在super.paint(g)哪里使用,g是Graphics Applets或awt或swing或Java中的变量.
我做过研究,发现它用于覆盖但是这个覆盖的用途是什么?
我是初学者.如果可能的话,你可以解释之间的差异paint(g),并super.paint(g)用小example或请帮助我的代码?
/*
Let us consider this code
This has only one paint declaration i.e; subclass's paint method declaration, no declaration for superclass's paint function... when we explicitly call superclass's paint function
what is the use of super.paint(g) and is it going to use superclass's paint declaration??
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="superpaintDemo" height=768 width=1366>
</applet>
*/
class superpaintDemo extends Applet
{
public void paint(Graphics …Run Code Online (Sandbox Code Playgroud) 我知道这里已经讨论过super()和多继承.但是我没有找到解决方案,关于我在python3中的具体问题.我们假设我们有:
#! /usr/bin/env python3
class A(object):
def __init__(self):
super().__init__()
def foo(self):
print("The")
class B(object):
def __init__(self):
super().__init__()
def foo(self):
print("world")
class C(B):
def __init__(self):
super().__init__()
def foo(self):
super().foo()
print("is")
class D(A,C):
def __init__(self):
super().__init__()
def foo(self):
super().foo()
print("nice")
d = D()
d.foo()
Run Code Online (Sandbox Code Playgroud)
这会让我:
The
nice
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我将D()中的继承顺序更改为:
class D(C,A):
def __init__(self):
super().__init__()
def foo(self):
super().foo()
print("nice")
Run Code Online (Sandbox Code Playgroud)
它给了我
world
is
nice
Run Code Online (Sandbox Code Playgroud)
但是,我只获得所需的输出:
The
world
is
nice
Run Code Online (Sandbox Code Playgroud)
使用:
class D(C,A):
def __init__(self):
super().__init__()
def foo(self):
A.foo(self)
C.foo(self)
print("nice")
Run Code Online (Sandbox Code Playgroud)
我发现它非常不优雅.
所以我的问题是:是否可以在python3中使用super()来调用两个超类的超级方法而不仅仅是第一个?
class Works(type):
def __new__(cls, *args, **kwargs):
print([cls,args]) # outputs [<class '__main__.Works'>, ()]
return super().__new__(cls, args)
class DoesNotWork(type):
def __new__(*args, **kwargs):
print([args[0],args[:0]]) # outputs [<class '__main__.doesNotWork'>, ()]
return super().__new__(args[0], args[:0])
Works() # is fine
DoesNotWork() # gets "RuntimeError: super(): no arguments"
Run Code Online (Sandbox Code Playgroud)
据我所知,在两种情况下,super._new__都接收类文字作为第一个参数,一个空元组作为第二个参数.
那么为什么会出错而另一个不出错?
functools.singledispatch有助于定义单调度泛型方法.同时,还有super()调用方法或访问超类的属性.
是否有类似的东西super()可以使用singledispatch?我尝试了以下,但结果super(Derived, value)只是不是实例Base,所以它不能按我的预期工作:
from functools import singledispatch
@singledispatch
def hello(value):
return ['default']
@hello.register(Base)
def hello_base(value):
return hello(super(Base, value)) + ['base']
@hello.register(Derived)
def hello_derived(value):
return hello(super(Derived, value)) + ['derived']
print(hello(Derived())
# expected ['default', 'base', 'derived'],
# but actually is ['default', 'derived'].
Run Code Online (Sandbox Code Playgroud) super ×10
python ×4
java ×3
inheritance ×2
overriding ×2
python-3.x ×2
android ×1
applet ×1
awt ×1
coffeescript ×1
field ×1
functools ×1
generics ×1
methods ×1
paint ×1
ruby ×1
superclass ×1
wildcard ×1