#!/usr/bin/python
# -*- coding: utf-8 -*-
class employee:
'所有員工的基類'
empcount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
employee.empcount += 1
def displaycount(self):
print("total employee %d" % employee.empcount)
def displayemployee(self):
print("name is : ", self.name, ", salary is: ", self.salary)
emp1 = employee("zara", 2000)
emp1.displayemployee()
emp1.displaycount()
emp2 = employee("manni", 5000)
emp2.displayemployee()
emp2.displaycount()
e*** = employee("33333", 8888)
e***.displayemployee()
e***.displaycount()
print("total employee %d" % employee.empcount)
執行結果為:
name is : zara , salary is: 2000
total employee 1
name is : manni , salary is: 5000
total employee 2
name is : 33333 , salary is: 8888
total employee 3
total employee 3
與下列**執行輸出的total employee不一致
#!/usr/bin/python
# -*- coding: utf-8 -*-
class employee:
'所有員工的基類'
empcount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
employee.empcount += 1
def displaycount(self):
print("total employee %d" % employee.empcount)
def displayemployee(self):
print("name : ", self.name, ", salary: ", self.salary)
emp1 = employee("zara", 2000)
emp2 = employee("manni", 5000)
e*** = employee("33333", 8888)
emp1.displayemployee()
emp2.displayemployee()
e***.displayemployee()
emp1.displaycount()
emp2.displaycount()
e***.displaycount()
print("total employee %d" % employee.empcount)
name : zara , salary: 2000
name : manni , salary: 5000
name : 33333 , salary: 8888
total employee 3
total employee 3
total employee 3
total employee 3
前者輸出為每次呼叫時候的employee.empcount為3、3、3,後者每次呼叫的時候輸出,為1、2、3 Python學習筆記之全域性變數
在python中,使用全域性變數是需要使用global關鍵字進行申明的,否則會出問題。例如這樣的一段 python就會報錯 python view plain copy usr bin python filename use global.py author boyce email boyce.yw...
Python學習筆記之全域性變數
在python中,使用全域性變數是需要使用global關鍵字進行申明的,否則會出問題。例如這樣的一段 python就會報錯 python view plain copy usr bin python filename use global.py author boyce email boyce.yw...
php學習筆記 全域性變數,超全域性變數
超全域性變數 它們可以在乙個指令碼的全部作用域中都可用。在函式或方法中無需執行global variable就可以訪問它們。globals 引用 全域性作用域中可用的 全部變數 globals index x 75 y 25 function addition addition echo z ser...