Chinaunix首页 | 论坛 | 博客
  • 博客访问: 62417
  • 博文数量: 33
  • 博客积分: 841
  • 博客等级: 准尉
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-20 20:54
文章分类

全部博文(33)

文章存档

2011年(33)

分类: Python/Ruby

2011-02-18 15:48:24

  1. class SpecialTest
  2.   def info
  3.    puts "This is SpecialTest method"
  4.   end
  5. end
  6. #实例化两个对象
  7. obj1 = SpecialTest.new
  8. obj2 = SpecialTest.new
  9. #两个对象的方法相同
  10. obj1.info
  11. obj2.info
  12. #直接通过对象来修改对象的方法,而不必修改类的方法,同时该方法只属于特殊类obj2
  13. def obj2.info
  14.   puts "This is obj2"
  15. end
  16. obj1.info
  17. obj2.info
执行结果:

This is SpecialTest method

This is SpecialTest method

This is SpecialTest method

This is obj2

阅读(613) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~