上一篇:异常的概念 | 手把手教你入门Python之七十六
下一篇:With关键字的使用 | 手把手教你入门Python之七十八
本文来自于千锋教育在阿里云开发者社区学习中心上线课程《Python入门2020最新大课》,主讲人姜伟。
面向对象案例练习
1、定义一个点类 Pointer
属性是横向坐标 x 与纵向坐标 y
定义一个圆类 Circle
属性有圆心点 cp 与 半径 radius
方法有:
1、求圆的面积
2、求圆的周长
3、求指定点与圆的关系
提示:关系有三种【圆内 圆外 圆上】
涉及到的数学公式:指定点与圆心点之间的距离 与 圆的半径进行比较
import math
class Pointer(object):
def __init__(self, x, y):
self.x = x
self.y = y
class Circle(object):
def __init__(self, cp, radius): # cp = p, radius = 5
self.cp = cp
self.radius = radius
def get_area(self):
return self.radius ** 2 * math.pi
def get_length(self):
return math.pi * 2 * self.radius
def relationship(self, point):
"""
求一个点和圆的关系。有三种关系:在圆内,在圆外,在圆上
:param point: 需要判断的点
:return:
"""
# 计算圆心到point的距离
distance = (point.x - self.cp.x) ** 2 + (point.y - self.cp.y) ** 2
if distance > self.radius ** 2:
print('在圆外')
elif distance < self.radius ** 2:
print('在圆内')
else: # 等于的情况
print('在圆上')
p = Pointer(3, 4) # 创建了一个Pointer对象
c = Circle(p, 5) # 创建好的Pointer对象传递给了Circle对象c
print(c.get_area()) # 78.53981633974483
print(c.get_length()) # 31.41592653589793
p1 = Pointer(10, 10)
c.relationship(p1) # 在圆外
p2 = Pointer(2, 2)
c.relationship(p2) # 在圆内
p3 = Pointer(0, 0)
c.relationship(p3) # 在圆上
2、宠物店类 PetShop
属性:店名,店中的宠物【使用列表存储宠物】
方法:展示所有宠物的信息
宠物狗类 PetDog
属性:昵称,性别,年龄,品种
方法:叫,拆家,吃饭
宠物猫类 PetCat
属性:昵称,性别,年龄,品种,眼睛的颜色
方法:叫,撒娇,吃饭
注意:
狗的叫声是汪汪 猫的叫声是喵喵
狗吃的是骨头 猫吃的是鱼
class PetShop(object):
def __init__(self, shop_name, pet_list=None):
self.shop_name = shop_name
if pet_list is None:
pet_list = []
self.pet_list = pet_list
# def __init__(self, shop_name):
# self.shop_name = shop_name
# self.pet_list = []
def show_pets(self):
if len(self.pet_list) == 0:
print('本店还没有宠物')
return
print('{}有{}个宠物,它们是:'.format(self.shop_name, len(self.pet_list)))
for pet in self.pet_list:
# print('姓名:{},性别:{},品种:{},年龄:{}'.format(pet.name, pet.gender, pet.breed, pet.age))
print(pet)
class Pet(object):
def __init__(self, name, gender, age, breed):
self.name = name
self.gender = gender
self.age = age
self.breed = breed
def bark(self):
print(self.name + '正在叫')
def eat(self):
print(self.name+'正在吃东西')
def __str__(self):
return '姓名:{},性别:{},品种:{},年龄:{}'.format(self.name, self.gender, self.breed, self.age)
class PetDog(Pet):
def bark(self):
print(self.name+'正在汪汪汪')
def build_home(self):
print(self.name+'正在拆家')
def eat(self):
print(self.name+'正在啃骨头')
class PetCat(Pet):
def __init__(self, name, gender, age, breed, eyes_color):
super(PetCat, self).__init__(name, gender, age, breed)
self.eyes_color = eyes_color
def bark(self):
print(self.name+'正在喵喵喵')
def sajiao(self):
print(self.name+'正在撒娇')
def eat(self):
print(self.name+'正在吃鱼')
def __str__(self):
x = super(PetCat, self).__str__()
x += ",眼睛颜色:{}".format(self.eyes_color)
return x
class PetBird(Pet):
pass
dog1 = PetDog('大黄', 'female', 3, '哈士奇')
dog2 = PetDog('二黄', 'male', 2, '萨摩耶')
cat1 = PetCat('tom', 'male', 2, '英短', 'blue')
cat2 = PetCat('包子', 'female', 3, '加菲猫', 'black')
ps = PetShop('萌宠', [dog1, dog2, cat1, cat2])
ps.show_pets()
3、打印列表
class Person(object):
def __init__(self, name, age):
self.name = name
self.agae = age
def __repr__(self):
return '姓名:{},年龄:{}'.format(self.name, self.age) # [姓名:张三,年龄:18,姓名:李四,年龄:20]
p1 = Person('张三', 18)
p1 = Person('李四', 20)
persons = [p1, p2]
# 直接打印列表,会调用列表里元素的 __repr__ 方法
# print(persons) # 直接打印一个列表,会把列表里的每一个对象的内存地址打印出来
4、1.学生类Student:
属性:学号,姓名,年龄,性别,成绩
班级类Grades
属性:班级名称,班级中的学生【使用列表存储学生】
方法:
1.查看该班级中的所有学生的信息
2.查看指定学号的学生信息
3.查看班级中成绩不及格的学生信息
4.将班级中的学生按照成绩降序排序
class Student(object):
def __init__(self, number, name, age, gender, score):
self.number = number
self.name = name
self.age = age
self.gender = gender
self.score = score
def __str__(self):
return '编号:{},姓名:{},年龄:{},性别:{},分数:{}'.format(self.number, self.name, self.age, self.gender, self.score)
class Grade(object):
def __init__(self, name, students):
self.name = name
self.students = students
def show_all(self):
for student in self.students:
print(student)
def failed_student(self):
result = filter(lambda student: student.score < 60, self.students)
for x in result:
print(x)
def order_student(self):
# self.students.sort(key = lambda s: s.score, reverse = True) # 直接修改 self.students
return sorted(self.students, key = lambda s: s.score, reverse = True)
def get_student_by_number(self, n):
for s in self.students:
if s.number == n:
return s
else:
return '用户未找到'
# 如果数字以0开头,在python2里表示八进制
s1 = Student(1, 'zhangsan', 18, 'male', 80)
s2 = Student(5, '李四', 19, 'male', 50)
s3 = Student(10, 'tony', 20, 'male', 70)
s4 = Student(7, 'jack', 18, 'female', 90)
s5 = Student(4, 'henry', 19, 'female', 56)
g = Grade('中二班', [s1, s2, s3, s4, s5])
g.show_all()
g.failed_student()
# g.order_students()
# for student in g.students:
# print(student)
x = g.order_student()
for student in x:
print(student)
print(g.get_student_by_number(1))