- class SpecialTest
- def info
- puts "This is SpecialTest method"
- end
- end
- #实例化两个对象
- obj1 = SpecialTest.new
- obj2 = SpecialTest.new
- #两个对象的方法相同
- obj1.info
- obj2.info
- #直接通过对象来修改对象的方法,而不必修改类的方法,同时该方法只属于特殊类obj2
- def obj2.info
- puts "This is obj2"
- end
- obj1.info
- obj2.info
执行结果:
This is SpecialTest method
This is SpecialTest method
This is SpecialTest method
This is obj2
阅读(655) | 评论(0) | 转发(0) |