[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

[Ruby] Define abstract methods

时间: 2024-11-03 22:42:48

[Ruby] Define abstract methods的相关文章

浅析Ruby中的methods,private_methods和instance_methods

首先,methods,private_methods是Object类的实例方法;instance_methods是Module类的实例方法. 我们先来看看这样安排的原因: 我们知道一个Ruby对象所能调用的方法包含在其祖先链中(包含这个对象的单例类).这里所说的Ruby对象可以分为2类,一类是普通对象,像"abc",2,obj=Object.new这种对象,它们所属的类分别是String,Fixnum,Object,我们称这种对象为普通对象:还有一类对象是类(类本身也是一种对象),像S

[Ruby] LEVEL 2 Methods and Classes

Optional Arguments Set default arguments, when we don't need to call it, we can simply skip it. def new_game(name, year=nil, system=nil) { name: name, year: year, system: system } end game = new_game("Street Figher II") Options Hash Argument Som

[ROR] 如何在mixin模块中定义类方法(Howto define class methods in a mixin module)

方法一: 修改模块的include方法 module Bbq def self.included(base) base.send :include, InstanceMethods base.extend ClassMethods end module InstanceMethods def m1 'instance method' end end module ClassMethods def m2 'this is class method' end end end class Test i

Interface VS Abstract Class

An abstract class can have shared state or functionality. An interface is only a promise to provide the state or functionality. A good abstract class will reduce the amount of code that has to be rewritten because it's functionality or state can be s

Java Interview Reference Guide--reference

Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, 2014 by Nitin Kumar JAVA Object Oriented Concepts Java in based on Object Oriented concepts, which permits higher level of abstraction to solve any p

ruby面向对象class

ruby对象是严格封装的:只能通过定义的方法访问其内部状态.方法使用的成员变量在对象外部不能直接访问,不过可以通过getter.setter等访问器方法(accessor),使他们看起来好像是直接访问的. 与对象状态的封装性相反,ruby中的类非常开放.每个ruby程序都可以为现有类添加方法,而且也可以为单个对象添加“单键方法(singleton method)”. 创建类 Classes are created in Ruby with the class keyword:class Poin

C# abstract function VS virtual function?

An abstract function has to be overridden while a virtual function may be overridden. Virtual functions can have a default /generic implementation in the base class. An abstract function can have no functionality. You're basically saying, any child c

The difference of Methods and Functions

Functions Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax

C# 抽象类abstract

不能初始化的类被叫做抽象类,它们只提供部分实现,但是另一个类可以继承它并且能创建它们的实例,有未被实现的方法.抽象类不可以new对象. "一个包含一个或多个纯虚函数的类叫抽象类,抽象类不能被实例化,进一步一个抽象类只能通过接口和作为其它类的基类使用. 抽象类能够被用于类,方法,属性,索引器和事件,使用abstract 在一个类声明中表示该类倾向要作为其它类的基类成员被标示成abstract,或被包含进一个抽象类,必须被其派生类实现. abstract class ShapesClass { ab