标签:
逗号写错改过来即可 a=Student.new("张三",23,"男")
class Student
Version="1.0.0"
attr_accessor :name
attr_reader :no
attr_writer :gender
def initialize(name,no,gender)
@name=name
@no=no
@gender=gender
end
def sayHi
puts "我叫#{@name},学号是#{@no}"
end
def self.test
return "小名"
end
end
class Student
def bye
p "hello"
p "再见"
puts "再见"
end
end
a=Student.new("张三",23,"男")
a.sayHi
a.bye
从上面可知,直接再写一次类就能扩充该类的方法,因此给String类扩充两个方法并且覆盖原有name方法
puts String.name #调用未被修改前String的name方法
class String
def self.one
puts "小s"
end
def two
puts "2是two"
end
def self.name
puts "字符串类"
end
end
a=String.new("ceshi")
a.two
String.one
String.name
标签:
原文地址:http://www.cnblogs.com/blueColdFire/p/5164153.html