2018-08-13

这周我们学子了
文件打开与关闭

f = open('test.txt','w')
关闭文件
close
#新建一个文件,文件名为:test.txt
f = open('test.txt','w')
#关闭这个文件
f.close()

文件的读写

f = open('test.txt','w')
f.write('hello world')

老王开枪

class Ren:
def __init__(self,name):
self.name = name
self.xue = 100
self.qiang = None

def __str__(self):
return self.name + "剩余血量为:" + str(self.xue)

def anzidan(self,danjia,zidan):
danjia.baocunzidan(zidan)

def andanjia(self,qiang,danjia):
qiang.lianjiedanjia(danjia)

def naqiang(self,qiang):
self.qiang = qiang

def kaiqiang(self,diren):
self.qiang.she(diren)

def diaoxue(self,shashangli):
self.xue -= shashangli

#弹夹类
class Danjia:
def __init__(self, rongliang):
self.rongliang = rongliang
self.rongnaList = []

def __str__(self):
return "弹夹当前的子弹数量为:" + str(len(self.rongnaList)) + "/" + str(self.rongliang)

def baocunzidan(self,zidan):
if len(self.rongnaList) < self.rongliang:
self.rongnaList.append(zidan)

def chuzidan(self):
#判断当前弹夹中是否还有子弹
if len(self.rongnaList) > 0:
#获取最后压入到单间中的子弹
zidan = self.rongnaList[-1]
self.rongnaList.pop()
return zidan
else:
return None

#子弹类
class Zidan:
def __init__(self,shashangli):
self.shashangli = shashangli

def shanghai(self,diren):
diren.diaoxue(self.shashangli)

#枪类
class Qiang:
def __init__(self):
self.danjia = None

def __str__(self):
if self.danjia:
return "枪当前有弹夹"
else:
return "枪没有弹夹"

def lianjiedanjia(self,danjia):
if not self.danjia:
self.danjia = danjia


def she(self,diren):
zidan = self.danjia.chuzidan()
if zidan:
zidan.shanghai(diren)
else:
print("没有子弹了,放了空枪....")


#创建一个人对象
laowang = Ren("老王")

#创建一个弹夹
danjia = Danjia(20)
print(danjia)

#循环的方式创建一颗子弹,然后让老王把这颗子弹压入到弹夹中
i=0
while i<5:
zidan = Zidan(5)
laowang.anzidan(danjia,zidan)
i+=1
#测试一下,安装完子弹后,弹夹中的信息
print(danjia)

#创建一个枪对象
qiang = Qiang()
print(qiang)
#让老王,把弹夹连接到枪中
laowang.andanjia(qiang,danjia)
print(qiang)


#创建一个敌人
diren = Ren("敌人")
print(diren)

#让老王拿起枪
laowang.naqiang(qiang)

#老王开枪射敌人
laowang.kaiqiang(diren)
print(diren)
print(danjia)

laowang.kaiqiang(diren)
print(diren)
print(danjia)

四个魔法方法

__init__()初始化属性
__str__()打印对象调用,要有返回值
__del__()删除对象调用,程序结束后自动会调用
__new__()创建对象

你可能感兴趣的:(2018-08-13)