面向对象
初始面向对象
- 什么是类? --------> 一系列具有相容属性和相似方法的事物的抽象
- 什么是对象? --------> 一个带有具体属性值得类的实例
- 什么是实例化? ------> 通过类创造对象打的过程
面向对象语法
class Person: #Person是类 def __init__(self,name,kind,hp,ad): # 参数初始化 self.name = name self.kind = kind self.hp = hp self.ad = ad man = Person('老王','man',10000,300) # man是对象 这是实例化的过程
## 人狗大战 class Person: def __init__(self,name,kind,hp,ad): self.name = name self.kind = kind self.hp = hp self.ad = ad def fight(self,dog): dog.hp -= self.ad print('%s打了%s一口,%s掉了%s滴血,%s还剩%s滴血'%(self.name,dog.name,dog.name,self.ad,dog.name,dog.hp)) class Dog: def __init__(self,name,kind,hp,ad): self.name = name self.kind = kind self.hp = hp self.ad = ad def bite(self,person): man.hp -= self.ad print('%s咬了%s一口,%s吊了%s滴血,%s还剩%s滴血'%(self.name,person.name,person.name,self.ad,person.name,person.hp)) man = Person('老王','man',10000,300) dog = Dog('teddy','man',80000,800) dog.bite(man) man.fight(dog) dog.bite(man) man.figth(dog)