就像标题所说的那样,我想要指定一个超级的NSArrayController东西,有些东西self = [super[NSArrayController] function],但没有运气搜索这个.有任何想法吗?提前致谢.
编辑删除抽象例子,因为他们让人们对我问题的性质感到困惑.
这样做的目的是以编程方式执行从NSArrayController到NSButton的'add'的简单绑定在IB中执行的操作.在我的应用程序中有几个arrayControllers,所以我希望能够指定我想要通过代码获取super的那个.
我正在寻找NSArrayController的超级的原因是因为我的印象是应该解决模型而不是控制器(NSArrayController)而我的模型是一个核心数据模型,我相信我可以通过使用超级我按名称指定的NSArrayController.也许有一种更直接的方式来添加数据模型.
这段代码
static void writeTo(List<? super Apple> apples) {
apples.add(new Apple());
apples.add(new Jonathan());
}
Run Code Online (Sandbox Code Playgroud)
该代码的作者说明了这一点
参数apples是某种类型的List,它是Apple的基本类型; 因此,您知道添加Apple或Apple的子类型是安全的.由于下限是Apple,
Jonathan是Apple的子类.
但是当我尝试这个时
List<Jonathan> loj = new ArrayList<Jonathan>();
listSuper(loj);
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误
The method listSuper(List<? super Apple>) in the type Basket<T> is not applicable for the arguments (List<Jonathan>)
Run Code Online (Sandbox Code Playgroud)
哪里listSuper是这样的
static void listSuper (List<? super Apple> cont) {}
Run Code Online (Sandbox Code Playgroud)
这两者有何不同?
还有什么让我对我发布的第一个代码感到困惑的是我想到的?super T意味着任何基本类型的T.但是从它的外观上他添加了T的子类型.我很困惑.
我知道当我们通过super方法调用parent的方法时,我们可以忽略绑定方法中的"self"参数,如下所示:
class Foo(object):
def __init__(self):
super(Foo, self).__init__() # We needn't pass in the "self" argument
# ...
Run Code Online (Sandbox Code Playgroud)
但__new__方法有所不同:
class Bar(object):
def __new__(cls, *args, **kwargs):
return super(Bar, cls).__new__(cls, *args, **kwargs) # Why need a "cls" argument?
Run Code Online (Sandbox Code Playgroud) 我想在Ember.js的视图中包含多个mixin,并且多个mixins和/或视图使用相同的事件(例如willInsertElement).我正在运行Ember 1.4.0-beta.5.
我知道每个mixin中的事件都会被视图覆盖.但是,我已经读过,通过this._super();在mixin的上述事件方法的开头调用,可以在mixin和view中使用相同的事件挂钩,或者在同一视图中包含多个mixin.但是,我无法成功实现这一目标.因此,我的问题是,如何在视图和mixin(或同一视图中包含的多个mixin)中的同一事件挂钩中编写逻辑,以便调用每次出现的事件挂钩中的所有逻辑.
这是一个例子:
App.StatsView = Em.View.extend(
App.DateFormatting, {
willInsertElement: function() {
// Some view-specific logic I want to call here
},
});
App.DateFormatting = Em.Mixin.create({
willInsertElement: function() {
this._super(); // This doesn't work.
// Some mixin logic I want to call here
},
});
Run Code Online (Sandbox Code Playgroud)
NB这里的一种方法可能是不使用mixin并扩展视图(因为willInsertElement特定于Em.View),但在我们的应用程序中无法维护.
请参阅以下代码:
class A {
private int b;
A(){
b=5;
}
}
class B extends A {
}
class C {
public static void main(String args[]){
B b=new B();
}
}
Run Code Online (Sandbox Code Playgroud)
当我创建B的实例时,B的默认构造函数调用A的构造函数,该构造函数将值赋给实例变量b.我的查询是因为实例变量与类的实例相关联,并且我们没有创建任何类A的实例,这个赋值(b = 5)究竟意味着什么?当没有A的实例时,对A的构造函数的调用是什么意思?
我试图在python中使用super扩展我的类初始化.在下面的例子中,我想Class A初始化一个参数乘以4,我希望这个参数可以通过继承在B类中使用.然后我想在B中进行另一次初始化以获取相同的参数并将其乘以8.第二次初始化不必在A中可用.
class A():
def __init__(self,parameter):
self.initialize_parameter=4*parameter
class B(A):
def __init__(self,parameter): #note this parameter is intended to be the same parameter from Class A
self.another_parameter=parameter*8
super(B,self).__init__(parameter)
Run Code Online (Sandbox Code Playgroud)
我一直在阅读的文件似乎表明以上是正确的语法(我认为),但关于这个主题的文档相当混乱.上面的代码返回TypeError: must be type, not classobj.
这是我第一次使用继承,有人可以建议我做错了吗?
我是python的新手.我试图了解super()python多重继承中的功能.
class B():
def __init__(self):
print("__init__ of B called")
self.b = "B"
class C():
def __init__(self):
print("__init__ of C called")
self.c = "C"
class D(B, C):
def __init__(self):
print("__init__ of D called")
super().__init__()
def output(self):
print(self.b, self.c)
d = D()
d.output()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
AttributeError: 'D' object has no attribute 'c'
Run Code Online (Sandbox Code Playgroud) 我第一次使用unittest和selenium,但这也是我在python中的第一个更大的代码。我为这个问题找到了一些答案,但是对于具有__ init __继承和方法super()的类没有解释。__ init __()
TL; DR 我有一个可以一一继承的类。创建chrome实例的第一个类StartInstance从unittest.TestCase继承,问题出在继承和super()上。其他类中的init(),因为当我删除它时,测试正常开始
一切看起来像:
class StartInstance(unittest.TestCase):
@classmethod
def setUpClass(cls): pass
class A(StartInstance):
def __init__(self):
super().__init__() adding variables etc to init
class B(A):
def __init__(self):
super().__init__() adding variables etc to init
class C(A):
def __init__(self):
super().__init__() adding variables etc to init
class PrepareTests(B, C, D):
def all tests(self):
self.tests_B
self.tests_C
self.tests_D
class Tests(PrepareTests):
def test_click:
click()
all_tests()
#and finally somewhere a test runner
suite = loader.loadTestsFromTestCase(Tests)
runner.run(suite())
#when i run this i get this …Run Code Online (Sandbox Code Playgroud) 众所周知,父类必须在子类之前构造; 如果父类的构造函数接受参数,则必须显式调用父类的构造函数.我的问题是,如果子类本身没有构造函数,如何调用从子类显式获取参数的父类构造函数?
public class A {
public String text;
public A(String text) {
this.text = text;
}
}
public class B extends A {
// Now I must call the constructor of A explicitly (other wise I'll get a
// compilation error) but how can I do that without a constructor here?
}
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,如果我创建一个名为example的类的构造函数,如下所示:
public class Example{
public Example(){
this.super();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的方法不起作用,因为会javac Example.java通知以下编译错误:
Example.java:3: error: illegal qualifier; Object is not an inner class
this.super();
^
1 error
Run Code Online (Sandbox Code Playgroud)
但是,它不应该像this使用隐式声明那样工作,而不是通过使用super()显式声明this吗?
super ×10
inheritance ×4
java ×4
python ×4
constructor ×3
cocoa ×1
ember.js ×1
generics ×1
javascript ×1
mixins ×1
objective-c ×1
oop ×1
python-3.x ×1
selenium ×1
superclass ×1
this ×1
unit-testing ×1
wildcard ×1
xcode ×1