一、什么是面向对象的程序设计
1. 概念:
(1)对象
对象是人们要进行研究的任何事物,从简单的整数到复杂的飞机均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则、计划或事件。
(2)对象的状态和行为
对象具有状态,一个对象用数据值来描述它的状态。
对象还有操作,用于改变对象的状态,对象及其操作就是对象的行为。
对象实现了数据和操作的结合,使数据和操作封装于对象的统一体中。
(3)类
具有相同特性(数据元素)和行为(功能)的对象的抽象就是类。因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型。
类具有属性,它是对象的状态的抽象,用数据结构来描述类的属性。
类具有操作,它是对象的行为的抽象,用操作名和实现操作的方法来描述。
(4)类的结构
在客观世界中有若干类,这些类有一定的结构关系。通常有两种主要的结构关系,即一般—具体结构关系,整体—部分结构关系。
①一般—具体结构称为分类结构,也可以说是"或"关系,或者是"is - a"关系。
②整体—部分结构称为组装结构,它们之间关系是一种"与"关系,或者是"has a"关系。
(5)消息和方法
对象之间进行通信的结构叫做消息。在对象操作中,当一个消息发送给某个对象时,消息包含接收对象去执行某种操作的信息。发送一条消息至少包括说明接受消息的对象名、发送给该对象的消息名(即对象名、方法名)。一般还要对参数加以说明,参数可以是认识该消息的对象所知道的变量名,或者是所有对象都知道的变量名。
类中操作的实现过程叫做方法,一个方法有方法名、返回值、参数、方法体。
2. 特征
(1)对象的唯一性
每个对象都有自身唯一的标识,通过这种标识,可找到相应的对象。在对象的整个生命期中,它的标识都不改变,不同的对象不能有相同的标识。
(2)抽象性
抽象性是指将具有一致的数据结构(属性)和行为(操作)的对象抽象成类。一个类就是这样一种抽象,它反映了与应用有关的重要性质,而忽略其他一些无关内容。任何类的划分都是主观的,但必须与具体的应用有关。
(3)继承性
继承性是子类自动共享父类数据结构和方法的机制,这是类之间的一种关系。在定义和实现一个类的时候,可以在一个已经存在的类的基础之上来进行,把这个已经存在的类所定义的内容作为自己的内容,并加入若干新的内容。
继承性是面向对象程序设计语言不同于其它语言的最重要的特点,是其他语言所没有的。
在类层次中,子类只继承个父类的数据结构和方法,则称为单重继承。
在类层次中,子类继承了多个父类的数据结构和方法,则称为多重继承。
多重继承,JAVA、VB、NET、Objective-C均仅支持单继承,注意在C++多重继承时,需小心二义性。
在软件开发中,类的继承性使所建立的软件具有开放性、可扩充性,这是信息组织与分类的行之有效的方法,它简化了对象、类的创建工作量,增加了代码的可重用性。
采用继承性,提供了类的规范的等级结构。通过类的继承关系,使公共的特性能够共享,提高了软件的重用性。
(4)多态性(多形性)
多态性是指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息可以产生不同的结果,这种现象称为多态性。
多态性允许每个对象以适合自身的方式去响应共同的消息。
多态性增强了软件的灵活性和重用性。
# 用函数作用域实现面向对象
def school(name,address,numbers):
def study(school):
print('%s的学生在%s上课' %(school['name'],school['address']))
def number(school):
print('%s的学生数量有%d' %(school['name'],school['number of students']))
def nature(name,address,numbers):
school = {
'name':name,
'type': 'college',
'address': address,
'number of students':numbers,
'study':study,
'number':number
}
return school
res = nature(name,address,numbers)
return res
a1 = school('cqitd','cq','20000')
a1['study'](a1) #cqitd的学生在cq上课
a2 = school('nh','nj','30000')
a2['number'](a2) #nh的学生数量有30000
二、面向对象设计和面向对象编程
1. 面向对象设计
把数据属性和函数属性整合到一起,形成一个类。通过这个类实例化对象。
2. 面向对象编程
用"class"定义类的方式来编写面向对象设计这种形式的程序。
# 实例化
class NBA:
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
a = NBA('Lakers','James','Wotton')
a.training()
a.match()
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
print(NBA.__name__) #NBA 类的名字
print(NBA.__doc__) #这是一个NBA的类 类的文档字符串
print(NBA.__base__) #<class 'object'> 类的第一个父类
print(NBA.__bases__) #(<class 'object'>,) 类的所有父类构成的元组
print(NBA.__dict__) #{'__module__': '__main__', '__doc__': '这是一个NBA的类', 'country': 'US', '__init__': <function NBA.__init__ at 0x03993468>, 'training': <function NBA.training at 0x03993348>, 'match': <function NBA.match at 0x03993390>, '__dict__': <attribute '__dict__' of 'NBA' objects>, '__weakref__': <attribute '__weakref__' of 'NBA' objects>} 类的属性字典
print(NBA.__module__) #__main__ 类定义所在的模块
print(NBA.__class__) #<class 'type'> 实例对应的类
三、类属性和实例属性的增删改查
1. 类属性的增删改查
# 查
class NBA:
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
print(NBA.country) #US
# 增
class NBA:
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
NBA.area = 'North America'
print(NBA.area) #North America
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
def eat(self):
print('%s在吃汉堡' %self.captain)
p1 = NBA('Lakers','James','th')
NBA.eat = eat
p1.eat() #th在吃汉堡
# 改
class NBA:
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
NBA.country = 'America'
print(NBA.country) #America
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
def match(self):
print('sichuan was beated %s' %self.name)
p1 = NBA('Lakers','James','th')
NBA.match = match
p1.match() #sichuan was beated Lakers
# 删
class NBA:
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
del NBA.country
print(NBA.__dict__) #{'__module__': '__main__', '__init__': <function NBA.__init__ at 0x02CA4468>, 'training': <function NBA.training at 0x02CA4348>, 'match': <function NBA.match at 0x02CA4390>, '__dict__': <attribute '__dict__' of 'NBA' objects>, '__weakref__': <attribute '__weakref__' of 'NBA' objects>, '__doc__': None}
2. 实例属性的增删查
# 查
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
p1 = NBA('Lakers','James','th')
print(p1.country) #US
print(p1.name) #Lakers
# 增
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
p1 = NBA('Lakers','James','th')
p1.time = '20181008'
print(p1.time)
# 删
class NBA:
'这是一个NBA的类'
country = 'US'
def __init__(self,name,captain,coach):
self.name = name
self.captain = captain
self.coach = coach
def training(self):
print('%s在训练%s' %(self.coach,self.captain))
def match(self):
print('%s won sichuan' %self.name)
p1 = NBA('Lakers','James','th')
del p1.name
print(p1.__dict__)
四、静态属性、类方法、静态方法
1. 静态属性
class Room:
def __init__(self,name,length,width,heigh):
self.name= name
self.length = length
self.width = width
self.heigh = heigh
@property
def cal_area(self):
return self.length * self.width
p1 = Room('照母山',10,10,4)
print(p1.cal_area)
2. 类方法
class test:
tag = '66'
def __init__(self,c1,c2,c3):
self.c1 = 'a'
self.c2 = 'b'
self.c3 = 'c'
@classmethod
def test_info(cls):
print(cls.tag)
test.test_info()
3. 静态方法
class test:
def __init__(self,name,age):
self.name = name
self.age = age
@staticmethod
def washing(a,b,c):
print('%s %s %s正在洗澡' %(a,b,c))
test.washing('my','mht','lqd')
五、重写函数(__str__和__repr__)
#__str__
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print('%s在吃饭' %self.name)
def run(self):
print('%s在跑' %self.name)
def __str__(self):
return 'my name is %s' %self.name
p1 = Person('th', 24)
print(p1) #my name is th
#__repr__(在__str__和__repr__都存在时,优先调用__str__;在python解释器里输入对象名后回车自动调用__repr__)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print('%s在吃饭' %self.name)
def run(self):
print('%s在跑' %self.name)
def __repr__(self):
return 'my name is %s' %self.name
def __str__(self):
return 'my age is %d' % self.age
p1 = Person('th', 24)
print(p1) #my age is 24
六、访问限制
class Person:
def __init__(self, name, money):
self.name = name
self.__money = money
p1 = Person('my', 999999999999)
print(p1.__money) #报错
class Person:
def __init__(self, name, money):
self.name = name
self.__money = money
def getMoney(self):
print(self.__money)
def setMoney(self, money):
self.__money = money
p1 = Person('my', 999999999999)
p1.getMoney() #999999999999
p1.setMoney(100)
p1.getMoney() #100
七、继承
1. 单继承
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print('%s在吃饭' %self.name)
def run(self):
print('%s在跑' %self.name)
class Doctor(Person):
def __init__(self, name, age, salary):
super(Doctor, self).__init__(name, age)
self.salary = salary
class Teacher(Person):
pass
p1 = Doctor('jhh', 32, 10000)
print(p1.name) #jhh
p2 = Teacher('msh', 40)
p2.eat() #msh在吃饭
2. 多继承
class Father:
def __init__(self, skill):
self.skill = skill
def driving(self):
print('开车')
class Mother:
def __init__(self, money):
self.money = money
class Child(Father, Mother):
def __init__(self, skill, money):
Father.__init__(self, skill)
Mother.__init__(self, money)
C1 = Child('repair', 999999)
print(C1.money, C1.skill) #999999 repair
C1.driving() #开车