1.聲明一個(gè)電腦類:屬性:品牌、顏色坤溃、內(nèi)存大小 方法:打游戲、寫代碼嘱丢、看視頻
a.創(chuàng)建電腦類的對象薪介,然后通過對象點(diǎn)的方式獲取、修改越驻、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取汁政、修改、添加和刪除它的屬性
class Computer:
def __init__(self,brand,color,memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print('play games')
def write_codes(self):
print('write codes')
def look_video(self):
print('look at video')
if __name__ == '__main__':
computer = Computer('HP','white','2g')
computer.play_game()
print(computer.brand,computer.color,computer.memory)
computer.brand = 'Apple'#修改
computer.type = 'PC'#添加
del computer.color #刪除
print(computer.brand, computer.type, computer.memory) #查看
setattr(computer,'brand','華碩') #修改
delattr(computer,'memory') #刪除
print(getattr(computer,'brand'),getattr(computer,'color','Black'),computer.__getattribute__('type')) #查看和添加
result:
play games
HP white 2g
Apple PC 2g
華碩 Black PC
2.聲明一個(gè)人的類和狗的類:
狗的屬性:名字伐谈、顏色烂完、年齡 狗的方法:叫喚
人的屬性:名字、年齡诵棵、狗
人的方法:遛狗
a.創(chuàng)建人的對象小明抠蚣,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Dog:
def __init__(self,name,color,age):
self.color = color
self.name = name
self.age = age
def voice(self):
return(self.name+'-汪汪汪')
class Person:
def __init__(self,name,age,dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self,voice):
print(self.name + '->遛狗->'+self.dog+'叫喚->'+voice)
if __name__ == '__main__':
dog = Dog('大黃','粉色','3')
person = Person('小明','10',dog.name)
person.walk_dog(dog.voice())
result:
小明->遛狗->大黃叫喚->大黃-汪汪汪
聲明一個(gè)矩形類:屬性:長履澳、寬 ?方法:計(jì)算周長和面積
a.創(chuàng)建不同的矩形嘶窄,并且打印其周長和面積
class Rectangle:
def __init__(self,length,width):
self.length = length
self.width = width
def circumference(self):
return '周長:%.2f' % 2*(self.length+self.width)
def area(self):
return '面積:%.2f' % 2*self.length*self.width
創(chuàng)建一個(gè)學(xué)生類:屬性:姓名,年齡距贷,學(xué)號 ?方法:答到柄冲,展示學(xué)生信息
創(chuàng)建一個(gè)班級類:屬性:學(xué)生,班級名 ?方法:添加學(xué)生忠蝗,刪除學(xué)生现横,點(diǎn)名
class Student:
def __init__(self,name,age,id):
self.name = name
self.age = age
self.id = id
def sign_in(self):
print(self.name+'已到達(dá)')
def show_student(self):
print( {'name':self.name,'age':self.age,'id':self.id})
class Class:
def __init__(self,class_name,students):
self.class_name = class_name
self.students = students
def add_student(self,student):
"""
添加學(xué)生到學(xué)生列表中
:param student: 學(xué)生信息的字典
:return:
"""
self.students.apppend(student)
print('添加學(xué)生成功.')
def del_student(self,student_name):
"""
傳入學(xué)生的姓名后從學(xué)生列表中刪除該學(xué)生
:param student_name:
:return:
"""
for stu in self.students[:]:
if student_name == stu['name']:
self.students.remove(stu)
print('刪除成功')
break
print('刪除失敗.')
def call_name(self):
for stu in self.students:
print(stu['name'],end=' ')
5.寫一個(gè)類,封裝所有和數(shù)學(xué)運(yùn)算相關(guān)的功能(包含常用功能和常用值阁最,例例如:pi, e等)
class Maths:
pi = 3.141592653589793
e = 2.718281828459045
@classmethod
def addition(cls,*number):
"""
加法
:param number:遠(yuǎn)算數(shù)元組
:return:
"""
return(sum(number))
@classmethod
def subtraction(cls,num,*number):
"""
減法
:param num:減數(shù)
:param number:被減數(shù)元組
:return:
"""
return(num-sum(number))
@classmethod
def multiplication(cls,*number):
"""
乘法
:param number: 乘數(shù)元組
:return:
"""
count = 1
for item in number:
count *= item
return count
@classmethod
def division(cls,num,*number):
"""
除法
:param num:除數(shù)
:param number:被除數(shù)元組
:return:
"""
for item in number:
num /= item
return num
@classmethod
def exponentiation(cls, number,num):
"""
乘方
:param number: 底數(shù)
:param num: 階數(shù)
:return:
"""
count = 1
if num > 0:
for _ in range(num):
count *= number
return count
@classmethod
def factorial(cls, number,num):
"""
階乘
:param number: 底數(shù)
:param num:階數(shù)
:return:
"""
count = 1
if not num:
return 1
if num > 0:
for n in range(1,num+1):
count *= n
return count
6.1.寫一個(gè)班級類戒祠,屬性:班級名、學(xué)生速种; 功能:添加學(xué)生姜盈、刪除學(xué)生、根據(jù)姓名查看學(xué)生信息配阵,展示班級的所有學(xué)生信息
class Class2:
def __init__(self,class_name,students):
self.class_name = class_name
self.students = students
def add_student(self,student):
"""
添加學(xué)生到學(xué)生列表中
:param student: 學(xué)生信息的字典
:return:
"""
self.students.apppend(student)
print('添加學(xué)生成功.')
def del_student(self,student_name):
"""
傳入學(xué)生的姓名后從學(xué)生列表中刪除該學(xué)生
:param student_name:
:return:
"""
for stu in self.students[:]:
if student_name == stu['name']:
self.students.remove(stu)
print('刪除成功')
break
print('刪除失敗.')
def query_student(self,student_name):
for stu in self.students:
if student_name == stu['name']:
print('找到該學(xué)生:%s' % stu)
break
print('沒有找到該學(xué)生')
def all_students(self):
for stu in self.students:
print(stu)