首页 > 其他 > 详细

[Ruby] Define abstract methods

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!