18、面向对象--类和实例属性
创建一个类
class PerSon(object):
def __init__(self, name , age):
self.name = name
self.age = age
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
kk = PerSon('kk', 30)
print(kk.get_name())
kk.set_name("huahua")
print(kk.get_name())
19、面向对象--类和实例方法
猫狗大战练习
#encoding:utf-8
import random
class Cat(object):
def __init__(self, name, blood=100):
self.name = name
self.blood = blood
def get_blood(self):
return self.blood
def drop_blood(self, drop):
self.blood -= drop
def attack(self, rival):
drop = random.randint(0, 20)
rival.drop_blood(drop)
class Dog(object):
def __init__(self, name, blood=100):
self.name = name
self.blood = blood
def get_blood(self):
return self.blood
def drop_blood(self, drop):
self.blood -= drop
def attack(self, rival):
drop = random.randint(0, 20)
rival.drop_blood(drop)
wangwang = Dog("wangwang")
miaomiao = Cat("miaomiao", 120)
while True:
wangwang.attack(miaomiao)
if miaomiao.get_blood() <= 0:
print(wangwang.name + ' is win.')
break
miaomiao.attack(wangwang)
if wangwang.get_blood() <= 0:
print(miaomiao.name + 'is win..')
break
F:\py3proj\07\cmdb\duixiang>python fire.py
Python代码运行页面,学习
20、面向对象--私有属性和继承
标签:name,get,--,self,drop,def,面向对象,blood,Python3 From: https://www.cnblogs.com/sunnyyangwang/p/16593266.html