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

Ruby类的继承

时间:2014-10-02 22:54:03      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   使用   ar   for   sp   div   

Ruby继承的语法

class DerivedClass < BaseClass
    #some stuff
end

 < 为继承符号

重写(override) 的概念

有时, 我们希望子类从父类继承来的方法可以做与父类不相同的事情,这就需要在子类中重写方法。例如, 你有一个类名字叫Email继承于类Message,两个类都有send方法,但是Email类的send方法需要有邮件地址和一系列邮件协议,但是Message中的send方法并不知道这些,与其在Email类中添加send_mail方法,而从父类继承的send方法弃之不用, 不如显式的修改send方法以适应Email的需求。

例如:

class Creature
  def initialize(name)
    @name = name
  end
  
  def fight
    return "Punch to the chops!"
  end
end

# Add your code below!
class Dragon < Creature
    def fight
        return "Breathes fire!"
    end
end
    
dragon = Dragon.new("dragon")
dragon.fight
--------------------------------------------------
输出:
"Breathes fire!"

 

另一方面, 有时子类发现它所需要的继承自父类的方法已经被改写, 不要惊慌, 我们可以直接获取父类的响应方法, 这需要使用super关键字。

语法为:

class DerivedClass < Base
  def some_method
    super(optional args)
      # Some stuff
    end
  end
end

当你在方法中调用super, 这就是告诉Ruby,在父类中找到与调用super的这个方法同名的函数,如果找到, 那么Ruby将会使用其父类版本的这个方法。

例如:

class Creature
  def initialize(name)
    @name = name
  end
  
  def fight
    return "Punch to the chops!"
  end
end

# Add your code below!
class Dragon < Creature
    def fight
        puts "Instead of breathing fire..."
        super
    end
end
    
dragon = Dragon.new("w")
dragon.fight
-------------------------------------------------------------------
输出:
Instead of breathing fire...
"Punch to the chops!"

 

Ruby不支持多继承。然而Ruby允许使用mixin, 这个我们稍后再讲。

 

为了程序的安全性, Ruby允许我们显式地对方法进行public或private声明, public方法允许作为接口被调用,private方法则对外界不可见。如果不写public或private,Ruby默认为public。

 1 class Person
 2   def initialize(name, age)
 3     @name = name
 4     @age = age
 5   end
 6   
 7   public    # This method can be called from outside the class.
 8   
 9   def about_me
10     puts "I‘m #{@name} and I‘m #{@age} years old!"
11   end
12   
13   private   # This method can‘t!
14   
15   def bank_account_number
16     @account_number = 12345
17     puts "My bank account number is #{@account_number}."
18   end
19 end
20 
21 eric = Person.new("Eric", 26)
22 eric.about_me
23 eric.bank_account_number  #错误, 调用了私有方法!
 -------------------------------------------------------------------------
 输出:
 Im Eric and Im 26 years old!
 private method `bank_account_number‘ called for #<Context::Person:0x0000000262d930 @name="Eric", @age=26>

 

用attr_reader, attr_writer读写属性(attribute)

根据前面我们所学,如果想要访问定义在类中的属性,例如,我们想要访问@name实例变量, 我们必须这么写

def name
  @name
end

如果我们想要我们想修改@name实例变量,那么我们要这么写:

def name=(value)
  @name = value
end

现在不必这么麻烦了。我们可以用attr_reader和attr_writer来读写变量,如下:

class Person
  attr_reader :name
  attr_writer :name
  def initialize(name)
    @name = name
  end
end

当遇到上面的代码时,Ruby自动地做类似如下的事情:

def name
  @name
end

def name=(value)
  @name = value
end

像变魔术一样, 我们可以随意读写变量了!我们仅仅是把变量(转换为symbol)传给attr_reader和attr_writer

如果你既想read也想write一个变量, 那么还有比使用attr_reader和attr_writer更加简短的办法, 那就是用attr_accessor.

 

Ruby类的继承

标签:style   blog   color   io   使用   ar   for   sp   div   

原文地址:http://www.cnblogs.com/tardis/p/4004438.html

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