zoukankan      html  css  js  c++  java
  • 面向对象之变量与实例变量




    class Role:
    # 定义一个类,class是定义类的语法,Role是类名,(object)是新式类的写法
    ac=None#类变量
    member=0
    def __init__(self,name, role, weapon, life_value): # 初始化函数,
    self.name = name#实例变量
    self.role = role
    self.weapon = weapon
    self.life_value = life_value
    Role.member+=1

    def shot(self):
    print("shooting....")

    def got_shot(self):
    print("i got shot....")

    def buy_gun(self, weapon):
    print("%s just bought [%s] " % (self.name,weapon))
    self.weapon =weapon

    r1 = Role('Alex', 'police', 'AK47', 90)
    r1.buy_gun("AK47")
    r1.ac="China Brand"

    r2 = Role('zy', 'sb', 'B51', 100)
    r2.buy_gun("B52")
    r2.ac="US Brand"

    r3 = Role('R3', 'sb', 'AK40', 100)
    r4 = Role('r4', 'sb', 'AK52', 100)

    Role.ac ="JP"
    Role.weapon="XXD"
    print("r1:", r1.weapon,r1.ac)
    print("r2:", r2.weapon,r2.ac)
    print("r3:", r3.weapon,r3.ac)
    print("r4:", r4.weapon,r4.ac)




    输出结果-----------------------------------------------

    D:Pythonpython.exe D:/Python/编辑器/代码/shutil.py
    Alex just bought [AK47]
    zy just bought [B52]
    r1: AK47 China Brand
    r2: B52 US Brand
    r3: AK40 JP
    r4: AK52 JP



  • 相关阅读:
    bzoj1072: [SCOI2007]排列perm
    bzoj1226: [SDOI2009]学校食堂Dining
    bzoj3208: 花神的秒题计划Ⅰ
    bzoj1079: [SCOI2008]着色方案
    bzoj3573: [Hnoi2014]米特运输
    bzoj1040: [ZJOI2008]骑士
    bzoj 1369: [Baltic2003]Gem
    bzoj2818: Gcd
    bzoj2705: [SDOI2012]Longge的问题
    整数分解
  • 原文地址:https://www.cnblogs.com/my334420/p/6435091.html
Copyright © 2011-2022 走看看