码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【python 小白到精通】第五章:面向对象编程 - 三星运行模拟


    文章目录

    • 前言
    • 为什么使用面向对象
    • 三星运行模拟
      • 定义星球类
      • 主程序


    前言

    这一章学习了面向对象编程,需要掌握的内容包括:会调用对象的方法和属性;如何编写类。

    这一章课程中有个模拟双星运行的小程序,为了增进理解,我改了改代码,成了模拟三星运行。

    为什么使用面向对象

    完成一些简单任务时,使用面向对象常常会需要写更多的代码,那我们为什么要使用面向对象呢?

    由于面向对象的一些特性 (这里不细说),我们可以设计出低耦合的系统,更容易修改和维护

    三星运行模拟

    我们把星球作为对象,设计一个星球类,类中就包含了星球的一些性质。然后在主程序中调用星球的方法,让它自己的事情自己做。

    模拟三星运行时遇到了一些问题,常常几个星球跑不了一会儿就飞开了。请添加图片描述
    在多次调整参数后,它才终于可以多一起运行一会而,最终被我调成了类似恒星系的结构(有一个大质量的中心星球)。本来是想试试能不能出现三体那种效果的。

    在这里插入图片描述

    定义星球类

    import math
    
    
    class Planet:
        Number = 0
    
        def __init__(self, color, mass, position, v_init):
            Planet.Number += 1
            self.color = color
            self.mass = mass
            self.x, self.y = position
            self.vx, self.vy = v_init
    
        def v_change(self, f, t=1e-3):
            fx, fy = f
            ax, ay = fx / self.mass, fy / self.mass
            self.vx += ax * t
            self.vy += ay * t
    
        def p_change(self, t=1e-3):
            self.x += self.vx * t
            self.y += self.vy * t
    
        def gravity(self, other_planet):
            distance = math.sqrt((self.x - other_planet.x) ** 2 + (self.y - other_planet.y) ** 2)
            f = self.mass * other_planet.mass / (distance ** 2)
            fx = (other_planet.x - self.x) / distance * f
            fy = (other_planet.y - self.y) / distance * f
            return fx, fy
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    主程序

    import matplotlib.pyplot as plt
    from matplotlib import animation
    
    fig = plt.figure()
    
    point_1 = Planet('r', 100, (-3, -3), (0, 0.3))
    point_2 = Planet('r', 10, (5, -5), (0, -3))
    point_3 = Planet('r', 0.1, (0, 10), (3, 0))
    
    def animate(i):
        fig.clear()
        for _ in range(500):
            f_21 = point_1.gravity(point_2)
            f_31 = point_1.gravity(point_3)
            f_12 = point_2.gravity(point_1)
            f_32 = point_2.gravity(point_3)
            f_13 = point_3.gravity(point_1)
            f_23 = point_3.gravity(point_2)
    
            point_1.p_change()
            point_1.v_change(f_21)
            point_1.v_change(f_31)
    
            point_2.p_change()
            point_2.v_change(f_12)
            point_2.v_change(f_32)
    
            point_3.p_change()
            point_3.v_change(f_13)
            point_3.v_change(f_23)
    
        plt.scatter(point_1.x, point_1.y, c='r')  # 红色星球
        plt.scatter(point_2.x, point_2.y, c='b')  # 蓝色星球
        plt.scatter(point_3.x, point_3.y, c='g')  # 绿色星球
    
        plt.xlim(-20, 20)
        plt.ylim(-20, 20)
        plt.draw()
    
    
    ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, interval=1, blit=False)
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    完

    14天学习训练营导师课程:
    杨鑫《Python 自学编程基础》
    杨鑫《 Python 网络爬虫基础》
    杨鑫《 Scrapy 爬虫框架实战和项目管理》

  • 相关阅读:
    「容器管理系统」 3. 初始化配置和日志监控
    java继承和重写(代码演示)
    一百八十一、Hive——海豚调度HiveSQL任务时当Hive的计算引擎是mr或spark时脚本的区别(踩坑,附截图)
    低代码时代,每个人都是开发者?
    mock 点击图片显示Too big of an image!
    电脑怎么图片转文字?建议收藏这几个方法
    Day45-9大内置对象、JSP标签、JSTL标签、EL表达式、JavaBean
    时序预测 | MATLAB实现TCN-BiGRU时间卷积双向门控循环单元时间序列预测
    GCN 翻译 - 1
    Tricentis NeoLoad:自动化的企业性能测试平台
  • 原文地址:https://blog.csdn.net/m0_63238256/article/details/127847811
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号