# 声明一个 Employee 类
class Employee:
# 声明一个类的变量
pay_raist_amount = 1.2
# 创建一个构造器
def init(self,first,last,pay,domain="qq.com"):
self.first = first
self.last = last
self.pay = pay
self.email = first+last+"@"+domain

# 创建一个方法

def fullname(self):
return self.first+self.last

def new_pay0(self):
# return self.pay * Employee.pay_raist_amount
return self.pay * self.pay_raist_amount

def new_pay1(self):
return self.pay * Employee.pay_raist_amount

创建一个类的实例

emp1 = Employee("xiaoming","wang",10000,"baidu.com")
emp2 = Employee("xiaohong",'zhang',20000)
Employee.pay_raist_amount = 1.4
print(emp1.new_pay0())
print(emp1.new_pay1())
print(emp2.new_pay0())
print(emp2.new_pay1())
emp1.pay_raist_amount = 1.5
print("emp1. raise, emp1.newpay0()",emp1.new_pay0())
print(emp1.new_pay1())
print(emp2.new_pay0())
print(emp2.new_pay1())
emp2.pay_raist_amount = 1.6
print(emp1.new_pay0())
print(emp1.new_pay1())
print(emp2.new_pay0())
print(emp2.new_pay1())

运行结果:
14000.0
14000.0
28000.0
28000.0
emp1. raise, emp1.newpay0() 15000.0
14000.0
28000.0
28000.0
15000.0
14000.0
32000.0
28000.0


↙↙↙阅读原文可查看相关链接,并与作者交流