16 12
发新话题
打印

Ruby学习笔记04 类变量和类方法

Ruby学习笔记04 类变量和类方法

6. 类变量和类方法
--------------------
  6.1 Ruby类也允许定义类变量和方法。
======
一个类变量允许在一个类的所有实例间共享单个变量。在Ruby中,两个@@号用于指示类变量。例如,如果你想要使一个BankAccount类的所有实例共享相同的利息率,那么该类可能被如下定义:

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

  6.2 继承
======
例子:Square类继承Rectangle

Square类可以使用下列定义创建:

class Square < Rectangle
end

重载现有的Square的initialize方法:

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

  6.3 所有类的基类 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

鄂式破碎机 冲击式破碎机

机制砂生产线、碎石设备生产线、机制砂生产线、碎石机、破碎机、第三代制沙机

TOP

反击式破碎机 反击式破碎机价格

机制砂生产线、碎石设备生产线、机制砂生产线、碎石机、破碎机、第三代制沙机

TOP

节能球磨机 高能球磨机

选矿设备选铁设备选矿生产线球磨机价格报价

TOP

dofus 2 ha

so do  you sell dofus kamas,if you have cheap dofus kamas , please connect me as soon as possible!
hihi^^
i want to buy more and more , more and more!
so please take your time !
do you have dofus money, dofus gold ? i want them , i love them !
i have change a new envionment , it's new ,so it's interesting and changllege to me!

TOP

UR dofus

dofus kamas

cool day, haha!
i play dofus online game, so i need buy dofus kamas , buy cheap dofus kamas,
dofus money

TOP

青岛轴承、青岛SKF轴承、青岛SKF进口轴承

TOP

dofus 2 ha

so do  you sell dofus kamas,if you have cheap dofus kamas , please connect me as soon as possible!
hihi^^
i want to buy more and more , more and more!
so please take your time !
do you have dofus money, dofus gold ? i want them , i love them !
i have change a new envionment , it's new ,so it's interesting and changllege to me!

TOP

北京辉乐豪不锈钢制品有限公司4.

在查询框中可填入的字符仅包括英文字母,玻璃隔断阿拉伯数字和减号。
“-”(减号)玻璃隔断不能出现在字符串的最前或最后。
在查询框中填入的字符串最长不能超过63个字符。
个人也可以注册国际域名,填写注册单位名称一栏时使用个人的姓名即可。
在您注册完毕玻璃隔断,并完成了域名付费工作后,玻璃隔断3天内有尝删除费用为10/元,玻璃隔断3天后域名的费用将无法退还!
详细请查询国际域名注册协议[WORD]

TOP

age of conan2

Some movies are serious, designed to make the audience think. Other movies are designed primarily to amuse and entertain. Which type of movie do you prefer? Use specific reasons and examples to support your answer.<a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan gold</a>
Comedies make one laugh. People can just relax, buy a bottle of coda and some popcorn and enjoy those moments. It is a means of eliminating stress and tension.<a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>AOC gold</a>
Usually, we go to the movie theatre once a month because in this case we always find new comedies to watch. I like those moments when it is dark and one just can enjoys the actions from a large screen. <a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan money</a>
I do not want to say that I do not like movies that make me think. Sometimes I really like the plot or performance.<a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>buy age of conan gold</a>
Usually, we go to the movie theatre once a month because in this case we always find new comedies to watch. I like those moments when it is dark and one just can enjoys the actions from a large screen. <a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>cheap age of conan gold</a>I do not want to say that I do not like movies that make me think. Sometimes I really like the plot or performance. For example, "Traffic", a movie about drugs and young people, is very touching and topical.

TOP

eve isk

Where to Live--in the City or in the Country<a href=http://www.fast-wowgold.com/dofus-kamas.htm>dofus gold</a>
Some people enioY the convenience of the city. First, there are many public places of entertainment, such as parks, cinemas, exhibitions, nightclubs and so on.<a href=http://www.just4gold.com>eve isk</a>
And there ale also quite a number of schools, universities and scientific research institutions that provide good education. Second, the public transportation facilities are very convenient.<a href=http://www.just4gold.com>eve online isk</a>
Taxicabs, buses, trolley buses, and subways can quickly take people anyplace in the city.<a href=http://www.just4gold.com>cheap eve isk</a>in the city. And third, shops and department stores are well stocked with goods of all kinds, high in quality and low in price.
    Some people like the rural scenery in the country.
First, it is close to the outdoors, the air being fresh and pure. Second, it is free from the noise and air pollution and traffic jams of the city. Third, gardening and fishing here are pleasant pastimes.<a href=http://www.just4gold.com>cheap eve online isk</a> But the city and country have their own disadvantages. For examples, the pollution and traffic jam have become more and more serious in the city, while in the country life is relatively backward. Comparing this with that. however. I prefer to live in the city.

TOP

顶。

哈哈哈,赞。
数据恢复,找我!

TOP

帮助女人营养肌肤

谁是美容美食大王?吃什么装修公司可以帮助女人营养肌肤?哪些是新科空调维修你不知道的美肤食品?让我们一起来看看这些洗衣机维修对你肌肤有益的食物吧!

TOP

 16 12
发新话题