1.聲明一個電腦類: 屬性:品牌期犬、顏色颜懊、內存大小 方法:打游戲财岔、寫代碼、看視頻
a.創(chuàng)建電腦類的對象河爹,然后通過對象點的方式獲取匠璧、修改、添加和刪除它的屬性
b.通過attr相關方法去獲取咸这、修改夷恍、添加和刪除它的屬性
class Computer(object):
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 look_video(self):
print('看視頻')
# a.創(chuàng)建電腦類的對象,然后通過對象點的方式獲取媳维、修改酿雪、添加和刪除它的屬性
#a創(chuàng)建和獲取
computer1 = Computer('蘋果', '白色', '8GB')
print(computer1.memory)
#a修改
computer1.memory = '16GB'
print(computer1.memory)
#a添加
computer1.size = '14inch'
print(computer1.size)
#a刪除
del computer1.color
# print(computer1.color) AttributeError
# b.通過attr相關的方法去獲取、修改侄刽、添加和刪除它的屬性
#b創(chuàng)建和獲取
computer2 = Computer('聯(lián)想', '黑色', '4GB')
print(getattr(computer2, 'color'))
#b修改
setattr(computer2, 'brand', '華碩')
print(computer2.brand)
#b添加
setattr(computer2, 'size', '17')
print(computer2.size)
#b刪除
delattr(computer2, 'color')
# print(computer2.color) # TypeError
2.聲明一個人的類和狗的類:
狗的屬性:名字指黎、顏色、年齡
狗的方法:叫喚
人的屬性:名字州丹、年齡醋安、狗
人的?方法:遛狗
a.創(chuàng)建人的對象小明杂彭,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Dogs(object):
def __init__(self, name, color):
self.name = name
self.color = color
self.age = 0
def dog_call(self):
print('汪汪汪吓揪!')
dog1 = Dogs('旺財', '白色')
class Persons(object):
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def person_way(self):
return print('%s遛%s' % (self.name, self.dog))
person1 = Persons('小明', 18, dog1.name)
person1.person_way()
3.聲明一個圓類:
class Circle(object):
pi = 3.1415926
def __init__(self, r, x, y):
self.r = r
self.x = x
self.y = y
def get_d(self):
return 2*self.r
def get_area(self):
return Circle.pi*self.r**2
def circle_x(self):
return abs(self.y - self.r)
def circle_y(self):
return abs(self.x - self.r)
circle1 = Circle(5, 10, 20)
print(circle1.get_d(), circle1.get_area(), circle1.circle_y(), circle1.circle
4.創(chuàng)建一個學生類:
屬性:姓名亲怠,年齡,學號
方法:答到柠辞,展示學生信息
創(chuàng)建一個班級類:
屬性:學生团秽,班級名
方法:添加學生,刪除學生钾腺,點名, 求班上學生的平均年齡
class Student(object):
def __init__(self, name, age, student_id):
self.name = name
self.age = age
self.student_id = student_id
def response(self):
print('%s到' % self.name)
def show_message(self):
print("""展示%s信息:
姓名:%s
年齡:%d
學號:%d
""" %(self.name,self.name, self.age, self.student_id))
# 創(chuàng)建一個班級類:
# 屬性:學生徙垫,班級名
# 方法:添加學生,刪除學生放棒,點名, 求班上學生的平均年齡
class Class(object):
def __init__(self, student, age, class_number):
self.student = student
self.class_number = class_number
self.age = age
def add_student(self):
student_list.append(self.student)
return student_list
def del_student(self):
student_list.remove(self.student)
return student_list
def call_name(self):
print('呼叫%s' % self.student)
def student_aver(self, age, n):
return age / n
n = 0
ages = 0
student_list = []
while 1:
name = input('請輸入學生的姓名:')
age = int(input('請輸入學生的年齡:'))
student_id = int(input('請輸入學生的學號:'))
student1 = Student(name, age, student_id)
print(student1.age)
student1.response()
student1.show_message()
n += 1
class1 = Class(student1.name, student1.age, 'py1808')
class1.add_student()
print(student_list)
class1.call_name()
print(n)
ages += student1.age
print(class1.student_aver(ages,n))
continue_number = input('請輸入(1-3):')
if continue_number == '1':
continue
elif continue_number == '2':
class1.del_student()
else:
break