class Person:

  country = 'China'  #类属性 静态属性 可被类和对象调用 可用Person.country = xx修改 对于不可变类型,对象只能改自己的,不能改类的;可变数据类型都能['English']与'English'

  def __init__(self,*args):  #初始化方法,self对象必须传

    self.name = args[0]

    self.hp = args[1]     

  def walk(self,n):  #一般必须传self参数且写在第一个,后面可以传其他参数

    print('%s走走走,走了%s步'%(self.name,n))

 

print(Person.country)  #查看类中的静态属性,不需要实例化

alex = Person('渣渣辉',100)  #alex对象 实例化

print(alex.__dict__)  #查看所有属性

alex.walk(5)  #等于Person.walk(alex,5)

Person.__dict__['country']

alex.__dict__['name']  #可以直接调用

alex.__dict__['name'] = '二哥'  #可以用字典形式更改,但是类的字典只能看不能改

 

 

#过程:

  #类名() 创造出一个对象,创建一个self对象(self是一个可以存储很多属性的大字典)

  #调用init方法,类命括号里的参数会被这里接收

  #执行init方法

  #返回self

 

#对象能做的事情

  #查看属性

  #调用方法

#类能做的事情

  #实例化

  #调用方法  #需要自己传self参数

  #调用类中的属性,即静态属性

  #__dict__ 对于类中的名字只能看,不能操作

 

#################################################

 

class Dog:

  def __init__(self,name1,hp1,att1,kind1):

    self.name = name1

    self.hp = hp1

    self .att= att1

    self.kind = kind1

  def bite(self,person):

    person.hp -= self.att

 

class Person:

  def __init__(self,name,hp,att,kind):

    self.name = name

    self.hp = hp

    self .att= att

    self.money = 0

  def attack(self,dog):

    dog.hp -= self.att

  

  def get_weapon(self,weapon):

    if self.money > weapon.price:

      self.money -= weapon.price

      self.att += weapon.att

  

class Weapon:

  def __init__(self,name,att,endu,price):

    self.name = name

    self.att = att

    self.endu = endu

    self.price = price

  def hand18(self,dog):

    if self.endu>0:

      dog.hp -= person.att*2

      self.endu -= 1

    else:

      print('武器已没有耐久度,请前往充值购买'%)

 

 

jin = Dog('金老板',1000,200,'未知')

alex = Person('Alex',999,199,'未知')

w = Weapon('打狗棒',100,3,998)

jin.bite(alex)

alex.money += 1000

alex.get_weapon(w)

alex.attack(jin)

alex.weapon.hand18(jin)  #组合

 

组合:一个对象的属性值是另一个对象