1.聲明一個(gè)電腦類: 屬性:品牌篓冲、顏色宠哄、內(nèi)存大小壹将。
方法:打游戲、寫代碼毛嫉、看視頻
a.創(chuàng)建電腦類的對(duì)象诽俯,然后通過(guò)對(duì)象點(diǎn)的方式獲取、修改承粤、添加和刪除它的屬性
b.通過(guò)attr相關(guān) 法去獲取暴区、修改、添加和刪除它的屬性
class Computer:
def __init__(self,brand,color,memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
return '打游戲'
def write_code(self):
return '寫代碼'
def watch_video(self):
return '看視頻'
x = Computer('lenovo','black','512G')
print('電腦可以:',x.play_game(),x.write_code(),x.watch_video())
# 獲取
print(x.brand) # lenovo
print(getattr(x,'color')) # black
# 修改
x.brand = 'apple'
x.color = 'white' # white
print(x.brand,x.color) # apple white
# 添加
x.price = 5000
setattr(x,'thickness','30mm')
print(x.price,x.thickness) # 5000 30mm
# 刪除
del x.brand
delattr(x,'color')
print(x.brand,x.color) # AttributeError: 'Computer' object has no attribute 'brand'
2.聲明一個(gè)人的類和狗的類:
狗的屬性:名字辛臊、顏色仙粱、年齡 狗的方法:叫喚 人的屬性:名字、年齡彻舰、狗 人的方法:遛狗
a.創(chuàng)建人的對(duì)象小明伐割,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Dog:
def __init__(self,name,color,age):
self.name = name
self.color = age
self.age = age
def dog(self):
print('汪汪汪')
class Person:
def __init__(self,name,age,dog):
self.name1 = name
self.age1 = age
self.dog1 = dog
def xiao_ming(self):
print('遛狗')
if __name__ == '__main__':
d1 = Dog('大黃','黃色','7')
p1 = Person('小明',18,d1.name)
print('%s遛%s'%(p1.name1,d1.name))
3.聲明一個(gè)矩形類:
屬性:長(zhǎng)刃唤、寬 方法:計(jì)算周長(zhǎng)和面積
a.創(chuàng)建不同的矩形隔心,并且打印其周長(zhǎng)和面積
class Rectangle:
def __init__(self,length,width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return (self.length + self.width)*2
length1 = int(input('請(qǐng)輸入長(zhǎng)度:'))
width1 = int(input('請(qǐng)輸入寬度:'))
rect = Rectangle(length1,width1)
print('矩形面積是:',rect.area())
print('矩形周長(zhǎng)是:',rect.perimeter())
4.創(chuàng)建一個(gè)學(xué)生類:
屬性:姓名,年齡,學(xué)號(hào) 方法:答到尚胞,展示學(xué)生信息 創(chuàng)建一個(gè)班級(jí)類
屬性:學(xué)生硬霍,班級(jí)名 方法:添加學(xué)生 ,刪除學(xué)生辐真,點(diǎn)名
class Student:
def __init__(self,name,age,id):
self.name = name
self.age = age
self.id = id
def call_name(self,):
print(self.name)
def stu_info(self):
print('到!我的名字是%s,今年%d歲,學(xué)號(hào)是%s'%(self.name,self.age,self.id))
s1 = Student('陳一',17,'001')
s2 = Student('王二',18,'002')
s3 = Student('張三',19,'003')
s4 = Student('李四',17,'004')
s1.call_name()
s1.stu_info()
class Class:
def __init__(self,class_name,students):
self.class_name = class_name
self.students = []
def add_stu(self,name):
self.students.append(name)
def del_stu(self,name):
for stu1 in self.students:
if stu1 == name:
self.students.remove(name)
def call_name1(self,name):
for stu2 in self.students:
if stu2 == name:
print(stu2)
c1 = Class('python1806',[])
c1.add_stu('王二')
c1.add_stu('陳一')
c1.add_stu('張三')
print(c1.students)
c1.del_stu('王二')
print(c1.students)
c1.call_name1('張三')
5.寫一個(gè)類须尚,封裝所有和數(shù)學(xué)運(yùn)算相關(guān)的功能(包含常用功能和常用值,例如:pi,e等)
class Math:
def __init__(self,num1,num2):
self.num1 = num1
self.num2 = num2
def operation(self):
return (self.num1*self.num2),(self.num1/self.num2),(self.num1+self.num2),(self.num1-self.num2)
def e(self):
return (self.num1**2,self.num2**2)
def pi(self):
return (self.num1**2*3.14,self.num2**2*3.14)
m1 = Math(10,20)
print(m1.operation())
print(m1.e())
print(m1.pi())