2010-06-29

ruby中的实例方法、类方法、单体方法、私有方法、protected方法

1、ruby的的实例方法和Java等其他语言的定义类似
irb(main):065:0> class Dog
irb(main):066:1>   def run
irb(main):067:2>     puts "I'm running!"
irb(main):068:2>   end
irb(main):069:1> end
=> nil
irb(main):070:0> dog = Dog.new
=> #<Dog:0x7fabbbefd918 @name="Dog">
irb(main):071:0> dog.run
I'm running!
2、类方法定义时需要在方法前加入类名称或者self关键字
irb(main):059:0> class Dog
irb(main):060:1>   def Dog.species   # 或者 def self.species
irb(main):061:2>     puts "I belong to the canine"
irb(main):062:2>   end
irb(main):063:1> end
=> nil
irb(main):064:0> Dog.species
I belong to the canine
注意:在ruby中类方法只能被类对象调用,不能被实例对象调用。在这里就是指:species方法只能这样调用Dog.species,而不能这样调用Dog.new.species。这与Java中不同(Java中类只能调用类方法,而实例既可以调用实例方法,也可以调用类方法)。 3、单体方法只在该对象上起作用
irb(main):072:0> dog1 = Dog.new
=> #<Dog:0x7fabbbef6208 @name="Dog">
irb(main):073:0> def dog1.speak
irb(main):074:1>   puts "I'm talking"
irb(main):075:1> end
=> nil
irb(main):076:0> dog1.speak
I'm talking
=> nil
irb(main):077:0> dog2 = Dog.new
=> #<Dog:0x7fabbbedfc88 @name="Dog">
irb(main):078:0> dog2.speak
NoMethodError: undefined method `speak' for #<Dog:0x7fabbbedfc88 @name="Dog">
from (irb):78
from :0
4、和Java一样,用private标识私有方法。但ruby中的私有方法可以在子类中调用(Java中只允许在本类内部调用)。位于private之后的方法都会被认为是私有方法。
irb(main):001:0> class Demo
irb(main):002:1>   def pub_method
irb(main):003:2>     puts "I'm public method"
irb(main):004:2>     puts "invoke private method in internal"
irb(main):005:2>     pri_method
irb(main):006:2>   end
irb(main):007:1>   private
irb(main):008:1>   def pri_method
irb(main):009:2>     puts "I'm private method"
irb(main):010:2>   end
irb(main):011:1> end
=> nil
irb(main):012:0> demo = Demo.new
=> #<Demo:0x7f2e7edf7650>
irb(main):013:0> demo.pub_method
I'm public method
invoke private method in internal
I'm private method
=> nil
irb(main):014:0> demo.pri_method
NoMethodError: private method `pri_method' called for #<Demo:0x7f2e7edf7650>
from (irb):14
from :0
irb(main):024:0> class SubDemo < Demo
irb(main):025:1>   def pub_method
irb(main):026:2>     puts "invoke parent class private method"
irb(main):027:2>     pri_method
irb(main):028:2>   end
irb(main):029:1> end
=> nil
irb(main):030:0> subDemo = SubDemo.new
=> #<SubDemo:0x7f2e7ee4ee00>
irb(main):031:0> subDemo.pub_method
invoke parent class private method
I'm private method
=> nil
5、protected方法 尽管ruby中子类可以调用父类中的私有方法,但是在子类中仍然不能通过外部对象调用。只有protected和public方法才能被外部对象调用。
irb(main):024:0> class SubDemo < Demo
irb(main):025:1>   def pri_pro_diff(other)
irb(main):026:2>     priv_method
irb(main):027:2> ^C
irb(main):027:0> class SubDemo < Demo
irb(main):028:1>   def pri_pro_diff(other)
irb(main):029:2>     pri_method
irb(main):030:2>     other.pri_method
irb(main):031:2>   end
irb(main):032:1> end
=> nil
irb(main):033:0> subDemo1 = SubDemo.new
=> #<SubDemo:0x7f4fb2bc2588>
irb(main):034:0> subDemo2 = SubDemo.new
=> #<SubDemo:0x7f4fb2bb5338>
irb(main):035:0> subDemo1.pri_pro_diff(subDemo2)
I'm private method
NoMethodError: private method `pri_method' called for #<SubDemo:0x7f4fb2bb5338>
from (irb):30:in `pri_pro_diff'
from (irb):35
from :0
irb(main):036:0>
说明:可以看出在子类内部是可以调用父类的私有方法,但在子类内部仍然不能通过外部对象调用私有方法。 下面是将私有方法转换成protected方法的情况
irb(main):036:0> class Demo
irb(main):037:1>   protected :pri_method
irb(main):038:1> end
=> Demo
irb(main):039:0> subDemo1.pri_pro_diff(subDemo2)
I'm private method
I'm private method
=> nil
blog comments powered by Disqus