码迷,mamicode.com
首页 > 其他好文 > 详细

[Ruby] Define abstract methods

时间:2014-07-08 19:45:49      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:ruby   class   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


[Ruby] Define abstract methods,布布扣,bubuko.com

[Ruby] Define abstract methods

标签:ruby   class   methods   

原文地址:http://blog.csdn.net/wonderfan/article/details/37383707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!