Python面向对象2-类和构造方法
生活随笔
收集整理的這篇文章主要介紹了
Python面向对象2-类和构造方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # 作者:Presley
4 # 郵箱:1209989516@qq.com
5 # 時(shí)間:2018-08-05
6 # OOP學(xué)習(xí)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的實(shí)例
28 #把一個(gè)抽象的類變成一個(gè)具體的對(duì)象的過程
29 r1 = Role("wohaoshuai1","police","AK47")#生成一個(gè)角色
30 #相當(dāng)于Role(p1,"wohaoshuai","police","AK47")
31
32 r2 = Role("wohaoshuai2","police","B22") #生成一個(gè)角色
33
34 r1.buy_gun("AK47") #會(huì)自動(dòng)轉(zhuǎn)換成Role.buy_gun(r1,"AK47")
?
轉(zhuǎn)載于:https://www.cnblogs.com/Presley-lpc/p/9689397.html
總結(jié)
以上是生活随笔為你收集整理的Python面向对象2-类和构造方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZJOI2005午餐
- 下一篇: 难缠的this