1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
# a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
# b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
"""电脑类"""
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print('打游戏')
def write_code(self):
print('写代码')
def look_tv(self):
print('看视频')
def point_to_obtain():
"""对象点的方式获取"""
c1 = Computer('联想', '黑色', '20T')
# 查
print(c1.brand) # 联想
# 修改
c1.memory = '100t'
print(c1.memory) # 100t
# 增加
c1.display = '200*200'
print(c1.display) # 200*200
# 删除
del c1.color
# print(c1.color) AttributeError: 'Computer' object has no attribute 'color'
def attr_obtain():
c2 = Computer('战神', '白色', '2t')
# 查
print(getattr(c2, 'brand')) # 战神
# 改
setattr(c2, 'color', '红色')
print(c2.color) # 红色
# 增
setattr(c2,'display','800*800')
print(c2.display) # 800*800
# 删
delattr(c2,'color')
# print(c2.color) AttributeError: 'Computer' object has no attribute 'color'
2.声明⼀个人的类和狗的类:
# 狗的属性:名字、颜⾊、年龄
# 狗的⽅方法:叫唤
# 人的属性:名字、年龄、狗
# 人的⽅方法:遛狗
# a.创建⼈的对象小明,让他拥有⼀条狗⼤大⻩黄,然后让小明去遛⼤⻩
class Person:
"""人的类"""
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
print('%s遛狗' % self.name)
class Dog:
"""狗的类"""
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def cry_out(self):
print('%s叫唤' % self.name)
def self_person():
p1 = Person('憨猜猜', 20, '大黄')
p1.walk_the_dog() # 憨猜猜遛狗
3.声明⼀一个圆类:
class Circle:
"""圆类"""
def __init__(self, r, ):
self.r = r
self.c = 3.14
def circular_area(self):
area = self.c * self.r ** 2
print('圆的面积是%s' % area)
def circumference(self):
circum = 2*self.c*self.r
print('圆的周长是%s'%circum)
c1 = Circle(5)
print(c1.circular_area()) # 圆的面积是78.5
print(c1.circumference()) # 圆的周长是31.400000000000002
4.创建⼀一个学⽣生类:
# 属性:姓名,年龄,学号
# 方法:答到,展示学⽣生信息
# 创建⼀一个班级类:
# 属性:学⽣,班级名
# 方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
"""学生类"""
def __init__(self, name, age, stu):
self.name = name
self.age = age
self.stu = stu
def answer_to_the(self):
"""答到"""
# return self.__dict__
print(self.__dict__)
# print('%s到,%s %s %s' % (self.name, self.stu, self.name, self.age))
# s1 = Student('憨猜猜', 21, 'stu10001')
class ClassAndGrade:
"""班级类"""
# number = []
def __init__(self, list1, classname='python1809'):
# self.student = student
self.list1 = list1
self.classname = classname
def all_student(self, student):
"""添加学生"""
self.list1.append(student)
# return self.list1
print(self.list1)
def del_student(self, student):
"""删除学生"""
self.list1.remove(student)
# return self.list1
print(self.list1)
def call_the_roll(self):
"""点名"""
for dict1 in self.list1:
print(dict1['name'])
def mean_age(self):
sum_age = 0
index = 0
for dict1 in self.list1:
index += 1
sum_age += dict1['age']
# return sum_age / index
print(sum_age/index)
def main():
s1 = Student('憨猜猜', 21, 'stu10001')
s1.answer_to_the() # 憨猜猜到,stu10001 憨猜猜 21
c1 = ClassAndGrade([{'name': '憨猜猜', 'age': 21, 'tel': 123456}])
c1.all_student({'name': '小明', 'age': 20, 'tel': 12345})
c1.del_student({'name': '憨猜猜', 'age': 21, 'tel': 123456})
c1.all_student({'name': '星系', 'age': 23, 'tel': 123456})
c1.all_student({'name': '拉拉', 'age': 30, 'tel': 123456})
c1.all_student({'name': '傻逼', 'age': 28, 'tel': 123456})
c1.all_student({'name': '网红', 'age': 50, 'tel': 123456})
c1.call_the_roll()
c1.mean_age()
# print(c1)
网友评论