Day14-homework

  1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
    def __init__(self):
        self.tyre_num = 4
        self.color = '黑色'
        self.weight = '2吨'
        self.speed = '十秒可以达到200码'

    def accelerate_ss(self):
        return '加速'

    def moderate_ss(self):
        return '减速'

    def park_ss(self):
        return '停车'

class CarAuto(Auto):
    def __init__(self):
        super().__init__()
        self.CD_ss = '给你一张过去的CD'
        self.air_conditioner = '制冷'

    def accelerate_ss(self):
        # super().accelerate_ss()
        return '踩油门'

    def moderate_ss(self):
        # super().moderate_ss()
        return '踩刹车'


kayan = CarAuto()
print(kayan.moderate_ss())
print(kayan.__dict__)
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
    count = 0
    def __init__(self,name):
        self.name = name
        Person.count += 1
p1 = Person('sss')
p2 = Person('ddd')
p3 = Person('dd')
print(Person.count)
  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
    要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
    def __init__(self, master):
        self.gender = '雌'
        self.age = 2
        self.color = '白'
        self.type = '猫'
        self.master = master

    def __str__(self):
        return ('/%s的宠物:性别-%s 年龄-%s 颜色-%s 类型-%s/')%(self.master, self.gender, self.age, self.color, self.type)

animal1 = Animal('小黑')
print(animal1)
  1. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class AreaError(Exception):
    def __str__(self):
        return '不能直接给面积赋值'

class PerimeterError(Exception):
    def __str__(self):
        return '不能直接给周长赋值'

class Circle:
      def __init__(self, radius):
          self.radius = radius
          self._area = 0
          self._perimeter = 0

      @property
      def area(self):
           self._area = self.radius**2*3.14
           return self._area

      @area.setter
      def area(self, x):
          if x != self.radius**2*3.14:
              raise AreaError
          else:
              self._area  = x


      @property
      def perimeter(self):
          self._perimeter = self.radius*2*3.14
          return self._perimeter

      @perimeter.setter
      def perimeter(self, x):
          if x != self.radius*2*3.14:
              raise PerimeterError
          self._perimeter = x

circle1 = Circle(3)
# circle1.area = -1  给面积赋值
# circle1.perimeter = 0   给周长赋值
print(circle1.area, circle1.perimeter)
  1. 面向对象学生管理系统(挣
class Students:
    def __init__(self):
        self.students = []

    def add_s(self, num_s):
        self.name = input("请输入学生姓名")
        self.age = input("请输入学生年龄")
        self.tel = input('请输入学生电话')
        self.num_s = num_s
        student = {'name': self.name, 'age': self.age, 'tel': self.tel, 'number_s': self.num_s}
        self.students.append(student)
        print(self.students)
        print('添加成功!')

    def del_s(self):
        name = input('输入要删除的学生的名字')
        for item in self.students:
            if item['name'] == name:
                self.students.remove(item)
                print('删除成功!')


    def check_s(self):
        name = input('请输入要查看的学生姓名')
        count = 0
        for item in self.students:
            if item['name'] == name:
                    print(item)
                    count += 1
        if count == 0:
            print('没有该学生')

    def exchange_s(self):
        name = input('请输入要修改的学生名字')
        important_x  = input('请输入要修改此学生的什么信息')
        i = input('请输入修改后的信息')
        for item in self.students:
            if item['name'] == name:
                item[important_x] = i
                print(self.students)
                print('修改成功')

funcs = Students()
# while True:
#     home = """
# =================================
#     欢迎蜡笔小新:
#         1.添加学生
#         2.查看学生
#         3.删除学生
#         4.修改学生信息
#         5.返回
# =================================
#            """
#     cor = '''
#     1.继续
#     2.返回
#     '''
#     print(home)
#     input_start = int(input('请输入1到5'))
#     while input_start == 1:
#         funcs.add_s(0)
#         print(cor)
#         change_num  = int(input('请选择1到2'))
#         if change_num == 2:
#             break
#     while input_start == 2:
#         funcs.check_s()
#         print(cor)
#         change_num = int(input('请选择1到2'))
#         if change_num == 2:
#             break
# 
#     while input_start == 3:
#         funcs.del_s()
#         print(cor)
#         change_num = int(input('请选择1到2'))
#         if change_num == 2:
#             break
# 
#     while input_start == 4:
#         funcs.exchange_s()
#         print(cor)
#         change_num = int(input('请选择1到2'))
#         if change_num == 2:
#             break
# 
#     if input_start == 5:
#         break

你可能感兴趣的:(Day14-homework)