1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
__slots__ = ('brand', 'color', 'memory', 'size')
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def play_games(self):
print('打游戏,打LOL')
def write_code(self):
print('写代码,真累')
def watch_video(self):
print('没事干就追剧,没事干那是不可能的')
computer = Computer('华硕', '银色', '8G')
computer.brand = '联想'
setattr(computer, 'brand', '华硕')
# computer.size = '14寸'
setattr(computer, 'size', '14寸')
print(computer.brand, computer.memory, computer.color)
print(getattr(computer, 'brand'), getattr(computer, 'color'), getattr(computer, 'memory'))
del computer.color
delattr(computer, 'brand')
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Person:
def __init__(self, name, dog, age):
self.name = name
self.dog = dog
self.age = age
def walk_the_dog(self):
print('{}在遛{}'.format(self.name, self.dog))
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def brak(self):
print('{}在叫唤'.format(self.name))
dog = Dog('大大黄黄', 'yellow', 2)
person = Person('小明', dog.name, 23)
person.walk_the_dog()
3.声明⼀一个圆类,自己确定有哪些属性和方法
from math import pi
class Crisis:
__slots__ = ('radius', 'perimeter', 'area')
def __init__(self, radius, perimeter = 0, area = 0):
self.radius = radius
self.perimeter = perimeter
self.area = area
def quadrature(self):
self.area = pi * self.radius ** 2
return self.area
def peri(self):
self.perimeter = 2 * pi * self.radius
return self.perimeter
crisis = Crisis(3)
print(crisis.quadrature())
print(crisis.peri())
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
"""__ author__= 小孩子 """
import json
class Student:
__slots__ = ('name', 'age', 'study_id', 'score')
def __init__(self, name, age, study_id):
self.name = name
self.age = age
self.study_id = study_id
def sign_in(self):
try:
with open('../files/imformation.txt', 'r', encoding='utf-8') as study_file:
imfomation = json.loads(study_file.read())
except FileNotFoundError:
print('文件未打开')
if self.name not in imfomation:
print('人未到')
else:
print('{}:到'.format(self.name))
print(imfomation[self.name])
class Class:
def __init__(self, student_name, class_name, students):
self.student_name = student_name
self.class_name = class_name
students.append(self.class_name)
self.studens = students
def add(self):
with open('../files/imformation.txt', 'r', encoding='utf-8') as study_file:
imfomation = json.loads(study_file.read())
imfomation[self.student_name] = self.studens
with open('../files/imformation.txt', 'w', encoding='utf-8') as study_file:
study_file.write(json.dumps(imfomation))
def delete(self):
with open('../files/imformation.txt', 'r', encoding='utf-8') as study_file:
imfomation = json.loads(study_file.read())
del imfomation[self.student_name]
with open('../files/imformation.txt', 'w', encoding='utf-8') as study_file:
study_file.write(json.dumps(imfomation))
def class_sign_in(self):
pass
student1 = Student('Tow', 18, '001')
student2 = Student('Tom', 20, '002')
student3 = Student('dr', 24, '003')
stu_class1 = Class(student1.name, 'python', [student1.age, student1.study_id])
stu_class2 = Class(student2.name, 'python', [student2.age, student2.study_id])
stu_class3 = Class(student3.name, 'python', [student3.age, student3.study_id])
stu_class1.add()
stu_class2.add()
stu_class3.add()
student1.sign_in()
student2.sign_in()
student3.sign_in()