编程范式:面向过程编程 函数式编程 面向对象编程
动作跟特征的结合 : 对象
类:特征+动作 (抽象) 模型
对象:类中具体的存在 实例
python中def 也是面向对象
面向对象编程:
class
用面向对象语言写程序 和一个程序的设计是面向对象的,两者是不相关的两件事。
声明函数:
def function(args):
'statement'
函数体
声明类:
class 类名:
'statement'
类体
__main__ 运行文件时的文件名
name()
函数的运行代表逻辑
类的运行产生实例
python2 区别经典类和新式类:新式类 class 类名(object):
类:描述一类事物,类的对象指的是这一类事物的一个个体
事物具有属性,属性分为:数据属性 函数属性
dir(类名) #列表
类名.__dict__ #属性字典
#python为类内置的特殊属性
类名.__name__# 类的名字(字符串)
类名.__doc__# 类的文档字符串
类名.__base__# 类的第一个父类(在讲继承时会讲)
类名.__bases__# 类所有父类构成的元组(在讲继承时会讲)
类名.__dict__# 类的字典属性
类名.__module__# 类定义所在的模块
类名.__class__# 实例对应的类(仅新式类中)
在class中,类名后不需要加形参,实例化时也可传入参数。
def __init__(self,parameter1,parameter2,...):
self.parameter1 = ***
self.parameter2 = ***
修改类属性
类名.类属性 = ‘’
删除
del 类名.类属性
增加函数属性
def func(self,para):
函数体
类名.函数属性 = func
修改函数属性
重新定义,覆盖
类名.旧函数属性 = 新函数属性
实例函数属性绑定类的函数属性
class中不能return
input尽量不放在class中
类属性
self.属性
p1 = class()
p1.list = [1,2,3] #p1.__dict__ {'list':[1,2,3],'name' = 'alex'}
p1.list.append('c') #没有新的赋值,是修改类属性
在类中的直接属性名仅是普通属性
类名.函数()#括号内需要加入参数
实例名.函数()#括号内不需加入参数
class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh
@property
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length
def test(self):
print('from test',self.name)
@classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag)
# print(Room.tag)
# Room.test(1) #1.name
# r1=Room('厕所','alex',100,100,100000)
Room.tell_info(10)
# r1=Room('厕所','alex',100,100,100000)
# r2=Room('公共厕所','yuanhao',1,1,1)
# # print('%s 住的 %s 总面积是%s' %(r1.owner,r1.name,r1.width*r1.length))
# # print('%s 住的 %s 总面积是%s' %(r2.owner,r2.name,r2.width*r2.length))
# # r1.cal_area()
# # r2.cal_area()
# print(r1.cal_area)
# print(r2.cal_area)
# print(r1.name)
# print(r2.name)
在class中的函数前加@property #跟实例绑定
可在后面不需加括号调用函数
在调用时看不到内部逻辑
@classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
类的函数属性前加@classmethod #跟类绑定
可在后面通过类调用类函数属性,Room.tell_info()
实例也可调用
class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh
@property
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length
@classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag)
@staticmethod
def wash_body(a,b,c):
print('%s %s %s正在洗澡' %(a,b,c))
def test(x,y):
print(x,y)
# Room.wash_body('alex','yuanhao','wupeiqi')
print(Room.__dict__)
r1=Room('厕所','alex',100,100,100000)
print(r1.__dict__)
# r1.wash_body('alex','yuanhao','wupeiqi')
# Room.test(1,2)
# r1.test(1,2)
@staticmethod
def wash_body(a,b,c):
print('%s %s %s正在洗澡' %(a,b,c))
跟实例和类都不绑定
类的工具包
类的组合
# class Hand:
# pass
#
# class Foot:
# pass
#
# class Trunk:
# pass
#
# class Head:
# pass
#
#
# class Person:
# def __init__(self,id_num,name):
# self.id_num=id_num
# self.name=name
# self.hand=Hand()
# self.foot=Foot()
# self.trunk=Trunk()
# self.head=Head()
# p1=Person('111111','alex')
# print(p1.__dict__)
# class School:
# def __init__(self,name,addr):
# self.name=name
# self.addr=addr
#
# def zhao_sheng(self):
# print('%s 正在招生' %self.name)
#
# class Course:
# def __init__(self,name,price,period,school):
# self.name=name
# self.price=price
# self.period=period
# self.school=school
#
#
#
# s1=School('oldboy','北京')
# s2=School('oldboy','南京')
# s3=School('oldboy','东京')
#
# # c1=Course('linux',10,'1h','oldboy 北京')
# c1=Course('linux',10,'1h',s1) #组合应用
#
# print(c1.__dict__)
# print(c1.school.name)
# print(s1)
class School:
def __init__(self,name,addr):
self.name=name
self.addr=addr
def zhao_sheng(self):
print('%s 正在招生' %self.name)
class Course:
def __init__(self,name,price,period,school):
self.name=name
self.price=price
self.period=period
self.school=school
s1=School('oldboy','北京')
s2=School('oldboy','南京')
s3=School('oldboy','东京')
# c1=Course('linux',10,'1h','oldboy 北京')
# c1=Course('linux',10,'1h',s1)
msg='''
1 老男孩 北京校区
2 老男孩 南京校区
3 老男孩 东京校区
'''
while True:
print(msg)
menu={
'1':s1,
'2':s2,
'3':s3
}
choice=input('选择学校>>: ')
school_obj=menu[choice]
name=input('课程名>>: ')
price=input('课程费用>>: ')
period=input('课程周期>>: ')
new_course=Course(name,price,period,school_obj)
print('课程【%s】属于【%s】学校' %(new_course.name,new_course.school.name))
在子类中调用父类:
class Vehicle:
Country='China'
def __init__(self,name,speed,load,power):
self.name=name
self.speed=speed
self.load=load
self.power=power
def run(self):
print('开动啦')
print('开动啦')
class Subway(Vehicle):
def __init__(self,name,speed,load,power,line):
Vehicle.__init__(self,name,speed,load,power)
self.line=line
def show_info(self):
print(self.name,self.speed,self.load,self.power,self.line)
def run(self):
Vehicle.run(self)
print('%s %s 线,开动啦' %(self.name,self.line))
line13=Subway('北京地铁','10km/s',1000000000,'电',13)
line13.show_info()
line13.run()
与上面方法相比,super()方法更好:
class Vehicle1:
Country='China'
def __init__(self,name,speed,load,power):
self.name=name
self.speed=speed
self.load=load
self.power=power
def run(self):
print('开动啦')
print('开动啦')
class Subway(Vehicle1):
def __init__(self,name,speed,load,power,line):
# Vehicle.__init__(self,name,speed,load,power)
# super().__init__(name,speed,load,power) #super(__class__,self).__init__(name,speed,load,power)
super(Subway,self).__init__(name,speed,load,power)
self.line=line
def show_info(self):
print(self.name,self.speed,self.load,self.power,self.line)
def run(self):
# Vehicle.run(self)
super().run()
print('%s %s 线,开动啦' %(self.name,self.line))
line13=Subway('北京地铁','10km/s',1000000000,'电',13)
line13.show_info()
line13.run()
print(line13.__class__)