1.聲明?個電腦類: 屬性:品牌、顏?异雁、內(nèi)存?小 方法:打游戲捶障、寫代碼、看視頻
a.創(chuàng)建電腦類的對象纲刀,然后通過對象點的方式獲取项炼、修改、添加和刪除它的屬性
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('打游戲')
def write_code(self):
print('寫代碼')
def watch_videos(self):
print('看視頻')
com1 = Computer('apple', 'white', '8G')
print(com1.brand, com1.color, com1.memory)
com1.brand = 'ASUS'
del com1.memory
setattr(com1, 'color', 'gray')
getattr(com1, 'brand')
setattr(com1, 'memory', '16G')
delattr(com1, 'color')
print(com1.brand, com1.memory)
2.聲明?個人的類和狗的類:
狗的屬性:名字、顏?面褐、年齡
狗的方法:叫喚
人的屬性:名字拌禾、年齡、狗
人的方法:遛狗
a.創(chuàng)建人的對象小明展哭,讓他擁有一條狗大黃湃窍,然后讓小明去遛大黃
class Person:
def __init__(self, name, dog, age):
self.name = name
self.dog = dog
self.age = age
def person_funcetion(self):
print('叫喚')
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def dog_function(self):
print('遛狗')
p1 = Person('name', 'age', 'dog')
d1 = Dog('name', 'color', 'age')
setattr(p1, 'name', '小明')
setattr(d1, 'name', '大黃')
print(getattr(p1, 'name')+'遛'+getattr(d1, 'name'))
3.聲明?一個圓類,自己確定有哪些屬性和方法
from math import pi
class Circular:
def __init__(self, radius):
self.radius = radius
def area(self):
area1 = pi*self.radius**2
print(area1)
def perimeter(self):
perimeter1 = 2*pi*self.radius
print(perimeter1)
c1 = Circular('radius')
setattr(c1, 'radius', 3)
c1.area()
c1.perimeter()
4.創(chuàng)建?一個學(xué)?生類:
屬性:姓名匪傍,年齡您市,學(xué)號
方法:答到,展示學(xué)?生信息
class Student:
def __init__(self, name, age, stu_id):
self.name = name
self.age = age
self.stu_id = stu_id
def answer(self):
print(self.name + '到役衡!')
def message(self):
print('姓名:%s\n年齡:%d\n學(xué)號:%s' % (self.name, self.age, self.stu_id))
s1 = Student('name', 'age', 'stu_id')
setattr(s1, 'name', '小明')
s1.answer()
s1 = Student('小紅', 21, '001')
s1.message()
5.創(chuàng)建?一個班級類:
屬性:學(xué)?生茵休,班級名
方法:添加學(xué)?生,刪除學(xué)生手蝎,點名, 求班上學(xué)生的平均年齡