• python中的对象


    对象 = 属性(数据) + 方法(函数)

    一、对象创建

    1 class创建类

    2 使用这个类来建立一个真正的对象,这个对象称为这个类的一个实例。

    3 范例:

    创建类:

    1. class Ball:
    2. def bounce(self):
    3. if self.direction == "down":
    4. self.direction = "up"

    创建Ball类的实例:

    ball = Ball()

    给实例加上属性并调用其方法

    1. ball.direction = "down"
    2. print("ball direction = {}".format(ball.direction))
    3. ball.bounce()
    4. print("ball direction = {}".format(ball.direction))

    4 范例扩展

    增加颜色属性,增加大小属性,多次调用bounce方法

    代码如下:

    1. class Ball:
    2. def bounce(self):
    3. if self.direction == "down":
    4. self.direction = "up"
    5. else:
    6. self.direction = "down"
    7. ball = Ball()
    8. ball.direction = "down"
    9. ball.color = "red"
    10. ball.size = "small"
    11. print("ball direction = {}".format(ball.direction))
    12. print("ball color = {}".format(ball.color))
    13. print("ball size = {}".format(ball.size))
    14. ball.bounce()
    15. print("after bounce.ball direction = {}".format(ball.direction))
    16. ball.bounce()
    17. print("after bounce.ball direction = {}".format(ball.direction))
    18. ball.bounce()
    19. print("after bounce.ball direction = {}".format(ball.direction))
    20. ball.bounce()
    21. print("after bounce.ball direction = {}".format(ball.direction))

    二、初始化对象

    1 direction,color,size这些内容在对象创建时不存在,是在对象创建完成后创建的。一般情况下我们不这样做,通常我们在创建对象时会将需要的属性都设置好,这称为初始化对象。

    在类定义时,可以定义一个特定的方法,名为__init__(),每次类被实例化时,都会调用这个方法。

    代码范例:

    1. class Ball:
    2. def __init__(self, direction, color, size): # 初始化
    3. self.direction = direction
    4. self.color = color
    5. self.size = size
    6. def bounce(self):
    7. if self.direction == "down":
    8. self.direction = "up"
    9. else:
    10. self.direction = "down"
    11. ball = Ball("down", "red", "small")
    12. print("ball direction = {}".format(ball.direction))
    13. print("ball color = {}".format(ball.color))
    14. print("ball size = {}".format(ball.size))
    15. ball.bounce()
    16. print("after bounce.ball direction = {}".format(ball.direction))
    17. ball.bounce()
    18. print("after bounce.ball direction = {}".format(ball.direction))
    19. ball.bounce()
    20. print("after bounce.ball direction = {}".format(ball.direction))
    21. ball.bounce()
    22. print("after bounce.ball direction = {}".format(ball.direction))

    三、魔法函数

    打印对象及结果

    1. print(ball)
    2. <__main__.Ball object at 0x0000024982D11FD0>

    此时系统默认调用一个魔法函数

    def __str__(self)

    默认会打印对象的3个方面的内容

    1 实例在哪里定义

    2 类名(Ball)

    3 实例的内存位置

    我们希望对象打印的内容:这是一个什么颜色的球

    1. def __str__(self):
    2. return ("this is direction:{},{},{} ball".format(self.direction, self.color, self.size))

    再来打印对象

    1. print(ball)
    2. this is direction:down,red,small ball

    self是什么?即具体的实例对象

  • 相关阅读:
    几款实用的照片变漫画免费软件,千万别错过
    一文带你了解RPA和爬虫的五大区别-花漾RPA
    C语言练习题-大数乘法、分解质因数
    Linux防火墙之firewalld
    VB.NET—Bug调试(参数话查询、附近语法错误)
    C语言中的 int main(int argc,char const *argv[])是什么意思?
    Flink 内存模型
    Android系统“使用其他文件打开”列表中包含自己的app
    020线上线下融合商业模式 新零售系统定制开发
    Python可视化数据分析08、Pandas_Excel文件读写
  • 原文地址:https://blog.csdn.net/luhouxiang/article/details/126911974