解释一下ruby中的特殊方法与特殊类
编辑: 面试啦 发布时间: 2024-01-23 12:18:26
特殊方法是指某实例所特有的方法。一个对象有哪些行为由对向所属的类决定,但是有时候,一些特殊的对象有何其他对象不一样的行为,在多数程序设计语言中,例如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
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中的对象,属性和方法 下一篇:返回列表
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题