“个人”红宝石方法 [英] "Personal" method in ruby

查看:100
本文介绍了“个人”红宝石方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使方法成为个人方法的方法-注意班级不是私人的

I'm looking for a way of making a method "personal" - note NOT PRIVATE to a class

这是一个示例-个人是指方法 foo的行为

here is an example - by "personal" I mean the behaviour of method "foo"

class A
  def foo
     "foo"
  end
end

class B < A
  def foo
      "bar"
  end
end

class C < B
end

a=A.new; b=B.new;c=C.new

我正在寻找一种产生遵循以下行为

I'm looking for a way of producing the following behaviour

a.foo #=> "foo"

b.foo #=> "bar"

c.foo #=> "foo" (ultimate base class method called)


推荐答案

创建个人方法的方法,更改继承结构。

Instead of creating 'personal' methods, change your inheritance structure.

似乎您希望C类只具有与B类相同的功能,而又不

It appears that you want the C class to have only some of the same functionality of the B class while not making changes to the A class.

class A
  def foo
     "foo"
  end
end

class BnC < A
end

class B < BnC
  def foo
      "bar"
  end
end

class C < BnC
end

a=A.new; b=B.new;c=C.new

这篇关于“个人”红宝石方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆