1.聲明一個電腦類:
屬性: 品牌腺毫、顏色骂澄、內(nèi)存大小
方法: 打游戲吓蘑、寫代碼、看視頻
a.創(chuàng)建電腦類的對象坟冲,然后通過對象點的方式獲取磨镶、修改、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取健提、修改琳猫、添加和刪除它的屬性
""" 易竹小居士 """
"""
1.聲明一個電腦類:
屬性:品牌、顏色私痹、內(nèi)存大小
方法:打游戲脐嫂、寫代碼、看視頻
a.創(chuàng)建電腦類的對象紊遵,然后通過對象點的方式獲取账千、修改、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取暗膜、修改匀奏、添加和刪除它的屬性
"""
class Computer:
"""聲明一個電腦類"""
def __init__(self, brand='', color='black', ram=0):
self.brand = brand
self.color = color
self.ram = ram
def play_game(self):
print('打游戲')
def write_code(self):
print('寫代碼')
def watch_tv(self):
print('看電視')
if __name__ == '__main__':
cp1 = Computer('lenovo', 'yellow', 4)
"""創(chuàng)建一個電腦對象"""
print(cp1)
# 獲取
print(cp1.color)
print(cp1.brand)
print(cp1.ram)
# 修改
cp1.color = 'red'
print(cp1.color)
cp1.brand = 'mac'
print(cp1.brand)
cp1.ram = 2
print(cp1.ram)
# 添加
cp1.listen = 'music'
print(cp1.listen)
# 刪除
del cp1.ram
#print(cp1.ram) # 在打印就報錯:AttributeError: 'Computer' object has no attribute 'ram'
# 通過attr獲取
print(cp1.__getattribute__('color'))
print(getattr(cp1, 'brand', '電腦品牌'))
# 通過attr增加
cp1.__setattr__('outline', 'rectangle')
print(cp1.outline) #得到rectangle
setattr(cp1, 'outline', 'rectangle')
print(cp1.outline)
#通過attr改
setattr(cp1, 'color', 'pink')
print(cp1.color) #得到pink
cp1.__setattr__('brand', 'hp')
print(cp1.brand) #得到hp
#通過attr刪除
delattr(cp1, 'color')
print(cp1.color) #得到:AttributeError: 'Computer' object has no attribute 'color'
cp1.__delattr__('color')
print(cp1.color) #得到:AttributeError: 'Computer' object has no attribute 'color'
后補:
""" 易竹小居士 """
"""
1.聲明一個電腦類:
屬性:品牌、顏色桦山、內(nèi)存大小
方法:打游戲攒射、寫代碼醋旦、看視頻
"""
class Computer:
"""電腦類"""
def __init__(self, brand='', color='black', memory=0):
self.brand = brand
self.color = color
self.memory = memory
@staticmethod
def play_game(game):
print('打%s游戲' % game)
@staticmethod
def coding(code_type):
print('寫%s程序' % code_type)
@staticmethod
def watch_video(video):
print('在看%s' % video)
"""
2.聲明一個人的類和狗的類:
狗的屬性:名字恒水、顏色会放、年齡 狗的方法:叫喚
人的屬性:名字、年齡钉凌、狗 人的方法:遛狗
a.創(chuàng)建人的對象小明咧最,讓他擁有一條狗大黃,然后讓小明去遛大黃
"""
class Dog:
"""狗類"""
def __init__(self, name='', color='', age=0):
self.name = name
self.color = color
self.age = age
def shout(self):
print('%s:汪汪汪S瘛矢沿!' % self.name)
class Person:
"""人類"""
def __init__(self, name='', age=0):
self.name = name
self.age = age
# None來表示對象的零值
self.dog = None
def walk_dog(self):
"""遛狗"""
if self.dog == None:
print('沒??,遛自己吧!')
return
print('%s牽著%s在散步' % (self.name, self.dog.name))
"""
3.聲明一個矩形類:
屬性:長酸纲、寬 方法:計算周長和面積
a.創(chuàng)建不同的矩形捣鲸,并且打印其周長和面積
"""
class Rectangle:
"""矩形類"""
def __init__(self, length, width):
self.length = length
self.width = width
def perimeter(self):
"""求周長"""
return (self.length + self.width)*2
def area(self):
"""求面積"""
return self.length * self.width
"""
4.創(chuàng)建一個學生類:
屬性:姓名,年齡闽坡,學號 方法:答到栽惶,展示學生信息
創(chuàng)建一個班級類:
屬性:學生,班級名 方法:添加學生疾嗅,刪除學生外厂,點名
"""
from random import randint
class Student:
"""學生類"""
def __init__(self, name='', age=0):
self.name = name
self.age = age
self.study_id = 'py1805'+str(randint(0, 50))
def answer(self):
print('%s,到代承!' % self.name)
def show(self):
print('姓名:%s 年齡:%d 學號:%s' % (self.name, self.age, self.study_id))
class Class:
"""班級類"""
def __init__(self, name=''):
self.name = name
self.students = []
def append_student(self, student=None):
"""添加學生"""
self.students.append(student)
def del_student(self, name):
"""刪除學生"""
for student in self.students[:]:
if student.name == name:
self.students.remove(student)
def call_names(self):
"""點名"""
for student in self.students:
# 點名
print(student.name)
# 答到
student.answer()
"""
5.寫一個類汁蝶,封裝所有和數(shù)學運算相關(guān)的功能(包含常用功能和常用值,例如:pi,e等)
"""
class Math:
"""數(shù)學類"""
pi = 3.1415926
e = 2.718
@staticmethod
def sum(*number):
"""求和"""
sum1 = 0
for x in number:
sum1 += x
return sum1
@classmethod
def cricle_area(cls, radius):
"""求圓的面積"""
return radius**2*cls.pi
if __name__ == '__main__':
"""第一題"""
cm = Computer()
cm.memory = 512
cm.brand = '惠普'
Computer.play_game('貪吃蛇')
# 添加屬性
# cm.price = 998
setattr(cm, 'price', 998)
print(cm.price)
# 刪除屬性
# del cm.color
cm.__delattr__('price')
# 修改屬性
# cm.memory = 258
cm.__setattr__('memory', 1024)
"""第二題"""
p1 = Person('小明', 18)
p1.dog = Dog('大黃', 'yellow', 3)
p1.walk_dog()
"""第三題"""
rect1 = Rectangle(10, 20)
print(rect1.area(), rect1.perimeter())
rect2 = Rectangle(100, 30)
print(rect2.perimeter())
"""第四題"""
stu1 = Student('張三', 20)
stu2 = Student('aa', 18)
stu3 = Student('bb', 30)
class1 = Class('py1805')
class1.append_student(stu1)
class1.append_student(stu2)
class1.append_student(stu3)
class1.call_names()
# 注意:如果函數(shù)的參數(shù)是對象(列表论悴、字典掖棉、類的對象),傳參的時候傳的是地址膀估,如果函數(shù)中需要對象的內(nèi)容進行修改啊片,
# 傳參的時候傳對象的拷貝(如果是列表可以切片)
def func1(list1):
list1.append(10)
print(list1)
def func2(lista):
lista.append(20)
print(lista)
list11 = [1, 2, 3, 4]
func1(list11[:])
func2(list11[:])
print(list11)
print(Math.sum(10, 2, 3))
print(Math.cricle_area(3))
print(Math.pi)