设为首页 加入收藏

TOP

[Ruby] Define abstract methods
2015-07-24 05:40:48 来源: 作者: 【 】 浏览:5
Tags:Ruby Define abstract methods

Ruby has its own style to define the abstract methods in its class.

class Shape
  
  def initialize
    raise NotImplementedError.new("#{self.class.name} is an abstract class")
  end
  
  def area
    raise NotImplementedError.new("#{self.class.name} area is an abstract method ")
  end
  
end

class Square < Shape
  
  def initialize(length)
    @length = length
  end
  
  def area
    @length ** 2
  end
end

Shape.new().area

puts Square.new(10).area


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYOJ 737 合并石子(一) 下一篇POJ 1573:Robot Motion

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: