zoukankan      html  css  js  c++  java
  • 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")
  • 相关阅读:
    五种线程池的分类与作用
    什么是死锁?
    事务隔离级别区分,未提交读,提交读,可重复读
    共享锁(读锁)和排他锁(写锁)
    java中的成员变量和全局变量的区别
    Algorithm
    6
    5
    4
    3
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/9689397.html
Copyright © 2011-2022 走看看