ruby单例方法 单例类_Ruby中的单例方法

ruby单例方法 单例类

Ruby| 单例方法 (Ruby | Singleton Methods)

In the last tutorial frozen objects, we have studied the way to freeze the objects in Ruby. We can't assign singleton methods to the objects which are frozen but what are Singleton methods, what are the properties they possess and how they are created? You will get the answer to all the above-raised questions in the rest of the content of this article. After successfully reading the tutorial, you will be very well aware of the things which are essential to understand Singleton methods in Ruby.

在上一期冻结对象教程中,我们研究了在Ruby中冻结对象的方法。 我们不能为冻结的对象分配单例方法,但是什么是单例方法 ,它们具有什么属性以及如何创建它们? 在本文其余的内容中,您将获得所有上述问题的答案。 成功阅读本教程后,您将非常了解了解Ruby中的Singleton方法所必需的内容。

We know that the behavior of an object is decided by the class it belongs to. It simply means that the instance will be able to invoke the methods which are defined in that particular class. But sometimes we want that our object should possess some unique behavior or our object should have a method which other objects of the same class do not have. In this scenario, singleton methods come into the scene. Singleton methods are used in the Graphical user interface where different operations are performed at the click event of different buttons. In Ruby, we have the privilege to provide objects their methods or you can say that we can define singleton methods in Ruby.

我们知道,对象的行为由其所属的类决定。 它只是意味着实例将能够调用该特定类中定义的方法。 但是有时候我们希望我们的对象应该具有某些独特的行为,或者我们的对象应该具有相同类的其他对象所没有的方法。 在这种情况下,单例方法就出现了。 在图形用户界面中使用单例方法 ,其中在不同按钮的click事件中执行不同的操作。 在Ruby中,我们有特权向对象提供其方法,或者可以说我们可以在Ruby中定义单例方法。

For a better understanding of the concept, let us understand this with the help of illustrating program codes.

为了更好地理解该概念,让我们在说明程序代码的帮助下对此进行理解。

Example 1:

范例1:

=begin
Ruby program to demonstrate singleton methods
=end	

class Example
  def prnt
    puts "Go Corona! Corona Go!"
  end
end

object1 = Example.new
object2 = Example.new

def object2.prnt
  puts "Corona bhag jaa!"
end

puts "singleton method demonstration"

puts "Invoke from object1:"
object1.prnt
puts "Invoke from object2:"
object2.prnt

Output

输出量

singleton method demonstration
Invoke from object1:
Go Corona! Corona Go!
Invoke from object2:
Corona bhag jaa!

Explanation:

说明:

In the above code, you can observe that we have redefined a method prnt for object2 of Example class. That prnt method is nothing but a singleton method that is behaving differently for the object2.

在上面的代码中,您可以看到我们已经为Example类的object2重新定义了方法prnt 。 该prnt方法只不过是对object2表现不同的单例方法。

Example 2:

范例2:

=begin
  Ruby program to demonstrate singleton methods
=end	

object1 = String.new
object1 = "includehelp"
object2 = String.new
object2 = "hrithik"

def object2.size
	return "Corona bhag jaa!"
end

puts "singleton method demonstration"

puts "Invoke from object1:"
puts object1.size
puts "Invoke from object2:"
puts object2.size

Output

输出量

singleton method demonstration
Invoke from object1:
11
Invoke from object2:
Corona bhag jaa!

Explanation:

说明:

In the above code, you can observe that we can define the singleton method for the objects which belong to library classes. Instead of printing the size of object2, our code is printing "Corona bhag jaa! because this is returned by the method.

在上面的代码中,您可以观察到我们可以为属于库类的对象定义单例方法 。 我们的代码不是打印object2的大小,而是打印“ Corona bhag jaa!”,因为这是由方法返回的。

翻译自: https://www.includehelp.com/ruby/singleton-methods.aspx

ruby单例方法 单例类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值