1. 程式人生 > >python_類變數、私有屬性

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)
'''
私有屬性,私有方法
私有屬性、私有方法,變數前加雙下劃線__
私有屬性、私有方法,外部不可訪問,只能內部訪問,內部修改
'''