IronRuby 中的面向对象与数据访问
1. 重写属性与方法
1.1 重写方法示例
在 IronRuby 中,我们可以重写方法。例如,有一个 Human 类继承自 Creature 类,其中 Jump 方法被重写并密封:
public class Human : Creature
{
public override sealed void Jump()
{
Console.WriteLine("Jumping");
}
}
在 IronRuby 里,我们可以创建一个 Athlete 类继承自 Human 类,并对 jump 方法进行重写:
require "human"
class Athlete < Human
def jump
puts "Jumping very high"
end
end
Athlete.new.jump # 输出 "Jumping very high"
Athlete.new.Jump # 输出 "Jumping"
为了统一调用方式,我们可以使用别名:
class Athlete < Human
超级会员免费看
订阅专栏 解锁全文
90

被折叠的 条评论
为什么被折叠?



