作业
import math
1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
# class Computer:
# __slots__ = ('brand', 'color', 'memory', 'height')
#
# def __init__(self, brand, color, memory):
# """
# 对象属性
# :param brand: 品牌属性
# :param color: 颜色属性
# :param memory: 内存属性
# """
# self.brand = brand
# self.color = color
# self.memory = memory
#
# def play_game(self):
# print("打游戏")
#
# def write_code(self):
# print("写代码")
#
# def watch_video(self):
# print("看视频")
#
# def modify_attribute(self):
# print(self.brand, self.color, self.memory)
#
#
# c_1 = Computer('联想', 'black', '4G')
# c_2 = Computer('华硕', 'white', '8G')
# c_1.modify_attribute() # 联想 black 4G
# c_2.modify_attribute() # 华硕 white 8G
# """
# 通过对象.属性
# """
# # 查
# print(c_1.brand) # 联想
# print(c_2.brand) # 华硕
#
# # 增,改
# c_1.brand = '戴尔' # 改
# c_1.modify_attribute() # 戴尔 black 4G
#
# c_2.height = '3.5kg' # 增
# print(c_2.height) # 3.5kg
# print(c_2.brand, c_2.color, c_2.memory, c_2.height) # 华硕 white 8G 3.5kg
#
# # 删
# del c_2.height
# c_2.modify_attribute() # 华硕 white 8G
#
# """
# 通过attr方法
# """
# # 查
# print(getattr(c_1, 'brand')) # 戴尔
#
# # value = input("属性名:") # 属性名:color
# # print(getattr(c_1, value)) # black
#
# # 增/改
# c_1.modify_attribute() # 戴尔 black 4G
# setattr(c_1, 'color', 'red')
# c_1.modify_attribute() # 戴尔 red 4G
#
# setattr(c_1, 'height', '4.0kg')
# print(c_1.brand, c_1.color, c_1.memory, c_1.height) # 戴尔 red 4G 4.0kg
#
# # 删
# delattr(c_1, 'color')
# print(c_1.color) # AttributeError: color color属性已经被删除,所以报错
print()
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
# class Person:
#
# def __init__(self, name, dog_name, age=18, dog=None):
# self.name = name
# self.age = age
# self.dog = dog
# self.dog_name = dog_name
# self.people_dog = None # 人没有狗
#
# def walk(self):
#
# if self.people_dog is None: # 判断人有没有狗
# print("%s还没有狗" % self.name)
# return
#
# print("%s正在遛%s" % (self.name, self.dog_name))
#
# def people_self(self):
# print(self.name, self.age, self.dog)
#
#
# class Dog:
# def __init__(self, name, age=2, color='yellow'):
# self.name = name
# self.age = age
# self.color = color
#
# def call(self):
# print("%s正在汪汪汪的叫" % self.name)
#
# def dog_self(self):
# print(self.name, self.age, self.color)
#
#
# # Instance = Person()
#
#
# def main():
# dog_1 = Dog("大黄") # 创建狗的对象
# people = Person('小明', dog_1.name) # 创建人的对象
# dog_1 = Dog("大黄") # 创建狗的对象
# people.people_dog = dog_1 # 人获得狗
# people.walk()
# dog_1.call()
#
#
# if __name__ == '__main__':
# main()
print()
3.声明⼀一个圆类,自己确定有哪些属性和方法
# class circular:
# pi = math.pi
#
# def __init__(self, radius):
# self.radius = radius
#
# def area(self):
# area_1 = self.radius ** 2 * circular.pi
# return area_1
#
#
# a1 = circular(5)
# print(a1.area()) # 78.53981633974483
print()
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
print()
# class Student:
# classroom = []
#
# def __init__(self, name, age, stu_id):
# self.name = name
# self.age = age
# self.stu_id = stu_id
#
# def answer_to(self):
# classroom = []
# if self.name in classroom:
# print("到")
# else:
# classroom.append(self.name)
# print("未到")
# return classroom
#
# def show_massage(self):
# print(self.name, self.age, self.stu_id)
#
#
# s_1 = Student('小明', 18, '001')
# s_1.show_massage()
# s_1.answer_to()
# print(s_1.answer_to())
# class Class:
# def __init__(self, all_student, class_name, all_score):
# self.all_student = all_student
# self.class_name = class_name
# self.all_score = all_score
#
# def add_student(self, student): # 添加学生
# self.all_student.append(student)
#
# def del_student(self, student): # 删除学生
# self.all_student.remove(student)
#
# def all_score(self, score): # 添加成绩
# self.all_score.append(score)
#
# def roll_call(self, student): # 点名
# if student in self.all_student:
# print("到")
# print("未到")
#
# def average_score(self):
# average = sum(self.all_score) / len(self.all_student)
# return average