Python

Python轻松入门 - 5 类的定义和使用

2019-05-16  本文已影响0人  V哥带你写程序

类的定义:把成员变量及其操作方法封装在一起就叫类

python类的定义,class关键字加冒号

class Casher:
    def __init__(self, initAmount: int = 0):
        self.total = initAmount

    def receiveMoney(self, goodPrice: int):
        self.total += goodPrice

    def __repr__(self) -> str:
        return str(self.total)

类方法

使用类

从类创建对象

c = Casher(100)
c.receiveMoney(50)
print(c)
c.total

d = Casher()
d.receiveMoney(20)
print(d)

检查一个对象的类型

type(d)

上一篇下一篇

猜你喜欢

热点阅读