python之面向对象
2021-01-25 01:15
标签:ips running 案例 asn 面向过程 qpi hci aci ini
class Students:
school = ‘宏福教育‘
def run(self):
# return ‘running......‘
print(‘running....‘)
stu1 = Students()
class Students:
school = ‘宏福教育‘
def __init__(self,name, age, job):
self.name = name
self.age = age
self.job = job
def run(self):
# return ‘running......‘
print(‘running....‘)
stu1 = Students(‘张三‘, 18, ‘IT‘)
stu2 = Students(‘王二‘, 16, ‘student‘)
stu3 = Students(‘赵五‘, 19, ‘teacher‘)
def __init__(self, name, weight):
self.name = name
self.weight = weight
def run(self):
self.weight -= 0.5
def eat(self):
self.weight += 1
def __str__(self):
return ‘名字:%s\n体重:%s\n‘ % (self.name, self.weight)
xm = Person(‘小明‘, 75)
xm.eat()
xm.run()
print(xm)
def __init__(self, name, area):
self.name = name
self.area = area
def __str__(self):
return ‘家具名称:%s\n占地面积:%s\n‘ % (self.name, self.area)
class House(object):
def __init__(self, house_type, total_area):
self.house_type = house_type
self.total_area = total_area
self.free_area = total_area
self.items = []
def add_item(self, item):
self.free_area = self.free_area - item.area
self.items.append(item.name)
def __str__(self):
return ‘房子类型:%s\n房子总大小:%s\n房间剩余面积:%s\n家具列表:%s\n‘ % (self.house_type, self.total_area, self.free_area, self.items)
bed = Items(‘席梦思‘, 10)
chest = Items(‘餐桌‘, 5)
house = House(‘两室一厅‘, 100)
house.add_item(bed)
house.add_item(chest)
print(house)
def __init__(self,gun_type):
self.gun_type = gun_type#初始化枪的型号
self.bullet = 0#初始化子弹的数量
def add_count(self, count):#添加子弹
self.bullet += count
def shoot(self):#射击
if self.bullet > 0:#假如子弹数量大于0
self.bullet -= 1# 那就射击,并且减少子弹数量
print(‘开火.......%s‘ % self.bullet)
else:
print(‘没有子弹啊,兄弟,别这么坑我啊???‘)#否则没有就显示没有子弹
class Soldier:#创建一个士兵的类
def __init__(self, name,gun=None):
self.name = name#初始化士兵的姓名
self.gun = gun#初始化枪
def fire(self):#开火方法
if self.gun:#假如有枪
self.gun.shoot()#那就射击
else:#不然就显示没有枪
print(‘没有枪啊,兄弟,你让我去送死吗??‘)
ak47 = Gun(‘AK47‘)#创建一个枪的对象叫ak47
ak47.add_count(10)#添加10颗子弹
xsd = Soldier(‘许三多‘)#实例化一个许三多的士兵对象
xsd.gun = ak47#把枪交到许三多手中
xsd.fire()#开火
下一篇:python字符串详解