标签: super

在define_method定义的方法上调用super

我创建了一个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)

有什么办法可以改变代码来实现这个目的吗?我需要一种方法来覆盖动态创建的方法.

ruby metaprogramming super superclass

4
推荐指数
1
解决办法
2673
查看次数

4
推荐指数
1
解决办法
1703
查看次数

[python]:被super()搞糊涂了

可能重复:
了解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)

我的问题是:

  1. 为什么不super(B, self).__init__(self)呢?仅仅因为返回代理对象是绑定的?

  2. 如果我在super中省略第二个参数并且返回代理对象是未绑定的,那么我应该写super(B).__init__(self)吗?

python super

4
推荐指数
1
解决办法
1246
查看次数

在蟒蛇中揭开神秘的神秘面纱?

我试图了解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 中打印出它?

python super

4
推荐指数
1
解决办法
197
查看次数

在控制器中存储超级方法

如何存根:包含模块的超级方法.我有以下控制器:

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)

谢谢你的所有答案.

rspec stub super

4
推荐指数
1
解决办法
1781
查看次数

在Java中,super.getClass()打印"Child"而不是"Parent" - 为什么会这样?

在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.为什么会这样?什么超级实际返回?

java super

4
推荐指数
1
解决办法
822
查看次数

Python:需要参数时使用super()调用基类的__init __()方法

我试图__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)

python inheritance super

4
推荐指数
1
解决办法
1211
查看次数

"TypeError:object()不带参数"将python2元类转换为python3

我正在将一些代码从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)

python metaclass super python-3.x

4
推荐指数
1
解决办法
1151
查看次数

kivy屏幕.我必须用super初始化吗?

来自文档:

# 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为什么?

python super kivy

4
推荐指数
1
解决办法
2561
查看次数

玩笑单元测试是否调用了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)

有什么办法可以测试吗?谢谢。

unit-testing super ecmascript-6 jestjs

4
推荐指数
1
解决办法
2171
查看次数