1.聲明?個電腦類:
屬性:品牌抱冷、顏?、內(nèi)存??
?法:打游戲添诉、寫代碼屁桑、看視頻
a.創(chuàng)建電腦類的對象,然后通過對象點的?式獲取栏赴、修改蘑斧、添加和刪除它的屬性
b.通過attr相關?法去獲取、修改艾帐、添加和刪除它的屬性
class Computer:
"""電腦類"""
def __init__(self, brand='asus', color= 'black', memory='4G'):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print('van游戲')
def watch_video(self):
print('看視頻')
def __str__(self):
return 'brand:%s color:%s memory:%s' % (self.brand, self.color, self.memory)
if __name__ == '__main__':
laptop = Computer('asus', 'red_black', '8G')
setattr(laptop, 'brand', 'acer')
color = laptop.__getattribute__('color')
print(color)
# 添加cpu屬性
laptop.cpu = 'i7_4720HQ'
print(laptop)
print('cpu:', laptop.cpu)
laptop.__delattr__('color')
# 檢測color屬性是否被刪除
print(getattr(laptop, 'color', None))
結果:
red_black
brand:acer color:red_black memory:8G
cpu: i7_4720HQ
None
2.聲明?個?的類和狗的類:
狗的屬性:名字、顏?盆偿、年齡 狗的?法:叫喚
?的屬性:名字柒爸、年齡、狗 ?的?法:遛狗
a.創(chuàng)建?的對象?明事扭,讓他擁有?條狗??捎稚,然后讓?明去遛??
class Human:
"""人的類"""
def __init__(self, name='', age=0, dog=None):
self.name = name
self.age = age
self.dog = dog
def walk_a_dog(self, dog):
print('%s正在遛%s' % (self.name, dog.name))
class Dog:
"""狗的類"""
def __init__(self, name='', color='yellow', age=0):
self.name = name
self.color = color
self.age = age
def barking(self):
print('%s:啊汪!' % self.name)
if __name__ == '__main__':
dog = Dog('tom', 'blue', 2)
man = Human('jerry', 18, dog)
man.walk_a_dog(dog)
dog.barking()
結果:
jerry正在遛tom
tom:啊汪求橄!
3.聲明?個矩形類:
屬性:?今野、寬 ?法:計算周?和?積
a.創(chuàng)建不同的矩形,并且打印其周?和?積
import random
class Rect:
"""矩形的類"""
def __init__(self, length=0, width=0):
self.length = length
self.width = width
def perimeter(self):
perimeter = (self.width + self.length) * 2
print('周長是:%d' % perimeter)
def slice(self):
slice = self.width * self.length
print('面積是:%d' % slice)
if __name__ == '__main__':
length = random.randint(1, 10)
width = random.randint(1, 10)
rect = Rect(length, width)
print('長:', length, '寬:', width)
rect.perimeter()
rect.slice()
結果:
長: 9 寬: 10
周長是:38
面積是:90
4.創(chuàng)建?個學?類:
屬性:姓名罐农,年齡条霜,學號 ?法:答到,展示學?信息 創(chuàng)建?個班級類:
屬性:學?涵亏,班級名 ?法:添加學?宰睡,刪除學?,點名
class Class:
"""班級的類"""
def __init__(self, name='', students=[]):
self.name = name
self.students = students
def student_check(self, name):
print('老師:%s' % name)
for index in range(len(self.students)):
if name == self.students[index].name:
result = self.students[index].say_yes(name)
if result:
print('%s到了' % name)
return True
else:
print('······')
print('%s曠課气筋!' % name)
return False
def add_student(self):
name = input('name:')
age = int(input('age:'))
id = input('id:')
# 根據(jù)輸入的信息創(chuàng)建學生對象
stu = Student(name, age, id)
# 添加學生
self.students.append(stu)
def del_student(self, name):
for index in range(len(self.students)):
if name == self.students[index].name:
del self.students[index]
print('刪除成功')
return
else:
print('查無此人')
return
class Student:
"""學生的類"""
def __init__(self, name='', age=0, id=''):
self.name = name
self.age = age
self.id = id
def say_yes(self, name):
if name == self.name:
print('%s:到' % name)
return True
else:
return False
def __str__(self):
return 'name:%s id:%s age:%d' % (self.name, self.id, self.age)
if __name__ == '__main__':
student = Student('張三', 20, '9526')
cls1 = Class('py1085')
cls1.add_student()
print(cls1.students[0])
cls1.student_check('張三')
cls1.student_check('李四')
結果:
name:李四
age:20
id:9527
name:李四 id:9527 age:20
老師:張三
······
張三曠課拆内!
老師:李四
李四:到
李四到了
5.寫?個類,封裝所有和數(shù)學運算相關的功能(包含常?功能和常?值宠默,例如:pi,e等)
class Arithmetic:
"""數(shù)學運算的類"""
@staticmethod
def sum(num1, num2):
return num1 + num2
@staticmethod
def subtraction(num1, num2):
return num1 - num2
@staticmethod
def multiplication(num1, num2):
return num1 * num2
@staticmethod
def division(num1, num2):
return num1/num2
if __name__ == '__main__':
func = Arithmetic()
print(func.sum(12, 20))
print(func.subtraction(20, 12))
print(func.multiplication(12, 20))
print(func.division(12, 20))
結果:
32
8
240
0.6
6.1.寫?個班級類麸恍,屬性:班級名、學?搀矫;功能:添加學?抹沪、刪除學?刻肄、根據(jù)姓名查看學?信息,展示班級的所有學?信息
class Class:
"""班級類"""
def __init__(self, name=''):
self.class_name = name
self.students = []
def add_student(self):
while True:
name = input('name(input 0 to back):')
if name == '0':
return
sex = input('sex:')
age = int(input('age:'))
# 根據(jù)輸入的信息創(chuàng)建學生對象
stu = Student(name, sex, age)
# 添加學生
self.students.append(stu)
def find_student(self, name):
for index in range(len(self.students)):
if name == self.students[index].name:
return self.students[index]
else:
print('查無此人')
return
def del_student(self, name):
for index in range(len(self.students)):
if name == self.students[index].name:
del self.students[index]
print('刪除成功')
return
else:
print('查無此人')
return
class Student:
"""學生類"""
def __init__(self, name='', sex='', age=0):
self.name = name
self.sex = sex
self.age = age
def __str__(self):
return 'name:%s sex:%s age:%d' % (self.name, self.sex, self.age)
if __name__ == '__main__':
cls1 = Class('py1805')
while True:
print('''
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
''')
cmd = input('>>>>>')
if not '0' <= cmd <= '4':
print('輸入有誤')
continue
if cmd == '1':
cls1.add_student()
if cmd == '2':
while True:
name = input('find student by name(input 0 to back):')
if name == '0':
break
stu = cls1.find_student(name)
print(stu)
if cmd == '3':
while True:
name = input('delete student by name(input 0 to back):')
if name == '0':
break
stu = cls1.del_student(name)
if cmd == '4':
for index in range(len(cls1.students)):
print(cls1.students[index])
if cmd == '0':
break
結果:
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>1
name(input 0 to back):張三
sex:男
age:20
name(input 0 to back):李四
sex:女
age:21
name(input 0 to back):王五
sex:男
age:23
name(input 0 to back):0
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>2
find student by name(input 0 to back):李四
name:李四 sex:女 age:21
find student by name(input 0 to back):0
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>
輸入有誤
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>4
name:張三 sex:男 age:20
name:李四 sex:女 age:21
name:王五 sex:男 age:23
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>3
delete student by name(input 0 to back):李四
刪除成功
delete student by name(input 0 to back):0
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>4
name:張三 sex:男 age:20
name:王五 sex:男 age:23
++++++++++++++++++++++++++++
+ simple system +
+ 1.add student +
+ 2.find student +
+ 3.delete student +
+ 4.find all +
+ 0.exit +
++++++++++++++++++++++++++++
>>>>>0
Process finished with exit code 0