python -- 面向对象

2021-01-18 13:13

阅读:2433

标签:format   实例   code   升级版   walk   lse   大战   初始   def   

一、举例讲解

class Person:
    country = China          #创造了一个只要是这个类就一定有的属性
                               #类属性  静态属性
    def __init__(person,*args):#初始化方法,self是对象,是一个必须传的参数
        #self就是一个可以储存很多属性的大字典
        person.name = args[0]
        person.blood = args[1]
        person.attack = args[2]
        person.sex = args[3]
    def walk(self,n):          #方法,一般情况下必须传self参数,且必须写在第一个,
                               # 后面还可以传其他参数是自由的
        print(%s走走走,走了%s步%(self.name,n))
print(Person.country)  #类名  可以查看类中属性,不需要实例化就可以查看
#China
people_1 = Person(大白,1000,50,woman)  #类名还可以实例化对象   #实例化
print(people_1.__dict__)#查看所有属性
#{‘name‘: ‘大白‘, ‘blood‘: 1000, ‘attack‘: 50, ‘sex‘: ‘woman‘}
print(people_1.name)  #查看属性值
#大白
Person.walk(people_1,5)   #调用方法:类名.方法名(对象名)
people_1.walk(5)
#大白走走走,走了5步
print(Person.__dict__[country])
print(people_1.__dict__[name])
# China
# 大白
people_1.__dict__[name] = dabai
people_1.name = "dabai"
print(people_1.__dict__)
#{‘name‘: ‘dabai‘, ‘blood‘: 1000, ‘attack‘: 50, ‘sex‘: ‘woman‘}
people_1.age = 20
print(people_1.__dict__)
#{‘name‘: ‘dabai‘, ‘blood‘: 1000, ‘attack‘: 50, ‘sex‘: ‘woman‘, ‘age‘: 20}
#过程:
    # 类名()首先会创作出一个对象,创建了一个self变量
    # 调用init方法,类名括号里的参数会被这里接受
    # 执行init方法
    # 返回self
#对象能做的事:
    #查看属性
    #调用方法
#类名能做的事:
    # 实例化
    # 调用方法:只不过要自己传递self参数
    # 调用类中的属性,也就是调用静态属性
    #__dict__对于类中的名字只能看 不能操作

 二、人狗大战升级版

#人狗大战
class People:
    def __init__(self,name,blood,hp,sex):
        self.name = name
        self.blood = blood
        self.hp = hp
        self.sex = sex
        self.money = 0

    def attack(self,dog,weapon):
        dog.blood -= self.hp
        print({}用{}打了{},{}掉了{}血\n{}还剩{}滴血.format(self.name,weapon.name,dog.name, dog.name,
                                              self.hp, dog.name,dog.blood))
    def get_weapon(self,weapon):
        you_inside_money = int(input(充值金额:))
        self.money += you_inside_money
        print(您目前金额:%s%self.money)
        if self.money >= weapon.price:
            self.money -= weapon.price
            self.blood += weapon.blood
            self.hp += weapon.hp
            print(购买成功!您金额还剩:%s%self.money)
        else:
            print("金额不足!")

class Dog:
    def __init__(self,name,blood,hp,kind):
        self.name = name
        self.blood = blood
        self.hp = hp
        self.kind = kind
    def bite(self,people):
        self.blood -= people.hp
        print({}咬了{},{}掉了{}血\n{}还剩{}滴血.format(self.name, people.name, people.name,
                                              self.hp, people.name,people.blood))
class weapon:
    def __init__(self,name,blood,hp,price):
        self.name = name
        self.blood = blood
        self.hp = hp
        self.price = price

people_1 = People(大白,1000,10,woman)
people_2 =People(哪吒,500,20,man)
dog_1 = Dog(二哈,100,50,二哈)
dog_2 = Dog(哮天犬,400,100,None)

print({}\n{}\n{}\n{}\n.format(people_1.__dict__,people_2.__dict__,dog_1.__dict__,dog_2.__dict__))
bar = weapon(打狗棒,200,50,1000)
people_1.get_weapon(bar)
people_1.attack(dog_1,bar)
dog_2.bite(people_2)

# {‘name‘: ‘大白‘, ‘blood‘: 1000, ‘hp‘: 10, ‘sex‘: ‘woman‘, ‘money‘: 0}
# {‘name‘: ‘哪吒‘, ‘blood‘: 500, ‘hp‘: 20, ‘sex‘: ‘man‘, ‘money‘: 0}
# {‘name‘: ‘二哈‘, ‘blood‘: 100, ‘hp‘: 50, ‘kind‘: ‘二哈‘}
# {‘name‘: ‘哮天犬‘, ‘blood‘: 400, ‘hp‘: 100, ‘kind‘: ‘None‘}
# 
# 充值金额:10000
# 您目前金额:10000
# 购买成功!您金额还剩:9000
# 大白用打狗棒打了二哈,二哈掉了60血
# 二哈还剩40滴血
# 哮天犬咬了哪吒,哪吒掉了100血
# 哪吒还剩500滴血

 

python -- 面向对象

标签:format   实例   code   升级版   walk   lse   大战   初始   def   

原文地址:https://www.cnblogs.com/2584808136-qq-com/p/12907861.html


评论


亲,登录后才可以留言!