1.声明一个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
class Computer:
__slots__ = ('brand', 'color', 'memory_size', 'price')
def __init__(self, brand='Dell', color='black', memory_size='2G'):
self.brand = brand
self.color = color
self.memory_size = memory_size
def play_game(self):
pass
def write_code(self):
pass
def watch_movie(self):
pass
my_dell = Computer(color='red')
# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
# “对象.属性”获取属性
print(my_dell.brand, my_dell.color, my_dell.memory_size)
# “对象.属性”修改属性
my_dell.brand = 'Lenovo'
print(my_dell.brand)
# “对象.属性”添加属性
my_dell.price = 21999
print(my_dell.price)
# “对象.属性”删除属性
del my_dell.memory_size
# print(my_dell.memory_size) # AttributeError: memory_size
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
__slots__ = ('brand', 'color', 'memory_size', 'price')
def __init__(self, brand='Dell', color='black', memory_size='2G'):
self.brand = brand
self.color = color
self.memory_size = memory_size
def play_game(self):
pass
def write_code(self):
pass
def watch_movie(self):
pass
my_dell = Computer(color='red')
# b.通过attr相关方法去获取、修改、添加和删除它的属性
# “getattr(对象, 属性)”获取属性
print(getattr(my_dell, "brand"), getattr(my_dell, "color"), getattr(my_dell, "memory_size"))
# “setattr(对象, 属性)”修改属性
setattr(my_dell, "brand", "Lenovo")
print(my_dell.brand)
# “setattr(对象, 属性)”添加属性
setattr(my_dell, "price", 21999)
print(my_dell.price)
# “delattr”删除属性
delattr(my_dell, 'memory_size')
# print(my_dell.memory_size) # AttributeError: memory_size
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Person:
__slots__ = ('name', 'age', 'dog')
def __init__(self, name, age=18, dog=Dog):
self.name = name
self.age = age
self.dog = dog
def walk_dogs(self):
print('{}带着{}去遛弯...'.format(self.name, self.dog.name))
print('当{}呼唤{}时,{}会"{}"叫'.format(self.name, self.dog.name, self.dog.name, self.dog))
class Dog:
__slots__ = ('name', 'color', 'age')
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def __str__(self):
return '汪汪汪'
dog1 = Dog('大黄', 'yellow', 5)
p1 = Person('小明', 22, dog1)
p1.walk_dogs()
"""
小明带着大黄去遛弯...
当小明呼唤大黄时,大黄会"汪汪汪"叫
"""
class Dog:
__slots__ = ('name', 'age', 'color')
def __init__(self, name, color, age=0):
self.name = name
self.age = age
self.color = color
def call_out(self):
print('%s:嗷嗷叫!' % self.name)
class Person:
__slots__ = ('name', 'age', 'dog')
def __init__(self, name: str, age=0, dog=None):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
if self.dog:
print('%s牵着%s散步!' % (self.name, self.dog.name))
self.dog.call_out()
else:
print('没有狗!自己遛自己!')
dog1 = Dog('大黄', '黄色')
p1 = Person('小明', dog=dog1)
p1.walk_the_dog()
"""
小明牵着大黄散步!
大黄:嗷嗷叫!
"""
3.声明一个圆类,自己确定有哪些属性和方法
from math import pi as PI
class Circle:
__slots__ = ('radius', '_area', '_girth')
def __init__(self, radius):
self.radius = radius
self._area = radius * radius * PI
self._girth = 2 * PI * radius
@property
def area(self):
return self._area
@property
def girth(self):
return self._girth
c1 = Circle(1)
print('%.3f' % c1.area)
print('%.3f' % c1.girth)
c1 = Circle(3)
print('%.3f' % c1.area)
print('%.3f' % c1.girth)
# 方法2
# 属性:圆周率、半径、圆心坐标
# 方法:获取面积、周长
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, other):
a = self.x - other.x
b = self.y - other.y
return math.hypot(a, b)
class Circle:
pi = 3.1415926
def __init__(self, radius: float, center: Point):
self.radius = radius
self.center = center
def area(self):
return Circle.pi * self.radius ** 2
def perimeter(self):
return 2 * Circle.pi * self.radius
def is_intersect(self, other):
distance = self.center.distance(other.center)
add_r = self.radius + other.radius
sub_r = math.fbs(self.radius - other.radius)
if distance == add_r:
print('外切')
elif distance == sub_r:
print('内切')
elif distance > add_r:
print('不相交')
elif sub_r < distance < add_r:
print('相交')
else:
print('包含关系')
p1 = Point(0, 0)
c1 = Circle(2, p1)
p2 = Point(1, 0)
c2 = Circle(2, p2)
c2.is_intersect(c1)
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Student:
__slots__ = ('name', 'age', 'study_id')
def __init__(self, name, age, study_id):
self.name = name
self.age = age
self.study_id = study_id
def answer(self):
print('到!', end='')
print('我叫{},我{}岁,我的学号是{}'.format(self.name, self.age, self.study_id))
def __repr__(self):
return '{}-{}-{}'.format(self.name, self.age, self.study_id)
class Class:
__slots__ = ('students', 'class_name')
def __init__(self, class_name=None):
self.students = {}
self.class_name = class_name
def add_student(self, *args):
for arg in args:
self.students.update({arg: [arg.study_id, arg.name, arg.age]})
print('添加成功!')
def del_student(self, student):
del self.students[student]
print('删除成功!')
def ask_student(self, str1):
for student in self.students:
if str1 == student.name:
student.answer()
return
print('该学生不存在!')
def average_age(self):
sum1 = sum(student.age for student in self.students)
return sum1 / len(self.students)
stu1 = Student('a1', 12, 'stu001')
stu2 = Student('a2', 13, 'stu002')
stu3 = Student('a3', 14, 'stu003')
class_math = Class('math')
class_math.add_student(stu1, stu2, stu3)
print(class_math.students)
class_math.del_student(stu3)
print(class_math.students)
class_math.ask_student('a1')
class_math.ask_student('a3')
aver_stu_age = class_math.average_age()
print('该班的学生的平均年龄是{}'.format(aver_stu_age))