做一个案例
名词是属性: 名字 体重
动词是方法: 吃 跑步
class Person:
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return('我是%s,体重%.2f公斤' % (self.name,self.weight))
def run(self):
pass
def eat(self):
pass
xiaoming = Person('小明',75.0)
xiaoming.eat()
xiaoming.run()
print(xiaoming)
接着补充。
class Person:
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return('我是%s,体重%.2f公斤' % (self.name,self.weight))
def run(self):
print('我爱跑步')
self.weight -= 0.5
def eat(self):
print('我是吃货')
self.weight += 1
xiaoming = Person('小明',75.0)
xiaoming.eat()
xiaoming.run()
print(xiaoming)
原始体重 75.0,跑一次步, 吃一次饭,体重为75.5
class Person:
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return('我是%s,体重%.2f公斤' % (self.name,self.weight))
def run(self):
print('%s 爱跑步' % self.name)
self.weight -= 0.5
def eat(self):
print('%s 是吃货' % self.name)
self.weight += 1
xiaoming = Person('小明',75.0)
xiaoming.eat()
xiaoming.run()
print(xiaoming)
xiaomei = Person('小美',50)
xiaomei.run()
xiaomei.eat()
print(xiaomei)
再做一个案例:
class HouseItem:
def __init__(self, name, area):
self.name = name
self.area = area
def __str__(self):
return('[ %s ] 占地 %.2f 平方' % (self.name, self.area))
class House:
def __init__(self, house_style, area):
self.house_style = house_style
self.area = area
self.free_area = area
self.item_list = []
def __str__(self):
return('户型: %s\n总面积: %.2f\n剩余面积: %.2f\n家具名称列表: %s'
% (self.house_style,self.area,self.free_area,self.item_list))
def add_item(self, item):
print('要添加 %s' % item)
if item.area > self.free_area:
print('%s 的面积太大了,摆放不开' % item.name)
return
else:
self.item_list.append(item.name)
self.free_area -= item.area
# 创建家具对象
bed = HouseItem('席梦思', 46)
chest = HouseItem('衣柜',20)
table = HouseItem('餐桌', 15)
# print(bed)
# print(chest)
# print(table)
# 创建家对象
my_house = House('两室一厅', 80)
my_house.add_item(bed)
my_house.add_item(chest)
my_house.add_item(table)
print(my_house)
再做一个案例:
class Gun:
def __init__(self, modle):
self.modle = modle
self.bullet_count = 0
def add_bullet(self, count):
self.bullet_count += count
def shoot(self):
if self.bullet_count <= 0:
print('%s 没有子弹了' % self.modle)
return
else:
self.bullet_count -= 1
print('%s突突突,还有%d颗子弹。' % (self.modle, self.bullet_count) )
ak47 = Gun('ak47')
ak47.shoot()
ak47.add_bullet(10)
ak47.shoot()
ak47.shoot()
class Gun:
def __init__(self, modle):
self.modle = modle
self.bullet_count = 0
def add_bullet(self, count):
self.bullet_count += count
def shoot(self):
if self.bullet_count <= 0:
print('%s 没有子弹了' % self.modle)
return
else:
self.bullet_count -= 1
print('%s 突突突,还有 %d 颗子弹。' % (self.modle, self.bullet_count) )
class Soldier:
def __init__(self, name):
self.name = name
self.gun = None
def fire(self):
# 判断是否有枪
if self.gun == None:
print('%s 还没有枪。' % self.name)
return
# 喊口号
print('%s 冲啊!!!' % self.name)
# 装子弹
if self.gun.bullet_count <= 0:
self.gun.add_bullet(10)
# 发射子弹
self.gun.shoot()
ak47 = Gun('ak47')
xusanduo = Soldier('许三多')
xusanduo.gun = ak47
xusanduo.fire()
xusanduo.fire()
xusanduo.fire()
所以这里建议使用 is 。
class Women:
def __init__(self, name, age):
self.name = name
self.__age = age
def secret(self):
print('%s 的年龄是 %d 。 ' % (self.name, self.__age))
xiaomei = Women('小美',18)
print(xiaomei.__age)
xiaomei.secret()
私有属性 __age 在外部不能访问。
class Women:
def __init__(self, name, age):
self.name = name
self.__age = age
def secret(self):
print('%s 的年龄是 %d 。 ' % (self.name, self.__age))
xiaomei = Women('小美',18)
xiaomei.secret()
私有属性 __age 在内部能访问。
class Women:
def __init__(self, name, age):
self.name = name
self.__age = age
def __secret(self):
print('%s 的年龄是 %d 。 ' % (self.name, self.__age))
xiaomei = Women('小美',18)
xiaomei.__secret()
私有方法 __secret 在外部不能调用。
class Women:
def __init__(self, name, age):
self.name = name
self.__age = age
def __secret(self):
print('%s 的年龄是 %d 。 ' % (self.name, self.__age))
xiaomei = Women('小美',18)
print(xiaomei._Women__age)
xiaomei._Women__secret()