面试啦 - 提供专业面试问题及答案、面试技巧、助您成功面试!
您的当前位置:首页 > 面试题库 > IT面试题 > Ruby面试题 > 正文

解释一下ruby中的特殊方法与特殊类

特殊方法是指某实例所特有的方法。一个对象有哪些行为由对向所属的类决定,但是有时候,一些特殊的对象有何其他对象不一样的行为,在多数程序设计语言中,例如C++和Java,我们必须定义一个新类,但在Ruby中,我们可以定义只从属于某个特定对象的方法,这种方法我们成为特殊方法(Singleton Method)。

class SingletonTest
def info
puts “This is This is SingletonTest method”
end
end

obj1 = SingletonTest.new
obj2 = SingletonTest.new

def obj2.info
puts “This is obj2″
end

obj1.info
obj2.info

执行结果为:
This is This is SingletonTest method
This is obj2

有时候,我们需要给一个对象定义一系列的特殊方法,如果按照前面的方法,那么只能一个一个定义:

def obj2.singleton_method1
end

def obj2.singleton_method2
end

def obj2.singleton_method3
end
……
def obj2.singleton_methodn
end

这样做非常繁复麻烦,而且无法给出一个统一的概念模型,因此Ruby提供了另外一种方法,
class << obj
……
end

obj是一个具体的对象实例,class << 代表它的特殊类。

class SingletonTest
def meth1
puts “This is meth1″
end

def meth2
puts “This is meth2″
end
end

obj1 = SingletonTest.new
obj2 = SingletonTest.new

class << obj2
def meth1
puts “This is obj2′s meth1″
end

def meth2
puts “This is obj2′s meth2″
end
end

obj1.meth1
obj1.meth2
obj2.meth1
obj2.meth2

执行结果为:
This is meth1
This is meth2
This is obj2′s meth1
This is obj2′s meth2
词条:ruby面试题
上一篇:介绍一下Ruby中的对象,属性和方法 下一篇:返回列表
与该文相关的文章

温馨提示:如果您对面试网有任何建议,请通过网站联系邮箱向我们反馈,感谢各位的建议与支持!