Ruby语言经典代码讲座:理解类

『》』理解类
『《』我们的实例代码如下:
class Auto
def Auto.max_length
puts "我是Auto.max_length!"
end

def length
puts "我是Auto.new.length!"
end
end

Auto.max_length
Auto.new.length

#=>我是Auto.max_length!
#=>我是Auto.new.length!

说明:
(一)我们可以使用类名Auto调用方法;
(二)我们也可以使用new调用方法;
(三)我们为什么要直接使用类名调用方法;

Comments

Popular posts from this blog

英语字母a的发音