开发一款叫做<人狗大战>的游戏,需要2个角色,一个是人, 一个是狗
def person(attack,HP,name,level): def atk(dog_dic): print(‘%s 打了 %s‘ % (name, dog_dic[‘name‘])) dog_dic[‘HP‘] -= attack person_dic={‘attack‘:attack, ‘HP‘:HP, ‘name‘:name, ‘level‘:level, ‘atk‘:atk} return person_dic def dog(attack,HP,name,level): def bite(person_dic): print(‘%s 咬了 %s‘ % (name, person_dic[‘name‘])) person_dic[‘HP‘] -= attack dog_dic={‘attack‘:attack, ‘HP‘:HP, ‘name‘:name, ‘level‘:level, ‘bite‘:bite} return dog_dic alexander=person(100,1000,‘alex‘,2) egg=person(200,2000,‘egon‘,2) alexander[‘atk‘](egg) print(egg[‘HP‘]) print(alexander[‘HP‘])
类:具有相同属性和方法的一类事物就是类
对象和类的关系:
对象是一个实实在在存在的事物,是独一无二的
类是一个抽象的大致规范了一类事物的范围
#面向对象编程 #有一个抽象的过程 #结果不能预测 #上帝视角 class Person: role=‘人‘ #静态属性 country=‘中国‘ def attack(self): #动态属性,方法 pass print(Person.role) print(Person.attack) #方法调用 Person.role=‘人类‘ #属性修改 print(Person.__dict__[‘role‘]) hantao=Person() #对象 print(hantao.role) hantao.name=‘hantao‘ #对象创建属性 print(hantao.name) egg=Person() egg.name=‘egon‘ print(egg,hantao)
#类加上括号的过程: 实例化 #先加上创建一个空对象 #再执行__init__,同时把创建的对象放在__init__参数里 #实例化对象
#使用init进行属性的初始化 #1.规范所有的对象都拥有一些基础的属性 #2.方便 class Dog: def __init__(self,name,type): self.name=name self.type=type self.HP=2000 def bite(self,name): print(‘%s咬了%s‘%(self.name,name)) wangcai=Dog(‘旺财‘,‘中华田园犬‘) print(wangcai.__dict__) wangcai.bite(‘hantao‘)
小练习:
创建计算圆的面积周长
from math import pi class Circle: def __init__(self,r): self.r=r def s(self): return self.r**2*pi def c(self): return 2*pi*self.r c1=Circle(3) print(c1.s()) print(c1.c())
class Dog: counter=0 def __init__(self,name,type,aggr): self.name=name self.type=type self.life_value=2000 self.aggr=aggr Dog.counter+=1 def bite(self,person_name): print(‘%s咬了%s‘%(self.name,person_name.name)) person_name.life_value-=self.aggr class Person: rol = ‘人‘ #数据属性、静态属性、类属性 country = ‘中国‘ def __init__(self,name,age,life_value): #初始化方法 self.name = name #属性、对象属性 self.theage = age self.life_value = life_value self.aggr = 1 def attack(self,dog_obj): #函数属性、动态属性、方法 print(‘%s打了%s‘%(self.name,dog_obj.name)) dog_obj.life_value-=self.aggr egg=Dog(‘egon‘,‘二哈‘,20) hantao=Person(‘hantao‘,18,500) print(hantao.life_value) egg.bite(hantao) print(hantao.life_value) hantao.attack(egg)
#组合-面向对象的一种功能 class Birthday: def __init__(self,y,m,d): self.y=y self.m=m self.d=d class Person: def __init__(self,name): self.name=name # self.birthday=birth hantao_birth=Birthday(1992,8,7) hantao=Person(‘hantao‘) hantao.birth=hantao_birth print(hantao.birth.d)
#继承--面向对象的三大特性之一 #表达了什么是什么的的关系 #Animal是父类 #一个类可以有多个父类 #子类调用方法:先掉自己的,再掉父类的 #先抽象,再继承 class Animal: def __init__(self,name,aggr,life_value): self.name=name self.aggr=aggr self.life_value=life_value def func(self): print(self.name) class Dog(Animal): def __init__(self,name,aggr,life_value,type): Animal.__init__(self,name,aggr,life_value) #调用父类属性 self.type = type #派生属性 def bite(self): #派生方法 Animal.func(self) #调用父类方法 class Person(Animal): def __init__(self,name,aggr,life_value,money): Animal.__init__(self,name,aggr,life_value) self.money=money egg=Dog(‘egon‘,200,3000,‘二哈‘) hantao=Person(‘hantao‘,500,6000,3000) print(egg.__dict__) print(hantao.__dict__) egg.bite()
#如果一个类没有指定的父类,那么他的父类是object #凡是继承了object类的类都是新式类(python3都是新式类) #新式类调用父类的方法的方式:1.指明道姓 父类名.方法名(self,aggr1...) 经典类 # 2.super关键字 super().方法名(aggr1...) 新式类 #多继承 class A(): pass class B(): pass class C(A,B): pass print(A.__base__) object
原文:http://www.cnblogs.com/hantaozi430/p/7755463.html