如何使用面向对象的方式进行设计
定义类/对象
class Aeroplane():
pass
aeroplane = Aeroplane()
class Aeroplane():
def __init__(self)
pass
aeroplane = Aeroplane()
class Aeroplane():
def __init__(self, name):
self.name = name
aeroplane = Aeroplane(
"Allen Moore")
print "Aeroplane's Name: ", aeroplane.name
Aeroplane's Name: Allen Moore
继承
class Aeroplane():
pass
class Boeing7x7(Aeroplane):
pass
class Airbus3x0(Aeroplane):
pass
get_a_boeing = Boeing7x7()
get_an_airbus = Airbus3x0()
class Aeroplane():
def exclaim(self):
print(
"I'm a Plane")
class Boeing7x7(Aeroplane):
pass
class Airbus3x0(Aeroplane):
pass
get_a_boeing = Boeing7x7()
get_an_airbus = Airbus3x0()
get_a_boeing.exclaim()
get_an_airbus.exclaim()
I'm a Plane
I'm a Plane
重载
重载方法
class Aeroplane():
def __init__(self, name):
self.name = name
class Boeing7x7(Aeroplane):
def __init__(self, name):
self.name =
"Plane 7X7 "+name
class Airbus3x0(Aeroplane):
def __init__(self, name):
self.name =
"Plane 3X0 "+name
get_a_plane = Aeroplane(
"Normal")
get_a_boeing = Boeing7x7(
"747")
get_an_airbus = Airbus3x0(
"320")
print "Plane Name: "+ get_a_plane.name
print "Boeing Name: "+ get_a_boeing.name
print "Airbus Name: "+ get_an_airbus.name
Plane Name: Normal
Boeing Name: Plane 7X7 747
Airbus Name: Plane 3X0 320
添加新成员
class Aeroplane():
def __init__(self, name):
self.name = name
class Boeing7x7(Aeroplane):
def __init__(self, name):
self.name =
"Plane 7X7 "+name
def get_a_pulse(self):
print "Get a Pulse"
get_a_boeing = Boeing7x7(
"747")
print get_a_boeing.get_a_pulse()
调用旧成员
class Aeroplane():
def __init__(self, name):
self.name = name
class Boeing7x7(Aeroplane):
def __init__(self, name, price):
super(Boeing7x7,self).__init__()
self.price = price
get_a_boeing = Boeing7x7(
"747",
100)
print get_a_boeing.name
print get_a_boeing.price
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-375953c61263> in <module>()
9 self.price = price
10
---> 11 get_a_boeing = Boeing7x7("747",100)
12 print get_a_boeing.name
13 print get_a_boeing.price
<ipython-input-22-375953c61263> in __init__(self, name, price)
6 class Boeing7x7(Aeroplane):
7 def __init__(self, name, price):
----> 8 super(Boeing7x7,self).__init__()
9 self.price = price
10
TypeError: super() argument 1 must be type, not classobj
操作属性
设置属性
property方法
class Aeroplane():
def __init__(self, input_name):
self.hidden_name =input_name
def get_name(self):
print "inside the getter, get the name"
return self.hidden_name
def set_name(self,input_name):
print "inside the setter, set the name"
self.hidden_name = input_name
name = property(get_name, set_name)
give_me_an_aeroplane = Aeroplane(
"Common Plane")
print give_me_an_aeroplane.name
print give_me_an_aeroplane.get_name()
give_me_an_aeroplane.name=
'Uncle Allen'
give_me_an_aeroplane.set_name(
"Uncle Allen")
inside the getter, get the name
Common Plane
inside the getter, get the name
Common Plane
inside the setter, set the name
decorator修饰符
class Aeroplane():
def __init__(self, input_name):
self.hidden_name =input_name
@property
def name(self):
print "inside the getter, get the name"
return self.hidden_name
@name.setter
def name(self,input_name):
print "inside the setter, set the name"
self.hidden_name = input_name
give_me_an_aeroplane = Aeroplane(
"Common Plane")
print give_me_an_aeroplane.name
give_me_an_aeroplane.name=
'Uncle Allen'
print give_me_an_aeroplane.name
优点: * 一处更改,处处更新 * 不设设置,不容设置
class Aeroplane():
def __init__(self, name, size):
self.name = name
self.size = size
@property
def used_oils(self):
return 100*self.size
give_me_an_aeroplane = Aeroplane(
"Common Plane",
100)
print give_me_an_aeroplane.used_oils
give_me_an_aeroplane.size=
10
print give_me_an_aeroplane.used_oils
class Aeroplane():
def __init__(self, name, size):
self.name = name
self.size = size
@property
def used_oils(self):
return 100*self.size
give_me_an_aeroplane = Aeroplane(
"Common Plane",
100)
print give_me_an_aeroplane.used_oils
give_me_an_aeroplane.size=
10
10000
封装
保护私有特性
class Aeroplane():
def __init__(self, input_name):
self.__name =input_name
@property
def name(self):
print "inside the getter, get the name"
return self.__name
@name.setter
def name(self,input_name):
print "inside the setter, set the name"
self.__name = input_name
give_me_an_aeroplane = Aeroplane(
"Common Plane")
print give_me_an_aeroplane.name
give_me_an_aeroplane.name=
"HQ Plane"
print give_me_an_aeroplane.name
print give_me_an_aeroplane._Aeroplane__name
inside the getter, get the name
Common Plane
HQ Plane
Common Plane
多态polymorphism
class Aeroplane():
def __init__(self, name, size):
self.name = name
self.size = size
def who(self):
return self.name
def which(self):
return "xxx-"+self.size
class Airbus3x0(Aeroplane):
def which(self):
return "3x0-"+self.size
class Boeing7x7(Aeroplane):
def which(self):
return "7x7-"+self.size
plane = Aeroplane(
"Common Plane",
"0")
print "Name: "+plane.who()
print "Size: "+plane.which()
planeA = Airbus3x0(
"Airbus",
"340")
print "Name: "+planeA.who()
print "Size: "+planeA.which()
planeB = Boeing7x7(
"Boeing",
"747")
print "Name: "+planeB.who()
print "Size: "+planeB.which()
def call_plane(obj):
print "Name: "+obj.who()+
"\n"+
"Size: "+obj.which()
call_plane(plane)
call_plane(planeA)
call_plane(planeB)
Name: Common Plane
Size: xxx-0
Name: Airbus
Size: 3x0-340
Name: Boeing
Size: 7x7-747
Name: Common Plane
Size: xxx-0
Name: Airbus
Size: 3x0-340
Name: Boeing
Size: 7x7-747
对象之间的关系
组合 composition
聚合 aggregation
class Color():
def __init__(self, color):
self.color = color
class Size():
def __init__(self, size):
self.size = size
class Aeroplane():
def __init__(self, color, size):
self.color = color
self.size = size
def about(self):
print "The Fruit is ", color.color,
"and ", size.size
color = Color(
"Yellow")
size = Size(
"Big")
aeroplane = Aeroplane(color, size)
aeroplane.about()
The Fruit is Yellow and Big
其他
方法分类
实例方法(instance method)
类方法(class method)
class Aeroplane():
count =
0
def __init__(self):
Aeroplane.count +=
1
def exclaim(self):
print "This is an aeroplane."
@classmethod
def account(cls):
print "They have ",cls.count,
" aeroplanes."
aero_a = Aeroplane()
aero_b = Aeroplane()
aero_c = Aeroplane()
print Aeroplane.account()
They have 3 aeroplanes.
静态方法(static method)
class Aeroplane():
def __init__(self,name):
self.name = name
def exclaim(self):
print "This is an aeroplane."
@staticmethod
def hello():
print "Welcome!"
Aeroplane.hello()
Welcome!
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-72-5eb75aa8c2ed> in <module>()
10
11 Aeroplane.hello()
---> 12 hello()
NameError: name 'hello' is not defined
特殊方法(special method)/魔术方法(magic method)
class BarCodes():
def __init__(self, code):
self.code = code
def equals(self, code):
return self.code.lower() == code.code.lower()
first = BarCodes(
"ha")
second = BarCodes(
"Ha")
first.equals(second)
class BarCodes():
def __init__(self, code):
self.code = code
def __eq__(self, code):
return self.code.lower() == code.code.lower()
first = BarCodes(
"ha")
second = BarCodes(
"Ha")
first == second
True
命名元祖
from collections
import namedtuple
Fruit = namedtuple(
"Fruit",
"color size")
fruit = Fruit(
'Yellow',
'Big')
print fruit
print fruit.color
print fruit.size
parts = {
"color":
"Yellow",
"size":
"Big"}
fruit = Fruit(**parts)
print fruit
print fruit.color
print fruit.size
fruit1 = fruit._replace(color=
"Green", size=
"Big")
print fruit1
print fruit1.color
print fruit1.size
parts[
"status"]=
"Fresh"
print parts
fruit2 = Fruit(**parts)
print fruit2
print fruit2.color
print fruit2.size
print fruit2.status
Fruit(color='Yellow', size='Big')
Yellow
Big
Fruit(color='Yellow', size='Big')
Yellow
Big
Fruit(color='Green', size='Big')
Green
Big
{'color': 'Yellow', 'status': 'Fresh', 'size': 'Big'}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-5349e1cad48d> in <module>()
24 print parts
25
---> 26 fruit2 = Fruit(**parts)
27 print fruit2
28 print fruit2.color
TypeError: __new__() got an unexpected keyword argument 'status'