Python面向对象2-类和构造方法

时间:2023-03-08 19:43:45
 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# 作者:Presley
# 邮箱:1209989516@qq.com
# 时间:2018-08-05
# OOP学习1 class Role(object):
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
self.aaa = 1 def shot(self):
print("shooting...") def got_shot(self):
print("ah...,I got shot...") def buy_gun(self,gun_name):
print("just bought {0}".format(gun_name))
print(self.aaa) #Role的实例
#把一个抽象的类变成一个具体的对象的过程
r1 = Role("wohaoshuai1","police","AK47")#生成一个角色
#相当于Role(p1,"wohaoshuai","police","AK47") r2 = Role("wohaoshuai2","police","B22") #生成一个角色 r1.buy_gun("AK47") #会自动转换成Role.buy_gun(r1,"AK47")