以前都写的面向过程,一下转到OOP还真不适应,于是小练了一下,跟着书本都搞错了郁闷
不动手不知道,一动手吓一跳!mark下来椅子鞭策
1、python\method.py", line 4
p = Person()
^
IndentationError: unindent does not match any outer indentation level
因为将调用类的语句错误的缩进在类里面导致报错,修改缩进后通过
2、inherit.py", line 21, in tell
print 'Salary:"%d"' % self.salary
AttributeError: Teacher instance has no attribute 'salary'
变量的大小写搞错,导致无法识别变量
3、TypeError: _new_() takes exactly 3 arguments (2 given)
函数定义__多了下划线___ __导致
代码:
- class SchoolMember:
- '''Represents any school member.'''
- def __init__(self,name,age):
- self.name = name
- self.age = age
- print '(Initialized SchoolMember: %s)' %self.name
- def tell(self):
- '''Tell my details.'''
- print 'Name:"%s" Age:"%s"' %(self.name,self.age),
- class Teacher(SchoolMember):
- '''Represents a teacher . '''
- def __init__(self, name, age, salary):
- SchoolMember.__init__(self,name,age)
- self.salary = salary
- print '(Initialized Teacher: %s)'%self.name
- def tell(self):
- SchoolMember.tell(self)
- print 'Salary:"%d"' % self.salary
- class Student(SchoolMember):
- '''Represents a student.'''
- def __init__(self,name,age,marks):
- SchoolMember.__init__(self,name,age)
- self.marks = marks
- print 'Initialized Student: %s' %self.name
- def tell(self):
- SchoolMember.tell(self)
- print 'Marks: "%d"' %self.marks
- t = Teacher('Mrs.Hw',28,1500)
- s = Student('Cx',18,80)
- print #prints a blank line
- members = [t,s]
- for member in members:
- member.tell()#works for both Teachers and Students
阅读(1544) | 评论(0) | 转发(0) |