Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142154
  • 博文数量: 43
  • 博客积分: 810
  • 博客等级: 军士长
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-03 02:39
文章分类
文章存档

2011年(43)

分类: Python/Ruby

2011-07-18 16:22:13

 以前都写的面向过程,一下转到OOP还真不适应,于是小练了一下,跟着书本都搞错了郁闷

不动手不知道,一动手吓一跳!mark下来椅子鞭策

 

1python\method.py", line 4

    p = Person()

               ^

IndentationError: unindent does not match any outer indentation level

因为将调用类的语句错误的缩进在类里面导致报错,修改缩进后通过

 

 

2inherit.py", line 21, in tell

    print 'Salary:"%d"' % self.salary

AttributeError: Teacher instance has no attribute 'salary'

变量的大小写搞错,导致无法识别变量

 

 

3TypeError: _new_() takes exactly 3 arguments (2 given)

函数定义__多了下划线___ __导致

 

 

代码:

  1. class SchoolMember:

  2.     '''Represents any school member.'''

  3.     def __init__(self,name,age):

  4.         self.name = name

  5.         self.age = age

  6.         print '(Initialized SchoolMember: %s)' %self.name



  7.     def tell(self):

  8.         '''Tell my details.'''

  9.         print 'Name:"%s" Age:"%s"' %(self.name,self.age),



  10. class Teacher(SchoolMember):

  11.     '''Represents a teacher . '''

  12.     def __init__(self, name, age, salary):

  13.         SchoolMember.__init__(self,name,age)

  14.         self.salary = salary

  15.         print '(Initialized Teacher: %s)'%self.name



  16.     def tell(self):

  17.         SchoolMember.tell(self)

  18.         print 'Salary:"%d"' % self.salary



  19. class Student(SchoolMember):

  20.     '''Represents a student.'''

  21.     def __init__(self,name,age,marks):

  22.         SchoolMember.__init__(self,name,age)

  23.         self.marks = marks

  24.         print 'Initialized Student: %s' %self.name



  25.     def tell(self):

  26.         SchoolMember.tell(self)

  27.         print 'Marks: "%d"' %self.marks





  28. t = Teacher('Mrs.Hw',28,1500)

  29. s = Student('Cx',18,80)



  30. print #prints a blank line



  31. members = [t,s]

  32. for member in members:

  33.     member.tell()#works for both Teachers and Students

  

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