Python面向对象2-类和构造方法
生活随笔
收集整理的這篇文章主要介紹了
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")
?
轉載于:https://www.cnblogs.com/Presley-lpc/p/9689397.html
總結
以上是生活随笔為你收集整理的Python面向对象2-类和构造方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZJOI2005午餐
- 下一篇: 难缠的this