发新话题
打印

Ruby程序快速入门之类变量和类方法

Ruby程序快速入门之类变量和类方法

  象大多数面向对象语言一样,Ruby类也允许定义类变量和方法。一个类变量允许在一个类的所有实例间共享单个变量。在Ruby中,两个@@号用于指示类变量。例如,如果你想要使一个BankAccount类的所有实例共享相同的利息率,那么该类可能被如下定义:

class BankAccount
@@interestRate = 6.5
def BankAccount.getInterestRate()
@@interestRate
end
attr_accessor :balance
def initialize (bal)
@balance = bal
end
end

  如你所见,类变量必须在使用前初始化,并且就象实例变量一样,如果你想存取类变量的话,你需要编写存取器方法。在此,我定义了一个类方法来返回利息率。注意,类名和在getInterestRate前面的句号表示一个类方法。一个类方法,不管对于任何实例,其工作方式都是相同的-在此,是把相同的利息率返回到所有的BankAccount实例。为了调用类方法,你需要使用类名,就象它使用于类方法定义中一样:

irb(main):045:0> BankAccount.getInterestRate
=> 6.5

  事实上,用于创建类实例的"new"方法就是一个类方法。因此,当你在程序中输入"Rectangle.new"时,你实际在调用new类方法-这是Ruby默认情况下所提供的。

  继承

  面向对象编程的原则之一是支持类层次结构。就象自然界中的事物分类一样,类允许从更为通用的类进行继承。面向对象编程的特征主要体现在方法和变量的使用上。例如,一个Square类继承Rectangle类的一些特征,如方法和变量。一个Square是一种更具体类型的Rectangle(高度和宽度相等的Rectangle实例),但是它仍然有一个高度和宽度,也有一个面积(而且与矩形的计算方法相同)。在Ruby中,Square类可以使用下列定义创建:

class Square < Rectangle
end

  "<Rectangle"意味着,Square是Rectangle的一个子类,或反过来说,Rectangle是Square的一个超类。默认情况下,一个Square实例自动地拥有所有一个Rectangle所拥有的相同的属性和方法,包括height,width和area方法。为了确保Square实例的边长相等,你可以重载现有的Square的initialize方法:

class Square < Rectangle
def initialize (size)
@height = size
@width = size
end
end

  因为在Ruby中一切都是对象,所以Ruby中的一切几乎都派生自Object类。尽管这在所有类定义中都不是显式的(你不会看到<Object出现在定义中),但是的确所有的类都派生自Ruby的基类Object。知道这个事实后,你就会更容易地理解接下来要讨论的内容。

  当编写你的应用程序时,你可以在一个类定义外定义方法。在本文开始,你已看到了一个并不是任何一个类的一部分的摄氏到华氏转换器方法。作为另外一个示例,下面是一个位于任何类之外的方法:

def feel?
return "I feel fine."
end

  为了执行这个方法,只要输入该方法名,而不需要类或实例:

irb(main):042:0> feel?
=> "I feel fine."

  此方法看似另外一种语言(如C)中的函数或过程。事实上,尽管这些方法看上去好象不属于任何类,但是这些方法却都是你已经添加到Object类上的方法,它(因为Object是所有类的超类)反过来也把这一方法添加到你的继承类上。因此,现在你可以在任何对象(如Square和Rectangle的实例)甚至一个类(如Rectangle类)上调用这个方法。

irb(main):043:0> sq1=Square.new(4)
=> #<Square:0x5a18b50 @width=4, @height=4>
irb(main):044:0> rect1=Rectangle.new(5,7)
=> #<Rectangle:0x5a139a8 @width=7, @height=5>
irb(main):045:0> sq1.feel?
=> "I feel fine."
irb(main):046:0> rect1.feel?
=> "I feel fine."
irb(main):047:0> Rectangle.feel?
=> "I feel fine."
这世界总有你不明白

TOP

人生就是游戏

生就是为了找寻爱的过程,每个人的人生都要找到四个人。  


第一个是完美世界外挂,  
第二个是你最爱的人,  
第三个是最爱你的人,  
第四个是共度一生的人..  


首先会遇到你最爱的人,然後体会到爱的感觉;  
因为了解被爱的感觉,所以才能发现最爱你的人;  
当你经历过爱人与被爱,学会了爱,才会知道什么是你需要的,  
也才会找到最适合你,能够相处一辈子的人。  

但很悲哀的,在现实生活中,这三个人通常不是同一个人;  
你最爱的,往往没有选择你;  
最爱你的,往往不是你最爱的;  
而最长久的,偏偏不是你最爱也不是最爱你的,  
只是在最适合的时间出现的那个人。  

你,会是别人生命中的第几个人呢?  
没有人是故意要变心的,他爱你的时候是真的爱你,  
可是他不爱你的时候也是真的不爱你了,  
他爱你的时候没有办法假装不爱你;  
同样的,他不爱你的时候也没有办法假装爱你 。  

当一个人不爱你要离开你,  
你要问自己还爱不爱他,  
如果你也不爱他了,千万别为了可怜的自尊而不肯离开;  
如果你还爱他,你应该会希望他过得幸福快乐,  
希望他跟真正爱的人在一起,绝不会阻止,  
你要是阻止他得到真正的幸福,就表示你已经不爱他了,  
而如果你不爱他,你又有什么资格指责他变心呢?  

热血江湖外挂不是占有,  
你喜欢月亮,不可能把月亮拿下来放在风火之旅外挂里,  
但月亮的光芒仍可照进你的诛仙外挂。  

换句话说,你爱一个人,也可以用另一种方式拥有,  
让爱人成为生命里的永恒回忆,  
如果你真爱一个人,就要爱他原来的样子—爱他的好,也爱他的坏:  
爱他的优点,也爱他的缺点,  
绝不能因为爱他,就希望他变成自己所希望的样子,  
万一变不成就不爱他了。  

真正爱一个完美私服是无法说出原因的,  
你只知道无论何时何地、心情好坏,你都希望这个人陪著你;  
真正的感情是两人能在最艰苦中相守,也就是没有丝毫要求。  

毕竟,感情必须付出,而不是只想获得;  
分开是一种必然的考验,  
如果你们感情不够稳固,只好认输,  
真爱是不会变成怨恨的。  

两人在谈情说爱的时候,  
最喜欢叫对方发誓,许下承诺我们为什么要对方发誓,  
就是因为我们不相信对方,我们根本不相信情人,  
而这些山盟海誓又很不切实际:  
海枯石烂、地老天荒,都不能改变我对你的爱!  
明知道海不会枯、石不会烂、地不会老、天不会荒;  
就算会,也活不到那时候。  

许下诺言的时候千万注意,不要许下可以实现的诺言,  
最好是承诺做不到的事,  
反正做不到的,随便说说也不要紧,  
请记住:”不可能实现的诺言最动人”  
在爱情里,说的是一套,做的是另一套;  
讲的人不相信,听的人也不相信。  


你呢?找到了第几个?  
茫茫人海中,你遇见了谁?谁又遇见了你?

TOP

沈阳宏海机械厂供应梅花糕机024-82116562

沈阳宏海机械厂www.086hh.com批发零售梅花糕机!单模5瓣梅花型,模具带上盖,两边对称带有手把,使用方便操作简单!另提供女士用轻便7头梅花糕机!赠送专用面浆壶,专用挑钩,操作台平面设计图,制作工艺流程光盘!电话:024-82116562 QQ:491783951
沈阳宏海

TOP

顶一个吧

既然看了,顶一个吧,好帖子

---------------
welcome to our wow power leveling webpage,and here you may have wow powerleveling at every levels, the wow power leveling web for wow power leveling,WOrld of warcraft Power Leveling , Cheap WoW Gold here,buy wow gold,
喜欢这里!

TOP

TOP

age of conan

There is much controversy today about whether television has destroyed communication among family and friends. Some people argue that the communication is baffled by television. Others criticize that television improve the relationship of family and friends. I totally agree with the latter one. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>cheap AOC gold</a>
Watching TV is a time for the whole family to stop from whatever they are doing to get together. While watch TV, they start to talk about the content of the movie, which is also a kind of communication. Imagining the TV disappears,<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan account</a>
we would do reading in rooms, while our family would do laundry, do washing dish or going shopping. It simply provides a chance for us to stay together. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a>
In addition, the TV supplies us with much information, which we have not known in our daily lives. Some of the constructive movies show on TV, like a family story, a love story, or so on, reinforce the relationship of a family and friends.<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a> At last, it is a kind of recreation that a family or friends can choose to have. Communication can be undertaken by many means, for example, going shopping together, entering a bar, talking intimately about ourselves.

TOP

顶支持楼上的

顶,支持楼上的

TOP

lotro

After thirty years of married happiness, he could still remind himself that Victoria was endowed with every charm except the thrilling touch of human frailty. Though her perfection discouraged pleasures, especially the pleasures of love, he had learned in time to feel the pride of a husband in her natural frigidity. For he still clung, amid the decay of moral platitudes, to<a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>buy cheap lotro gold</a>
discredited ideal of chivalry. In his youth the world was suffused with the after-glow of the long Victorian age, and a graceful feminine style had softened the manners, if not the natures, of men. At the end of that interesting epoch, when womanhood was exalted from a biological f<a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>lord of rings gold</a>
into a miraculous power, Virginius Littlepage, the younger son of an old and affluent family, had married Victoria Brooke, the grand-daughter of a tobacco planter, who had made a satisfactory fortune by forsaking his plantation and converting tobacco into cigarettes. While Virginius had been trained by stern tradition to respect every woman who had not stooped to folly, the virtue peculiar to her sex was among the least of his reasons for admiring Victoria. She was not only modest, which was usual in the neties, but she was beautiful, which is unusual in any <a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>buy lord of rings gold</a>
the beginning of their acquaintance he had gone even further and ascribed intellect to her; but a few months of marriage had shown this to be merely one of the many delusions created by perfect features and noble expression. Everything about her had been smooth and definite, even the tones of her voice and the way her light brown hair, which she wore ?la Pompadour, was rolled stiffly<a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>cheap lord of rings gold</a>
m her forehead and coiled in a burnished rope on the top of her head. A serious young man, ambitious to attain a place in the world more brilliant than the secluded seat of his ancestors, he had been impressed at their first meeting by the compactness and precision of <a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>buy cheap lord of rings gold</a>

TOP

上海私家侦探,丽江旅游,丽江自助游,云南丽江

专业私家侦探就选上海私家侦探服务

选择好的旅游就选丽江旅游
丽江自助游
云南丽江

TOP

无缝管销售欢迎联系!

无缝管销售. ... 无缝管天津地区销售-生产各类无缝管. 天津无缝管销售基地,鼎盛无缝管销售中.销售无缝管、无缝钢管、锅炉管、流体管、厚壁无缝管等。无缝管联系电话:无缝管-华诚腾飞管材主营无缝管、合金管、锅炉管、高压锅炉管.我公司主要经营各类无缝管,自创办以来,一直坚持“以质量求生存,以诚信求发展”的经营理念销售无缝管,在无缝管行业内建立了良好的信誉,公司凭借对无缝管产品的了解,无缝管市场报价,钢厂价格,市场动态,市场分析等. ... 无缝管, 20# 8163, 159*4.5, 4750, 25, 鞍钢, 上海. 无缝管, 无缝钢管,不锈钢管-天津鸿鹏伟业高压合金管材销售有限公司是天津市钢管行业中创立最早,无缝钢管销售规模最大,实力最强的企业之一,无缝钢管和不锈钢管库存常年保持万吨 ,无缝管和无缝钢管销售处-天津无缝钢管厂直销处是华北地区一家专业无缝管,无缝钢管销售公司,公司专业销售无缝管及无缝钢管产品和高压合金管产品!天津无缝钢管厂直销处是专业经销无缝管和无缝钢管的企业,在天津无缝管和无缝钢管行业中有较高的信誉!最优最全的无缝管和无缝钢管尽在天津无缝钢管厂!
我的家:wow power leveling

TOP

沈飞航天024-31401808三色冰淇淋机,西雅斯冰淇淋机,软硬冰淇淋机网址www.9e9i.com

TOP

沈飞航天024-31401808三色冰淇淋机,西雅斯冰淇淋机,软硬冰淇淋机网址www.9e9i.com

TOP

发新话题