python_类变量、私有属性

# !/user/bin/env python
# -*- coding:utf-8 -*-
# Author:feifei

class Role(object):
n = 123
def __init__(self,name,role,weapon,life_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon
self.life_value = life_value
self.money = money

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

def got_shot(self):
print("ah...,I got shot...")

def buy_gun(self,gun_name):
print("just bought %s" %gun_name)


r1 = Role('Alex','police','AK47') #生成一个角色
print(r1.n)
Role.n = 456
r2 = Role('Jack','terrorist','B22') #生成一个角色
print(r1.n)
print(r2.n)
'''
私有属性,私有方法
私有属性、私有方法,变量前加双下划线__
私有属性、私有方法,外部不可访问,只能内部访问,内部修改
'''

猜你喜欢

转载自www.cnblogs.com/cly0205/p/10204246.html
今日推荐