1. 声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computer:
    """电脑类"""
    def __init__(self, brand='联想', color='黑色', memory=8):
        self.brand = brand
        self.color = color
        self.memory = memory

    @staticmethod
    def play_game():
        print('打游戏')

    def code(self):
        print(self.brand)
        print('编码')

    @staticmethod
    def watch_tv():
        print('看电视')
  1. 声明⼀个人的类和狗的类:
    狗的属性:名字、颜色、年龄
    狗的方法:叫唤
    人的属性:名字、年龄、狗
    人的方法:遛狗
    a.创建人的对象小明,让他拥有⼀条狗⼤黄,然后让⼩明去遛⼤黄
class Dog:
    def __init__(self, name='', age=0, color='白色'):
        self.name = name
        self.age = age
        self.color = color

    def bark(self):
        print(f'{self.name}在嗷嗷叫!')


class Person:
    def __init__(self, name, age=18, dog=None):
        self.name = name
        self.age = age
        self.dog = dog

    def walk_the_dog(self):
        if self.dog:
            print(f'{self.name}牵着{self.dog.name}散步')
        else:
            print('你现在没有狗!')


p1 = Person('小明', dog=Dog('大黄', 2, '黄色'))
p1.walk_the_dog()

p2 = Person('小花')
p2.walk_the_dog()
  1. 声明一个圆类,自己确定有哪些属性和方法
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    # 求两点之间的距离
    def distance(self, other):
        return ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5


class Circle:
    # pi是类属性
    pi = 3.1415926

    def __init__(self, r: float, center: Point):
        self.r = r
        self.center = center

    def perimeter(self):
        return 2 * Circle.pi * self.r

    def area(self):
        return Circle.pi * self.r ** 2

    # 判断一个圆是否是另外一个圆的外切圆
    def is_exterior_contact(self, other):
        return self.center.distance(other.center) == self.r + other.r


c1 = Circle(3, Point())
c2 = Circle(2, Point(0, 5))
print(c1.perimeter())
print(c1.area())
print(c1.is_exterior_contact(c2))
  1. 创建一个学生类:
    属性:姓名,年龄,学号
    方法:答到,展示学生信息
    创建⼀个班级类:
    属性:学⽣,班级名
    方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Student:
    def __init__(self, name='', age=18, study_id='000'):
        self.name = name
        self.age = age
        self.study_id = study_id

    def response(self):
        print(f'{self.name}, 到!')

    def show_message(self):
        print(f'姓名:{self.name} 年龄:{self.age} 学号:{self.study_id}')

    def __repr__(self):
        # 当通过print打印当前类的对象的时候会自动调用这个函数,获取函数的返回值为打印的值(返回值必须是字符串)
        return f'<name:{self.name} age:{self.age} stuid:{self.study_id}>'


stu1 = Student('小明')
print('stu1:', stu1)


class Class:
    def __init__(self, name, students=[]):
        self.students = students
        self.class_name = name
        self.max_student_count = 50
        self.create_id = (self.class_name+str(x).zfill(2) for x in range(1, self.max_student_count+1))

    def add_student(self):
        name = input('姓名:')
        age = int(input('年龄:'))
        stu_id = next(self.create_id)
        stu = Student(name, age, stu_id)
        self.students.append(stu)

    def del_student(self):
        name = input('请输入删除学生的姓名:')
        for stu in self.students[:]:
            if stu.name == name:
                self.students.remove(stu)
        print(self.students)

    def call_the_roll(self):
        for stu in self.students:
            print(stu.name)
            if randint(0, 1) == 1:
                stu.response()
            else:
                print(f'{stu.name}缺席!')

    def average_age(self):
        ages = reduce(lambda x, y: x+y.age, self.students, 0)
        return ages / len(self.students)


# c1 = Class('py2002')
# c1.add_student()
# c1.add_student()
# print(c1.students)
students = [
    Student('stu1', 18, 'py001'),
    Student('stu2', 30, 'py002'),
    Student('stu3', 23, 'py003'),
    Student('stu4', 22, 'py004'),
    Student('stu5', 19, 'py005')
]
c1 = Class('py2002', students)
print(c1.average_age())

# c1.del_student()
c1.call_the_roll()
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
    count = 0

    def __init__(self):
        Person.count += 1


p1 = Person()
p2 = Person()
print(Person.count)
  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
    要求打印这个类的对象的时候以’/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/’ 的形式来打印
class Animal:
    def __init__(self, gender='公', age=0, color='白色', breed='土狗'):
        self.gender = gender
        self.age = age
        self.color = color
        self.breed = breed

    def __repr__(self):
        return f'/{self.__class__.__name__}的对象:性别-{self.gender} 年龄-{self.age} 颜色-{self.color} 类型-{self.breed}/'


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


class Circle:
    pi = math.pi

    def __init__(self, r):
        self.r = r
        self._area = 0
        self._perimeter = 0

    @property
    def area(self):
        return Circle.pi * self.r ** 2

    @area.setter
    def area(self, value):
        raise ReadOnlyError


c1 = Circle(4)
print(c1.area)
# c1.area = 100
  1. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
class Poker:
    def __init__(self, color: str, num: str):
        self.color = color
        self._num = num

    def __repr__(self):
        return f'{self.color}{self._num}'

    @property
    def num(self):
        sp = ['J', 'Q', 'K', 'A', '2', 'joker', 'JOKER']
        if self._num in sp:
            return 11+sp.index(self._num)
        else:
            return int(self._num)


class PokerGame:
    def __init__(self):
        # 创建一副牌所有的扑克对象
        self.pokers = []
        colors = ['♥️', '♦️', '♠', '♣']
        nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
        for color in colors:
            for num in nums:
                p = Poker(color, num)
                self.pokers.append(p)
        self.pokers.append(Poker('', 'JOKER'))
        self.pokers.append(Poker('', 'joker'))
        # print(self.pokers)

    def shuffle(self):
        """洗牌"""
        random.shuffle(self.pokers)
        # print(self.pokers)

    @staticmethod
    def func1(item):
        return item.num

    def deal(self):
        """发牌"""
        pokers_iter = iter(self.pokers)
        player1, player2, player3 = [], [], []
        for _ in range(17):
            player1.append(next(pokers_iter))
            player2.append(next(pokers_iter))
            player3.append(next(pokers_iter))

        return sorted(player1, key=PokerGame.func1, reverse=True), sorted(player2, key=PokerGame.func1, reverse=True), sorted(player3, key=PokerGame.func1, reverse=True), list(pokers_iter)


p1 = PokerGame()
p1.shuffle()
print(p1.deal())


nums = [10, 98, 77, 19, 25]
print(sorted(nums, key=lambda item: item % 10))