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

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # 作者:Presley
 4 # 邮箱:1209989516@qq.com
 5 # 时间:2018-08-05
 6 # OOP学习1
 7 
 8 class Role(object):
 9     def __init__(self,name,role,weapon,life_value=100,money=15000):
10         self.name = name
11         self.role = role
12         self.weapon = weapon
13         self.life_value = life_value
14         self.money = money
15         self.aaa = 1
16 
17     def shot(self):
18         print("shooting...")
19 
20     def got_shot(self):
21         print("ah...,I got shot...")
22 
23     def buy_gun(self,gun_name):
24         print("just bought {0}".format(gun_name))
25         print(self.aaa)
26 
27 #Role的实例
28 #把一个抽象的类变成一个具体的对象的过程
29 r1 = Role("wohaoshuai1","police","AK47")#生成一个角色
30 #相当于Role(p1,"wohaoshuai","police","AK47")
31 
32 r2 = Role("wohaoshuai2","police","B22") #生成一个角色
33 
34 r1.buy_gun("AK47") #会自动转换成Role.buy_gun(r1,"AK47")

 

posted @ 2018-09-22 08:37  Presley  阅读(418)  评论(0编辑  收藏  举报