我创建了一个Model类,我根据在User中调用的方法(属性)定义方法(继承自Model).问题是我无法覆盖define_method定义的方法,并调用super来传递给定义的方法.我想这是因为定义的方法被添加到User本身,而不是模型,所以它实际上没有超类中的方法(即模型).
我想这样做的原因是因为大多数属性应该直接保存到数据库中,而某些属性(如密码)需要一些额外的处理.
class Model
def self.attribute(name)
define_method(name) do
self
end
end
end
class User < Model
attribute :password
end
class User2 < Model
attribute :password
def password
super
end
end
@user = User.new
puts @user.password # => <User:0x00000100845540>
@user2 = User2.new
puts @user2.password
# define_super.rb:17:in `password': super: no superclass method
# `password' for #<User2:0x00000100845578> (NoMethodError)
# from define_super.rb:25:in `<main>'
Run Code Online (Sandbox Code Playgroud)
有什么办法可以改变代码来实现这个目的吗?我需要一种方法来覆盖动态创建的方法.
有什么区别
[self.view addSubview:view1];
Run Code Online (Sandbox Code Playgroud)
和
[super.view addSubview:view1];
Run Code Online (Sandbox Code Playgroud)
谢谢!!
Franhu
可能重复:
了解Python super()
类的B子类A,所以在B中__init__我们应该__init__像这样调用A :
class B(A):
def __init__(self):
A.__init__(self)
Run Code Online (Sandbox Code Playgroud)
但是super(),我看到这样的事情:
class B(A):
def __init__(self):
super(B, self).__init__() #or super().__init__()
Run Code Online (Sandbox Code Playgroud)
我的问题是:
为什么不super(B, self).__init__(self)呢?仅仅因为返回代理对象是绑定的?
如果我在super中省略第二个参数并且返回代理对象是未绑定的,那么我应该写super(B).__init__(self)吗?
我试图了解python中的超级工作方式,并尝试了以下示例:
class A(object):
def __init__(self):
print "in A's init"
class B(object):
def __init__(self):
print "in B's init"
class C(A,B):
def __init__(self):
super(C,self).__init__()
print "In C"
if __name__=="__main__":
c=C()
Run Code Online (Sandbox Code Playgroud)
相当简单..我尝试了以下超级调用(在此处显示结果):
>>> super(B,c).__init__()
>>> super(B,c).__init__()
>>> super(A,c).__init__()
in B's init
>>> super(A,c).__init__()
in B's init
>>> super(A,c).__init__()
in B's init
>>> super(B,c).__init__()
>>> super(C,c).__init__()
in A's init
Run Code Online (Sandbox Code Playgroud)
我不明白为什么super(A,c).__init__()在B的init 中打印出它?
如何存根:包含模块的超级方法.我有以下控制器:
class ImportsController < BaseController
include ImportBackend
def import_from_file
super
rescue TransferPreview::Error => exc
flash[:error] = "Some String"
redirect_to imports_path
end
end
Run Code Online (Sandbox Code Playgroud)
和importBackend模块:
module ImportBackend
def import_from_file
//something
end
end
Run Code Online (Sandbox Code Playgroud)
我想测试那个控制器.我的问题是如何在ImportBackend中存根方法来引发错误?我试过几个解决方案但没有任何作用
ImportBackend.stub(:import_from_file).and_raise(Transfer::Error)
controller.stub(:super).and_raise(Transfer::Error)
controller.stub(:import_from_file).and_raise(Transfer::Error)
Run Code Online (Sandbox Code Playgroud)
谢谢你的所有答案.
在Java类和对象中,我们使用"this"关键字来引用类中的当前对象.从某种意义上说,我相信"这个"实际上会回归自身的对象.
示例:
class Lion
{
public void Test()
{
System.out.println(this); //prints itself (A Lion object)
}
}
Run Code Online (Sandbox Code Playgroud)
在超类和子类的场景中.我认为"超级"关键字会返回超类的对象.但是这次我似乎弄错了:
例:
class Parent
{
public Parent(){
}
}
class Child extends Parent
{
public Child(){
System.out.println(super.getClass()); //returns Child. Why?
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:在上面的例子中,我期待编译器打印出来class Parent,但它打印出来class Child.为什么会这样?什么超级实际返回?
我试图__init__()在一个超类中调用该方法,其中所说的方法接受参数,但是似乎不起作用。请参见下面的代码:
>>> class A:
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
>>> class B(A):
def __init__(self, param1, param2, param3):
super(B, self).__init__(param1, param2)
self._var3 = param3
>>> a = A("Hi", "Bob")
>>> a._var1
'Hi'
>>> a._var2
'Bob'
>>>
>>> b = B("Hello", "There", "Bob")
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
b = B("Hello", "There", "Bob")
File "<pyshell#69>", line 3, in __init__
super(B, self).__init__(param1, param2)
TypeError: must be type, not classobj
>>> …Run Code Online (Sandbox Code Playgroud) 我正在将一些代码从python2转换为python3,并且我遇到了一个带有元类的错误.
这是工作的python2代码(简化):
#!/usr/bin/env python2
# test2.py
class Meta(type):
def __new__(mcs, name, bases, clsdict):
new_class = type.__new__(mcs, name, bases, clsdict)
return new_class
class Root(object):
__metaclass__ = Meta
def __init__(self, value=None):
self.value = value
super(Root, self).__init__()
class Sub(Root):
def __init__(self, value=None):
super(Sub, self).__init__(value=value)
def __new__(cls, value=None):
super(Sub, cls).__new__(cls, value)
if __name__ == '__main__':
sub = Sub(1)
Run Code Online (Sandbox Code Playgroud)
这是转换后的python3代码:
#!/usr/bin/env python3
# test3.py
class Meta(type):
def __new__(mcs, name, bases, clsdict):
new_class = type.__new__(mcs, name, bases, clsdict)
return new_class
class Root(object, metaclass=Meta):
def __init__(self, value=None): …Run Code Online (Sandbox Code Playgroud) 来自文档:
# Declare both screens
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
Run Code Online (Sandbox Code Playgroud)
从这个SO问题:
class WelcomeScreen(Screen):
def __init__(self, **kwargs):
super(Screen,self).__init__(**kwargs)
Run Code Online (Sandbox Code Playgroud)
在哪种情况下需要初始化屏幕,super为什么?
我有一个自定义错误类,该类扩展了Javascript中的内置Error类。我想到的问题是,是否通过我的Jest单元测试调用了“ super()”方法。
export class AppError extends Error {
public name: string;
public message: string;
public status?: number;
public data?: any;
constructor(message: string, status?: number, data?: any) {
super(); <-- this guy!!
this.name = 'AppError';
this.status = status || 500;
this.message = message;
this.data = data;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以测试吗?谢谢。
super ×10
python ×5
ecmascript-6 ×1
inheritance ×1
java ×1
jestjs ×1
kivy ×1
metaclass ×1
objective-c ×1
python-3.x ×1
rspec ×1
ruby ×1
self ×1
stub ×1
subview ×1
superclass ×1
superview ×1
unit-testing ×1