类:class
好处:减少重复代码的编写、可以更好地进行程序的维护、扩展和升级
class P:pass
P:类,定义个名字为P的类。
在内存中,会有一个存储P类所有的内容。
p1 = P()#实例化,从类P,生成一个具体的对象p1
p2 = P()#实例化,从类P,生成一个具体的对象p2
p3 = P()#实例化,从类P,生成一个具体的对象p3
P 是模板(模具),实例是通过这个模板(模具),添加了不同的初始化参数生产出来的具体产品。可以有多个,且每个具体产品可以有不同参数设定不同的造型。
class Person():
def __init__(self,name,sex,height,weight,bachelor):
self.name = name
self.sex = sex
self.height = height
self.weight = weight
self.bachelor = True
wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)
print(wulaoshi.sex)
print(lilaoshi.sex)
对于封装:
不用关心程序的内部实现,我们只需要调用函数或者类方法即可。
函数和类方法都可以实现封装:
函数只有操作某些数据的过程和算法,但是自己不保存数据。
类实例:
不但操作某些数据的过程和算法,且保存相关的数据。
类:
管理一组数据(可以是0个、1个或多个),以及提供操作这组数据的相关方法,数据和方法组成了类。
class Person():
def __init__(self,name,sex,height,weight,bachelor):
self.name = name
self.sex = sex
self.height = height
self.weight = weight
self.bachelor = True
wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)
print(wulaoshi.sex)
print(lilaoshi.sex)
wulaoshi.weight = str(int(wulaoshi.weight)+1)
print(wulaoshi.weight)
类定义的所有内容,在内存中仅有一份(类变量和类方法)。
类实例(多个),每个类实例在内存中都有自己的空间。
类的方法里面为啥要有个self呢?
wulaoshi.print_name()-------->在内存中找到类定义所在的内存位置,找到类里面定义的方法print_name(),self------>wulaoshi所在的内存地址,使用wulaoshi实例中的name值进行打印。
class Person():
def __init__(self,name,sex,height,weight,bachelor):
self.name = name
self.sex = sex
self.height = height
self.weight = weight
self.bachelor = True
def print_name(self):
print(self.name)
wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)
print(wulaoshi.sex)
print(lilaoshi.sex)
wulaoshi.weight = str(int(wulaoshi.weight)+1)
print(wulaoshi.weight)
wulaoshi.print_name()
写一个车的实例,实现:
初始化一个车里有多少油
调用run方法,实现车的运行,没油的时候提示需要加油了。跑一次里程数加100公里,油量减20升。
调用add_gas方法可以实现加油,让车继续运行。
check_distance方法查看车跑的里程数。
class CAR(object):
CAR_NUM = 0 #类变量
def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
self.name = name #实例变量
self.length = length
self.height = height
self.color = color
self.gas_num = gas_num #初始化汽油的数量
self.gas_consume_num = gas_consume_num #百公里油耗
self.distance = distance #车跑的里程数
def run(self):
if self.gas_num >= self.gas_consume_num:
self.distance+=100
self.gas_num -= 20
else:
print("没油了,请速加!")
def add_gas(self,n):
self.gas_num+=n
def check_distance(self):
return self.distance
def check_gas(self):
return self.gas_num
c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
for i in range(5): #跑5次
c.run()
print(c.check_gas())
print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
class CAR(object):
CAR_NUM = 0 #类变量
def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
self.name = name #实例变量
self.length = length
self.height = height
self.color = color
self.gas_num = gas_num #初始化汽油的数量
self.gas_consume_num = gas_consume_num #百公里油耗
self.distance = distance #车跑的里程数
def run(self):
if self.gas_num >= self.gas_consume_num:
self.distance+=100
self.gas_num -= 20
else:
print("没油了,请速加!")
def add_gas(self,n):
self.gas_num+=n
def check_distance(self):
return self.distance
def check_gas(self):
return self.gas_num
c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""for i in range(5): #跑5次
c.run()
print(c.check_gas())
print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""
l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())
c和l都在不同的内存空间里面,所以互相都不影响,都没关系。
class CAR(object):
CAR_DISTANCE_NUM = 0 #类变量
def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
self.name = name #实例变量
self.length = length
self.height = height
self.color = color
self.gas_num = gas_num #初始化汽油的数量
self.gas_consume_num = gas_consume_num #百公里油耗
self.distance = distance #车跑的里程数
def run(self):
if self.gas_num >= self.gas_consume_num:
self.distance+=100
self.gas_num -= 20
CAR.CAR_DISTANCE_NUM+=20
else:
print("没油了,请速加!")
def add_gas(self,n):
self.gas_num+=n
def check_distance(self):
return self.distance
def check_gas(self):
return self.gas_num
@classmethod
def get_total_distance(cls):
return CAR.CAR_DISTANCE_NUM
c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""
for i in range(5): #跑5次
c.run()
print(c.check_gas())
print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""
l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())
print(CAR.get_total_distance())
class CAR(object):
CAR_DISTANCE_NUM = 0 #类变量
def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
self.name = name #实例变量
self.length = length
self.height = height
self.color = color
self.gas_num = gas_num #初始化汽油的数量
self.gas_consume_num = gas_consume_num #百公里油耗
self.distance = distance #车跑的里程数
def run(self):
if self.gas_num >= self.gas_consume_num:
self.distance+=100
self.gas_num -= 20
CAR.CAR_DISTANCE_NUM+=100
else:
print("没油了,请速加!")
def add_gas(self,n):
self.gas_num+=n
def check_distance(self):
return self.distance
def check_gas(self):
return self.gas_num
@classmethod
def get_total_distance(cls):
return CAR.CAR_DISTANCE_NUM #不能使用self的实例变量
c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""
for i in range(5): #跑5次
c.run()
print(c.check_gas())
print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""
l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())
print(CAR.get_total_distance())
l.run()
print(CAR.get_total_distance())