python面向对象练习
1.声明?个电脑类: 属性:品牌、颜?、内存?小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的?方式获取、修改、添加和删除它的属性
b.通过attr相关?方法去获取、修改、添加和删除它的属性
class Computer:def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memorydef play_game(self):
print('打游戏')def coding(self):
return '写代码'def watch_video(self):
return '看视频'computer1 = Computer('Apple', 'black', '8G')# 方法1
# 获取属性
print(computer1.brand)
print(computer1.color)
print(computer1.memory)# 修改属性
computer1.brand = 'new Apple'
computer1.color = 'new white'
computer1.memory = 'new 8G'# 添加属性
computer1.price = '$1000'
print(computer1.price)# 删除属性
del computer1.price
# print(computer1.price)AttributeError: 'Computer' object has no attribute 'price'# 方法2
# 获取属性
print(getattr(computer1, 'brand'))
print(getattr(computer1, 'color'))
print(getattr(computer1, 'memory'))# 修改属性
setattr(computer1, 'brand', 'new new Apple')
print(computer1.brand)
setattr(computer1, 'color', 'new new white')
print(computer1.color)
setattr(computer1, 'memory', 'new new 8G')
print(computer1.memory)# 添加属性
setattr(computer1, 'price', '$200')
print(computer1.price)# 删除属性
delattr(computer1, 'price')
print(computer1.price)# AttributeError: 'Computer' object has no attribute 'price'
2.声明?个人的类和狗的类:
狗的属性:名字、颜?色、年年龄
狗的?方法:叫唤
人的属性:名字、年年龄、狗
人的?方法:遛狗
a.创建?人的对象?小明,让他拥有?一条狗?大?黄,然后让?小明去遛?大?黄
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = agedef method(self):
print('汪汪汪')class Person:
def __init__(self, name, age):
self.name = name
self.age = age
self.dog = Nonedef personMethod(self):
if not self.dog:
print('没狗')
else:
print('%s溜%s' % (self.name,self.dog.name))bigHuang = Dog('大黄', '黄色', 2)person1 = Person('小明', 18)
person1.dog= bigHuang
person1.personMethod()
3.声明?一个圆类,自己确定有哪些属性和方法
import mathclass Circle:
pi = math.pidef __init__(self, radius):
self.radius = radiusdef area(self):
return '面积为%.2f' % (Circle.pi*self.radius**2)defperimeter(self):
return '周长为%.2f' % (2*Circle.pi*self.radius)circle1 = Circle(3)print(circle1.area())
print(circle1.perimeter())
【python面向对象练习】4.创建?一个学?生类:
属性:姓名,年龄,学号
方法:答到,展示学?生信息
创建?一个班级类:
属性:学?生,班级名
方法:添加学?生,删除学生,点名, 求班上学生的平均年龄
class Students:
def __init__(self, name='', age='', stu_id=''):
self.name = name
self.age = age
self.stu_id = stu_id
def answer(self):
return "到"
def show_info(self):
return {"姓名":self.name, "年龄":self.age, "学号":self.stu_id}stu1 = Students("小明","22","022")
print(stu1.answer())
print(stu1.show_info())class ClassGrade:def __init__(self, stus=[], class_name=''):
self.stus = stus
self.class_name = class_namedef add_stu(self, stu_name='', stu_age=0):
stu = {}
stu["姓名"]=stu_name
stu["年龄"]=stu_age
self.stus.append(stu)
return self.stusdef del_stu(self, stu_name):
for stu in self.stus:
if stu["姓名"]==stu_name:
self.stus.remove(stu)
break
#
def check_name(self, stu_name):
return stu_name+'到'def aver_age(self):
sum_age = 0
for stu in self.stus:
sum_age += stu["年龄"]
return sum_age/len(self.stus)def show_info(self):
return self.class_name, self.stusclass1 = ClassGrade(class_name="1903")
#添加学生
class1.add_stu("小红", 22)
class1.add_stu("小明", 23)
print(class1.show_info())
#删除学生
class1.del_stu("小明")
print("删除学生后的班级信息:", class1.show_info())
#点名
print(class1.check_name("小红"))
#求学生平均年龄
print("该班学生的平均年龄是", class1.aver_age())
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- 数组常用方法一
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)