Ruby中抽象类的替代方法? [英] Alternatives to abstract classes in Ruby?

查看:70
本文介绍了Ruby中抽象类的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的新手.一个简单的例子,我需要什么:

I am new to Ruby. A simple example, what I need:

class Animal
   abstract eat()

class Cat < Animal
   eat():
     implementation

class Dog < Animal
   eat():
     implementation

换句话说,所有扩展Animal的类都必须使用eat()方法.

In other words, the eat() method should be required for all the classes which extend Animal.

在JAVA中,我只使用一个抽象类,但是经过一些研究,我发现很多人都不在Ruby中使用它,而建议使用mixin/modules.

In JAVA I would just use an abstract class, but after doing some research I found that many people don't use it in Ruby and mixin / modules are recommended instead.

但是,我不理解模块是否可以做更多的事情,而不仅仅是添加方法.确切地说,模块是否可以为类设置必须实现的方法的要求(如果可以,将提供一个示例)?

However, I don't understand, if modules can do more than just include an addition methods. To be exact, can modules set the requirements for classes which methods they must implement (if yes, an example would be appreciated)?

总而言之,当我想确定所有相同类型的类都具有特定的方法并以自己的方式实现时,我应该在这种情况下使用什么?

To sum up, what should I use in this case, when I want to be sure, that all classes of the same type have particular methods and implement them in their own way?

推荐答案

Ruby不提供此功能.您有责任确保您的类实现了他们应该实现的功能.

Ruby does not offer this functionality, no. You are responsible for making sure that your classes implement what they ought to implement.

Ruby无法实现这种功能的部分原因是可以重新打开Ruby类,并且Ruby支持在运行时加载任意代码,因此,在尝试调用该类之前,我们不知道该类是否实现了特定接口

Part of the reason that such functionality is impossible for Ruby is that Ruby classes can be reopened, and Ruby supports loading arbitrary code at runtime, so we can't know whether a class implements a certain interface until we try to call it.

假设Animal必须具有eat方法,我将执行以下操作:

Supposing an Animal must have an eat method, and I do the following:

class Cat < Animal
  def talk
    puts "meow"
  end
end

class Cat
  def eat
    puts "om nom nom"
  end
end

在该文件的末尾,Cat将具有其eat定义,因为Ruby类可以多次重新打开和修改.因为尚未定义eat,所以代码在第一个定义之后是否会出错?该实现的弊大于利,因为重新打开类是很常见的,即使这个例子是人为设计的.一旦调用eat方法并且该方法不存在,它是否会出错,因此可以确定一旦需要它就可以定义它?好吧,如果缺少该方法,无论如何都会发生 .解释器永远不会知道是否正在定义另一个类,因此在真正调用该方法之前,它永远不会切断您的视线.

By the end of that file, the Cat will have its eat definition, because Ruby classes can reopened and modified multiple times. Should the code error out after the first definition because eat wasn't defined yet? That implementation would hurt more than it would help, since reopening classes is common, even if this example is contrived. Should it error out once the eat method is called and does not exist, so we can be certain that it's defined once we need it? Well, if the method were missing, that would happen, anyway. The interpreter can never know if another class definition is on the way, so it can never cut you off until the method is actually called.

简而言之,超类根本不可能要求在Ruby中定义方法,因为类的动态性质与该目标背道而驰.

In short, superclasses simply cannot possibly require a method to be defined in Ruby, because the dynamic nature of classes contradict such a goal.

对不起!不过,在这里可以方便地进行单元测试,以确保子类无论如何都可以做他们应该做的事情.

Sorry! This is a place where unit testing might come in handy, though, to ensure that your subclasses do what they're supposed to be doing, anyway.

这篇关于Ruby中抽象类的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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