面向对象
- Python是一种完全面向对象的语言,即使是数据类型也被当做对象。定义在类中的函数被称为方法。
创建类、声明对象
- 使用class关键字来声明一个类,即
class className:
method
variable
obj = className() # declaer a objection
初始化对象
- 在Python中使用 init(self,objvariable1,objvariable,…)方法来初始化创建好的对象,当对象被创建后,将会立刻使用该方法来初始化对象。self变量就是创建的对象本身,与C++中的this是一个意思。
回收对象
- Python用 del(self)来回收对象,即当对象不再使用的时候,Python自动调用该方法回收对象所占用的空间。
类变量与对象变量
-
类变量与对象变量其作用范围有所不同,类变量为该类所有对象共有,对象变量只为该对象共有,类变量与类中的方法是并列关系,对象变量在初始化对象的时候指明。
-
程序
class dog:
count = 0 #declear a global variable 'count'
def __init__(self,name,count): #a objection has two objection variables 'name' and 'count'
self.name = name
self.count = count+1
dog.count = dog.count+1
def __del__(self):
print '''I'm %s, bye''' % self.name
def howMany(self):
print '''I'm %s, the count of dog is %d''' % (self.name,dog.count)
toy=dog('toy',0)
toy.howMany()
print 'count%d'%toy.count
kerian=dog('kerian',0)
kerian.howMany()
print 'count:%d' % kerian.count
- 运行结果
I'm toy, the count of dog is 1
count:1
I'm kerian, the count of dog is 2
count:1
I'm kerian, bye
I'm toy, bye
继承
- 如果已经定义了一个class A,而class B是继承class A的,则class B的定义方式:
class B(A):
...
...
- 程序
class Person():
def __init__(self,name,age):
self.name=name
self.age=age
def __del__(self):
print '''I'm %s,bye''' % self.name
def tell(self):
print '''My name is %s, I'm %d years old''' % (self.name,self.age),
class Student(Person):
def __init__(self,name,age,mark):
Person.__init__(self,name,age)
self.mark = mark
def tell(self):
Person.tell(self)
print 'My mark is %d' % self.mark
S = Student('kerian',18,100)
S.tell()
- 运行结果
My name is kerian, I'm 18 years old My mark is 100
I'm kerian,bye
异常
- 程序有些时候会引发一些异常,如当我们需要打开某些串口的时候,这些串口恰巧被占用,那么将会引发一些错误,所以可以使用异常来处理。
try … except
- 使用try…except语句来处理异常,将可能会引发错误的语句放在try块中,将异常处理放在except中,except可以处理单个异常也可以处理很多异常,最后还可以跟一个else从句,即如果没有引发异常,则执行else从句中的内容。
- 程序
class ShortInputException(Exception):
'''this class inherits from Exception class'''
def __init__(self,length,atleast):
Exception.__init__(self) # initialize the class
self.length=length
self.atleast=atleast
try:
s = raw_input('Enter->>') #input a string
if len(s) < 3:
x = ShortInputException(len(s),3) # create a object
raise x #raise a exception
except EOFError: # input a unexpected end
print '\nWhy did you do an EOF on me?'
except ShortInputException: #raise a exception because the length of input strin
g less atleast
print 'ShortInputException: the at least of input length is %d' % x.atleast
else:
print 'No exception was raised'
- 运行结果
Enter->>ab
ShortInputException: the at least of input length is 3
Enter->>abc
No exception was raised
try…finally
- 如果希望无论是否引发异常都要进行一些同样的处理,那么可以使用try finally语句
- 程序
class ShortInputException(Exception):
'''this class inherits from Exception class'''
def __init__(self,length,atleast):
Exception.__init__(self) # initialize the class
self.length=length
self.atleast=atleast
try:
s = raw_input('Enter->>') #input a string
if len(s) < 3:
x = ShortInputException(len(s),3) # create a object
raise x #raise a exception
except EOFError: # input a unexpected end
print '\nWhy did you do an EOF on me?'
except ShortInputException: #raise a exception because the length of input string less atleast
print 'the atleast of length is %d' % x.atleast
finally:
print 'input is %s' % s
- 输出结果
Enter->>a
the atleast of length is 3
input is a
Enter->>adc
input is adc