python类的初识

先看看标准库中对类的解释
Classes
Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override new(). The arguments of the call are passed to new() and, in the typical case, to init() to initialize the new instance.
Class Instances
Instances of arbitrary classes can be made callable by defining a call() method in their class.
意思就是类是可调用的,这些对象作为类模型的新的实例,通常情况是通过init()去初始化新实例。类的实例的方法是可调用的。

class Person:
    animal = '高级动物'
    soul = '有灵魂'
    language = '语言'
    def __init__(self, country, name, sex, age, height):  # __init__就是初始化实例对象,是python语言类的定义规则
        self.country = country  # self 就是每个实例化对象。就是一个单位。类就是一个范围。就是把类的本身具体化,现实的例子。
        self.name = name
        self.sex = sex
        self.age = age
        self.height = height

    def eat(self):
        return '%s is eating' % self.name

    def sleep(self):
        return '%s is sleep' % self.name

    def work(self):
        return '%s is working' % self.name
p1 = Person('China', 'alex', 'unkown', 42, 175)  # p1就是这个Person的具体实现。因为Person是个类,太广泛了、
p2 = Person('American', 'Wuda', 'male', 35, 160)
p3 = Person('Korea', 'Dumingjun', 'male', 29, 180)
p4 = Person(p1.country, p2.name, p3.sex, p2.age, p3.height)
print(p1.eat())
print(p2.sleep())
print(p3.work())
print(p1.animal)
print(p2.soul)
print(p3.language)

class Human:

    def __init__(self, name, age, sex):  # self 代表类的实例化对象
        self.name = name
        self.age = age
        self.sex = sex

    def action1(self):
        print(self.name, self.age, self.sex, '上山去砍柴')

    def action2(self):
        print(self.name, self.age, self.sex, '最爱大保健')

    def action3(self):
        print(self.name, self.age, self.sex, '开车去东北')

p1 = Human('小明', '10岁', '男')  # 这里self就是代表P1
p2 = Human('老李', '90岁', '男')  # self 是p2
p1.action1()
p1.action3()
p1.action2()
p2.action1()
p2.action2()
p2.action3()

class GameRole:  # 类名字使用驼峰体
    def __init__(self, name, ad, hp):
        self.name = name
        self.ad = ad
        self.hp = hp

    def attack(self, role): # attack 的方法传入两个实例对象,一个是本身,一个另外一个GameRole的实例化对象
        print('{0}攻击{1}, {1}掉了{2}血,还剩{3}血'.format(self.name, role.name, self.ad, role.hp - self.ad))

gailun = GameRole('盖伦', 10, 100)
jianhao = GameRole('剑豪', 20, 80)

gailun.attack(jianhao)  # 实例化后的对象执行方法。

内存运行机制


print(GameRole)  # <class '__main__.GameRole'> 创建类时的开辟的空间
print(GameRole.__dict__)  # 查看这个地址存的各种属性和方法名地址。
print(GameRole.attack)  # <function GameRole.attack at 0x000001B65A879950>  单独查看attack方法在类中的地址

print(gailun)   # 实例化对象在内存中的地址
print(gailun.__dict__)   # {'name': '盖伦', 'ad': 10, 'hp': 100}  单独传给对象的属性
print(gailun.attack)  # <bound method GameRole.attack of <__main__.GameRole object at 0x000001B65A898438>>
                      # 引用了类空间中的attack方法,说明是哪个在用它

这里写图片描述
同时调用连个角色时内存运行机制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值