Loading... ## 类属性 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-fbb8ba2f28ea1df46753738f13ed52cc14" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-fbb8ba2f28ea1df46753738f13ed52cc14" class="collapse in collapse-content"><p></p> ``` 定义一个类属性,记录通过这个类创建了多少个对象。 ``` <p></p></div></div></div> ### 代码 ``` class Person(object): count = 0 # 类属性 def __init__(self, name, age): Person.count += 1 self.name = name self.age = age # 创建对象 p1 = Person('张三', 18) p2 = Person('李四', 21) p3 = Person('王五', 22) print(Person.count) ``` --- ## 汽车类 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-cc31f076d019af668cf50e770bf243b838" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-cc31f076d019af668cf50e770bf243b838" class="collapse in collapse-content"><p></p> ``` # 需求: # 建立一个汽车类Auto,包括轮胎个数(wheel_count,默认为4)、汽 # 车颜色(color)、车身重量(weight)、速度(speed,默认为0)等属性,并 # 定义一个可以改变车速的方法。 # 至少要求汽车能够加速、减速、停车。 # 再定义一个小汽车类CarAuto,继承Auto,并添加空调(ac)、 # CD(cd)属性,并且重新实现方法覆盖加速、减速的方法。 ``` <p></p></div></div></div> ### 代码 ``` class Auto: def __init__(self, wheel_count=4, color='', weight=0, speed=0): self.wheel_count = wheel_count self.color = color self.weight = weight self.speed = speed def up_speed(self): self.speed += 1 print('汽车加速') def down_speed(self): if self.speed > 0: self.speed -= 1 print('汽车减速') def stop_car(self): self.speed = 0 print('汽车停止了') def __str__(self): return "汽车属性:\n 颜色:{}\n 轮胎个数:{}\n 重量:{}\n 速度:{}\n".format(self.color, self.wheel_count, self.weight, self.speed) class CarAuto(Auto): def __init__(self, ac, cd): super(CarAuto, self).__init__() self.ac = ac self.cd = cd def __str__(self): return "CarAuto:\n AC:{}\n CD:{}".format(self.ac, self.cd) def up_speed(self): self.speed += 1 print('CarAuto加速') def down_speed(self): self.speed -= 1 print('CarAuto减速') a1 = Auto(4, '红色', 900, 80) print(a1) a1.up_speed() a1.down_speed() a1.stop_car() print("\n") c1 = CarAuto("制热", "") print(c1) c1.up_speed() c1.down_speed() ``` --- ## 银行账户类 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-4642d687af653fc7efc3312115f72d9e6" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-4642d687af653fc7efc3312115f72d9e6" class="collapse in collapse-content"><p></p> ``` # 需求: # 有一个银行账户类Account,包括名字、余额等属性,方法有存钱、 # 取钱、查询余额等操作。要求: # 1. 在存钱时,注意存款数据的格式 # 2. 取钱时,要判断余额是否充足,余额不够的时候要提示余额不足。 ``` <p></p></div></div></div> ### 代码 ``` class Account(object): def __init__(self, name, balance): self.name = name self.balance = balance def save_money(self, money): assert isinstance(money, float) or isinstance(money, int), '请注意存钱数据的格式' self.balance += money print('存钱操作:{}, 当前余额:{}'.format(money,self.balance)) def take_money(self, money): if self.balance < money: print('取钱操作:{},余额不足,当前余额:{}'.format(money, self.balance)) else: self.balance -= money print('取钱操作:{}, 当前余额:{}'.format(money, self.balance)) def check_balance(self): return self.balance a = Account('张三', 8000) a.save_money(10000) a.take_money(1000) print('当前余额:', a.check_balance()) a.take_money(20000) print('当前余额:', a.check_balance()) ``` --- ## 点与圆的关系 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-a377d7e3f37ba69e760f453b49fa42de48" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-a377d7e3f37ba69e760f453b49fa42de48" class="collapse in collapse-content"><p></p> ``` # 需求: # 定义一个点类 Pointer,属性是横向左边 x 和 纵向坐标 y # 定义一个圆类 Circle,属性有圆心点 cp 和 半径 radius # 方法有: # 1. 求圆的面积 # 2. 求圆的周长 # 3. 求指定点与圆的关系(圆内、圆外、圆上) # 涉及到的数学公式:指定点与圆心点之间的距离 与 圆的半径进行比较 ``` <p></p></div></div></div> ### 代码 ``` import math class Pointer: def __init__(self, x, y): self.x = x self.y = y class Circle: def __init__(self, cp, radius): self.cp = cp self.radius = radius def num1(self): s = math.pi * self.radius ** 2 c = 2 * math.pi * self.radius return f'圆的周长{c},圆的面积{s}' def num2(self, obj): b = ((obj.x - p3.x) ** 2 + (obj.y - p3.y) ** 2) ** 1 / 2 # 点与圆心点之间的距离 if b > self.radius: print('点在圆外') elif b < self.radius: print('点在园内') else: print('点在圆上') p1 = Pointer(0, 0) # 设置圆心点 c2 = Circle(p1, 5) # 设置圆 p3 = Pointer(1, 2) # 设置点 print(c2.num1()) # 圆的周长和面积 c2.num2(p1) ``` --- ## 宠物店猫狗 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-9e62b211d8687f4fce8e68fd014679ad96" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-9e62b211d8687f4fce8e68fd014679ad96" class="collapse in collapse-content"><p></p> ``` # 需求: # 定义一个宠物店类PetShop:属性有店名、店中的宠物(使用列表存 # 储宠物);方法:展示所有宠物的信息。 # 定义一个宠物狗类PetDog:属性有昵称、性别、年龄、品种;方法: # 叫、拆家、吃饭。 # 定义一个宠物猫类PetCat:属性有昵称、性别、年龄、品种、眼睛的 # 颜色;方法:叫、撒娇、吃饭。 # 注:狗的叫声是“汪汪”,吃的是骨头,猫的叫声是“喵喵”,吃的是鱼。 ``` <p></p></div></div></div> ### 代码 ``` class PetShop(): def __init__(self, name, pet_list): self.name = name self.pet_list = pet_list def show(self): if len(self.pet_list) == 0: print("还没有宠物呢!") return print("{}".format(self.name)) for pet in self.pet_list: print(pet.__dict__) class Pet(object): def __init__(self, name, gender, age, kind): self.name = name self.gender = gender self.age = age self.kind = kind def cry(self): pass def eat(self): pass def myprint(self): print(self.name, self.gender, self.age, self.kind) class PetDog(Pet): def des_home(self): print(self.name + "拆家") def cry(self): print(self.name + "汪汪") def eat(self): print(self.name + "吃骨头") class PetCat(Pet): def __init__(self, name, gender, age, kind, eye_color): self.eye_color = eye_color super().__init__(name, gender, age, kind) def cry(self): print(self.name + "喵喵") def play(self): print(self.name + "撒娇") def eat(self): print(self.name + "吃⻥") dog = PetDog("小黄", "公", 2, "柴犬") dog1 = PetDog("小黄1", "公", 3, "柴犬") dog2 = PetDog("小黄2", "公", 4, "柴犬") cat = PetCat("小美", "母", 2, "美短", "黑色") cat1 = PetCat("小美1", "母", 3, "美短", "黑色") cat2 = PetCat("小美2", "母", 4, "美短", "黑色") ps = PetShop("shop", [dog, dog1, cat]) ps.show() dog.eat() dog.cry() cat.eat() cat.cry() ``` --- ## 学生类列表 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-2f2c3438387d05d294f27e97ebc4c6f969" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-2f2c3438387d05d294f27e97ebc4c6f969" class="collapse in collapse-content"><p></p> ``` # 需求: # 定义一个学生类 Student:属性有学号、姓名、年龄、性别、成绩。 # 定义一个班级类Grade:属性有班级名称、班级中的学生(使用列表 # 存储学生) # 方法有: # 1. 查看该班级中的所有学生信息; # 2. 查看指定学号的学生信息; # 3. 查看班级中成绩不及格的学生信息; # 4. 将班级中的学生按照成绩降序排序。 ``` <p></p></div></div></div> ### 代码 ``` class Grade(): def __init__(self, class_name, list=None): self.class_name = class_name if list is None: list = [] self.list = list def add_list(self, stu): self.list.append(stu.stu_list) def show(self): # 1.查看该班级中的所有学生的信息 print(self.list) def show_num(self, num): # 2.查看指定学号的学生信息 for i in self.list: if num in i[0]: print(i) break else: print("该学号的学生不存在。") def fail(self): # 3.查看班级中成绩不及格的学生信息 for i in self.list: if i[4] < 60: print(i) def sort(self): # 4.将班级中的学生按照成绩降序排序 for i in range(len(self.list) - 1): for j in range(len(self.list) - 1 - i): if self.list[j][4] < self.list[j + 1][4]: self.list[j], self.list[j + 1] = self.list[j + 1], self.list[j] print(self.list) class Student(): def __init__(self, num, name, age, gender, score, stu_list=None): self.num = num self.name = name self.age = age self.gender = gender self.score = score self.stu_list = [self.num, self.name, self.age, self.gender, self.score] grade = Grade("逆战班") wm = Student("001", "王猛", 24, "男", 100) grade.add_list(wm) xm = Student("002", "小美", 24, "女", 99) grade.add_list(xm) zs = Student("003", "张三", 24, "男", 88) grade.add_list(zs) ls = Student("004", "李四", 24, "男", 40) grade.add_list(ls) # 1.查看该班级中的所有学生的信息 grade.show() # 2.查看指定学号的学生信息 grade.show_num("001") # 3.查看班级中成绩不及格的学生信息 grade.fail() # 4.将班级中的学生按照成绩降序排序 grade.sort() ``` 最后修改:2021 年 10 月 29 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏
4 条评论
你真牛逼OωO!
滴!学生卡!打卡时间:下午6:51:54,请上车的乘客系好安全带~
已读
୧(๑•̀⌄•́๑)૭