Csharp/C#教程:你如何在Ruby中做多态?分享


你如何在Ruby中做多态?

在C#中,我可以这样做:

class Program { static void Main(string[] args) { List animals = new List(); animals.Add(new Dog()); animals.Add(new Cat()); foreach (Animal a in animals) { Console.WriteLine(a.MakeNoise()); a.Sleep(); } } } public class Animal { public virtual string MakeNoise() { return String.Empty; } public void Sleep() { Console.Writeline(this.GetType().ToString() + " is sleeping."); } } public class Dog : Animal { public override string MakeNoise() { return "Woof!"; } } public class Cat : Animal { public override string MakeNoise() { return "Meow!"; } } 

显然,输出是(略有释义):

由于C#经常因其冗长的类型语法而被模拟,你如何处理像duck这样的鸭子类型语言中的多态/虚方法?

编辑:为您更新的问题添加了更多代码

免责声明:我在一年左右的时间里没有使用过Ruby,也没有在这台机器上安装过Ruby,因此语法可能完全错误。 但这些概念是正确的。


完全相同的方式,使用类和重写方法:

 class Animal def MakeNoise return "" end def Sleep print self.class.name + " is sleeping.n" end end class Dog < Animal def MakeNoise return "Woof!" end end class Cat < Animal def MakeNoise return "Meow!" end end animals = [Dog.new, Cat.new] animals.each {|a| print a.MakeNoise + "n" a.Sleep } 

到目前为止,所有的答案对我来说都很好。 我以为我只是提到整个遗产并不是完全必要的。 暂时排除“睡眠”行为,我们可以使用duck-typing实现整个期望的结果,并且省略了创建Animal基类的需要。 谷歌搜索“鸭子打字”应该产生任何数量的解释,所以在这里,我们只是说“如果它像鸭子一样走路,像鸭子一样嘎嘎叫……”

可以通过使用mixin模块来提供“睡眠”行为,例如Array,Hash和其他Ruby内置类,包括Enumerable。 我并不是说它一定更好,只是一种不同的,也许是更具惯用性的Ruby方式。

 module Animal def sleep puts self.class.name + " sleeps" end end class Dog include Animal def make_noise puts "Woof" end end class Cat include Animal def make_noise puts "Meow" end end 

你知道其余的……

使用惯用的Ruby

 class Animal def sleep puts "#{self.class} is sleeping" end end class Dog < Animal def make_noise "Woof!" end end class Cat < Animal def make_noise "Meow!" end end [Dog, Cat].each do |clazz| animal = clazz.new puts animal.make_noise animal.sleep end 

在前一个答案的基础上,您可以这样做吗?


澄清后的第二次削减:

 class Animal def MakeNoise raise NotImplementedError # I don't remember the exact error class end def Sleep puts self.class.to_s + " is sleeping." end end class Dog < Animal def MakeNoise return "Woof!" end end class Cat < Animal def MakeNoise return "Meow!" end end animals = [Dog.new, Cat.new] animals.each {|a| puts a.MakeNoise a.Sleep } 

(我将原样保留,但“self.class.name”胜过“.to_s”)

duck typing的原理就是对象必须响应被调用的方法。 所以类似的东西也可以做到这一点:

 module Sleeping def sleep; puts "#{self} sleeps" end dog = "Dog" dog.extend Sleeping class << dog def make_noise; puts "Woof!" end end class Cat include Sleeping def to_s; "Cat" end def make_noise; puts "Meow!" end end [dog, Cat.new].each do |a| a.sleep a.make_noise end 

manveru解决方案的一个小变体,它动态地创建基于类类型数组的不同类型的对象。 没有什么不同,只是更清楚一点。

 Species = [Dog, Cat] Species.each do |specie| animal = specie.new # this will create a different specie on each call of new print animal.MakeNoise + "n" animal.Sleep end 

这是我写它的方式:

 class Animal def make_noise; '' end def sleep; puts "#{self.class.name} is sleeping." end end class Dog < Animal; def make_noise; 'Woof!' end end class Cat < Animal; def make_noise; 'Meow!' end end [Dog.new, Cat.new].each do |animal| puts animal.make_noise animal.sleep end 

它与其他解决方案并没有什么不同,但这是我更喜欢的风格。

这是12行与41行(实际上,您可以通过使用集合初始化程序来减少3行)来自原始的C#示例。 不错!

有一种实现多态的方法(通过将所有实例变量从给定类复制到新类)

 class Animal attr_reader :name def initialize(name = nil) @name = name end def make_noise '' end def becomes(klass) became = klass.new became.instance_variables.each do |instance_variable| value = self.instance_variable_get(instance_variable) became.instance_variable_set(instance_variable, value) end became end end class Dog < Animal def make_noise 'Woof' end end class Cat < Animal def make_noise 'Meow' end end animals = [Dog.new('Spot'), Cat.new('Tom')] animals.each do |animal| new_animal = animal.becomes(Cat) puts "#{animal.class} with name #{animal.name} becomes #{new_animal.class}" puts "and makes a noise: #{new_animal.make_noise}" puts '----' end 

结果是:

 Dog with name Spot becomes Cat and makes a noise: Meow ---- Cat with name Tom becomes Cat and makes a noise: Meow ---- 

我希望它对你有所帮助

上述就是C#学习教程:你如何在Ruby中做多态?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/949583.html

(0)
上一篇 2021年11月17日
下一篇 2021年11月17日

精彩推荐