Skip to main content

Command Palette

Search for a command to run...

Python 阶段编程练习(十四)

Published
2 min read
Python 阶段编程练习(十四)

编程练习

自定义一个交通工具类(Vehicle),并根据提示对该类进行进一步封装,使其拥有工具类型、速度、体积等属性值。通过自定义实例方法实现交通工具的前移、速度设置、获取当前速度、加速行驶、减速行驶、实例信息展示、实例类型判别等功能。

效果图如下:

1

任务

  1. 自定义一个交通工具类(Vehicle)
  2. 设置类属性trans_type(默认值为SUV)和实例属性速度speed(int 类型,单位为 km/h)、体积size(tuple类型,单位为米。)
  3. 自定义方法 show_info( ),打印实例的所属类型和速度、体积的值;
  4. 自定义实例方法如下:

    1. 定义move( )方法,实现打印"我已向前移动了50米"
    2. 定义set_speed(new_speed)方法,设置对应实例的速度为new_speed km/h
    3. 定义get_speed()方法,如果(2)中设置了速度值则打印出来,打印格式为"我的时速为:设置的速度值 km/h"
    4. 定义speed_up()方法,设置每次调用时实例的速度都增加10km/h,并打印"我的速度由xx km/提升到了xx km/h"
    5. 定义speed_down()方法,设置每次调用时实例的速度都降低15km/h,并打印"我的速度由xx km/下降到了xx km/h"
  5. 自定义方法 transport_identify( ),判断实例的trans_type属性是否为SUV。若是则打印"类型匹配",反之则打印"类型不匹配"
  6. 初始化实例对象tool_1,并根据上述效果图调用对应方法

任务提示

  • 类的初始化方法中所传参数size是元组类型,直接传入实例的长,宽,高即可,如size=(10,10,10)

初始代码

class Vehicle(object):
    # 自定义Vehicle类属性

    # 自定义实例的初始化方法

    # 自定义实例方法show_info,打印实例的速度和体积

    # 自定义实例方法move,打印“我已向前移动了50米”

    # 自定义实例方法set_speed,设置对应的速度值

    # 自定义实例方法get_speed,打印当前的速度值

    # 自定义实例方法speed_up,实现对实例的加速

    # 自定义实例方法speed_down,实现对实例的减速

    # 自定义实例方法transport_identify,实现对实例

if __name__ == "__main__":
    tool_1 = Vehicle(20, (3.6, 1.9, 1.75))

    # 调用实例方法 打印实例的速度和体积

    # 调用实例方法 实现实例的前移

tool_1.set_speed(40)
    # 调用实例方法 打印当前速度

    # 调用实例方法 对实例进行加速

    # 调用实例方法 对实例进行减速

    # 调用实例方法 判断当前实例的类型

代码提交区

# coding:utf-8

class Vehicle(object):
    # 自定义Vehicle类属性
    trans_type = 'SUV'

    # 自定义实例的初始化方法
    def __init__(self, speed, size):
        self.speed = speed
        self.size = size

    # 自定义实例方法show_info,打印实例的速度和体积
    def show_info(self):
        print('所属类型:{},速度是{}km/h,体积是{}'.format(self.trans_type, self.speed, self.size))

    # 自定义实例方法move,打印“我已向前移动了50米”
    def move(self):
        print('我已向前移动了50米')

    # 自定义实例方法set_speed,设置对应的速度值
    def set_speed(self, speed):
        self.speed = speed

    # 自定义实例方法get_speed,打印当前的速度值
    def get_speed(self):
        print('我的时速为:{}'.format(self.speed))

    # 自定义实例方法speed_up,实现对实例的加速
    def speed_up(self):
        self.__new_speed = self.speed + 10
        print('我的速度由{}km/h,提升到了{}km/h'.format(self.speed,self.__new_speed))

    # 自定义实例方法speed_down,实现对实例的减速
    def speed_down(self):
        self.__speed = self.speed - 15
        print('我的速度由{}km/h,下降到了{}km/h'.format(self.speed, self.__speed))

    # 自定义实例方法transport_identify,实现对实例
    def transport_identify(self):
        if isinstance(self, Vehicle):
            print('类型匹配')
        else:
            print('类型不匹配')


if __name__ == "__main__":
    tool_1 = Vehicle(20, (3.6, 1.9, 1.75))
# 调用实例方法 打印实例的速度和体积
    tool_1.show_info()
# 调用实例方法 实现实例的前移
    tool_1.move()
    tool_1.set_speed(40)
# 调用实例方法 打印当前速度
    tool_1.get_speed()
# 调用实例方法 对实例进行加速
    tool_1.speed_up()
# 调用实例方法 对实例进行减速
    tool_1.speed_down()
# 调用实例方法 判断当前实例的类型
    tool_1.transport_identify()

结果检验

C:\Python39\python.exe C:/Users/admin/PycharmProjects/python_object/hello.py
所属类型:SUV,速度是20km/h,体积是(3.6, 1.9, 1.75)
我已向前移动了50米
我的时速为:40
我的速度由40km/h,提升到了50km/h
我的速度由40km/h,下降到了25km/h
类型匹配

进程已结束,退出代码为 0
学霸答案区
class Vehicle(object):
    # 自定义Vehicle类属性
    trans_type = 'SUV'

    # 自定义实例的初始化方法
    def __init__(self,speed,size):
        self.speed = speed
        self.size = size


# 自定义实例方法show_info,打印实例的速度和体积
    def show_info(self):
        print('速度:{0}km/h,体积:{1}'.format(self.speed,self.size))
    # 自定义实例方法move,打印“我已向前移动了50米”
    def move(self):
        print('我已经向前移动了50米')
    # 自定义实例方法set_speed,设置对应的速度值
    def set_speed(self,speed):
        self.speed = speed
    # 自定义实例方法get_speed,打印当前的速度值
    def get_speed(self):
        print('我的时速为:{0}km/h'.format(self.speed))
    # 自定义实例方法speed_up,实现对实例的加速
    def speed_up(self):
        speed2 = self.speed+10
        print('我的速度由{0}km/h提升到了{1}km/h'.format(self.speed,speed2))
        self.speed = speed2
    # 自定义实例方法speed_down,实现对实例的减速
    def speed_down(self):
        speed3 = self.speed - 15
        print('我的速度由{0}km/h下降到了{1}km/h'.format(self.speed,speed3))
        self.speed = speed3
    # 自定义实例方法transport_identify,实现对实例所属类型的判断
    def transport_identify(self):
        if isinstance(self,Vehicle):
            print('类型匹配')
        else:
            print('类型不匹配')
if __name__ == "__main__":
    tool_1 = Vehicle(20, (3.6, 1.9, 1.75))

    # 调用实例方法 打印实例的速度和体积
    tool_1.show_info()
    # 调用实例方法 实现实例的前移
    tool_1.move()

    tool_1.set_speed(40)
    # 调用实例方法 打印当前速度
    tool_1.get_speed()
    # 调用实例方法 对实例进行加速
    tool_1.speed_up()
    # 调用实例方法 对实例进行减速
    tool_1.speed_down()
    # 调用实例方法 判断当前实例的类型
    tool_1.transport_identify()
1 views

More from this blog

MySQL | 表的内连接

数据操作语言:表连接查询(一) 从多张表中提取数据 从多张表提取数据,必须指定关联的条件。如果不定义关联条件就会出现无条件连接,两张表的数据会交叉连接,产生 笛卡尔积。 规定了连接条件的表连接语句,就不会出现笛卡尔积。 # 查询每名员工的部门信息 SELECT e.empno,e.ename,d.dname FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno; 表连接的分类 表连接分为两种:内连接 和 外连接 内连接是结果集中只保留符合...

May 16, 20221 min read13
MySQL | 表的内连接

MySQL | 分组查询的应用

数据操作语言:分组查询 为什么要分组? 默认情况下汇总函数是对全表范围内的数据做统计 GROUP BY 子句的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对每个小区域分别进行数据汇总处理 SELECT deptno,AVG(sal) FROM t_emp GROUP BY deptno; SELECT deptno,ROUND(AVG(sal)) FROM t_emp GROUP BY deptno; -- ROUND 取整 逐级分组 数据库支持多列分组条件,执行的时候...

Apr 27, 20221 min read10
MySQL | 分组查询的应用

MySQL | 聚合函数的使用

数据操作语言:聚合函数 什么是聚合函数 聚合函数在数据的查询分析中,应用十分广泛。聚合函数可以对 数据求和、求 最大值 和 最小值 、求 平均值 等等。 求公司员工的评价月收入是多少? SELECT AVG(sal+IFNULL(comm,0)) FROM t_emp; SELECT AVG(sal+IFNULL(comm,0)) AS avg FROM t_emp; SUM 函数 SUM 函数用于求和,只能用户数字类型,字符类型的统计结果为 0 ,日期类型统计结果是毫秒数相加 SE...

Apr 26, 20221 min read8
MySQL | 聚合函数的使用
U

Untitled Publication

173 posts