1.聲明一個電腦類: 屬性:品牌、顏色、內(nèi)存大小 方法:打游戲、寫代碼、看視頻
class Computer:
def __init__(self, brand, color, memory_size):
self.brand = brand
self.color = color
self.memory_size = memory_size
def play_game(self):
print('%s品牌可以打游戲' % self.brand)
def write_code(self):
print('%s品牌可以寫代碼' % self.brand)
def watch_video(self):
print('%s品牌可以看視頻' % self.brand)
my_computer = Computer('聯(lián)想', 'black', '8GB')
print(my_computer.brand, my_computer.color, my_computer.memory_size)
my_computer.play_game()
my_computer.write_code()
my_computer.watch_video()
a.創(chuàng)建電腦類的對象,然后通過對對象點的方式獲取、修改、添加和刪除它的屬性
print(my_computer.color)
my_computer.color = 'white'
print(my_computer.color)
my_computer.price = '54000'
print(my_computer.price)
del my_computer.price
b.通過attr相關(guān)方法去獲取、修改、添加和刪除它的屬性
print(getattr(my_computer, 'color'))
setattr(my_computer, 'color', 'black')
print(my_computer.color)
setattr(my_computer, 'size', '24寸')
print(my_computer.size)
delattr(my_computer, 'size')
2.聲明一個人的類和狗的類:
狗的屬性:名字、顏色、年齡
狗的方法:叫喚
人的屬性:名字、年年齡、狗
人的方法:遛狗
a.創(chuàng)建人的對象小明,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Person:
def __init__(self, name, age, his_dog):
self.name = name
self.age = age
self.his_dog = his_dog
def take_dog(self):
print('%s在遛他的名字叫%s的狗' % (self.name, self.his_dog))
class Dog:
def __init__(self, dog_name, color, dog_age):
self.dog_name = dog_name
self.color = color
self.dog_age = dog_age
def call_out(self):
print('%s正在叫喚' % self.dog_name)
ming = Person('小明', 18, '大黃')
huang = Dog('大黃', 'white', 3)
ming.take_dog()
3.聲明一個圓類,自己確定有哪些屬性和方法
from math import *
class Circle:
def __init__(self, radius, center_coordinates):
self.radius = radius
self.center_coordinates = center_coordinates
def areas(self):
area = pi*self.radius**2
return area
def perimeters(self):
perimeter = 2*pi*self.radius
return perimeter
my_circle = Circle(10, (15, 20))
print(my_circle.radius, my_circle.center_coordinates)
print(my_circle.areas(), my_circle.perimeters())
4.創(chuàng)建一個學(xué)生類:
屬性:姓名,年齡,學(xué)號
方法:答到,展示學(xué)生信息
創(chuàng)建一個班級類:
屬性:學(xué)生,班級名
方法:添加學(xué)生,刪除學(xué)生,點名, 求班上學(xué)生的平均年齡
class Student:
def __init__(self, name, age, study_id):
self.name = name
self.age = age
self.study_id = study_id
def answer(self):
print('%s 到' % self.name)
def stu_information(self):
print(self.name, self.age, self.study_id)
class Class:
def __init__(self, class_name, students=[]):
self.students = students
self.class_name = class_name
def add_student(self, name, age, study_id):
new_student = Student(name, age, study_id)
self.students.append(new_student)
return self.students
def del_student(self, del_id):
for stu in self.students:
if stu.study_id == del_id:
self.students.remove(stu)
return self.students
def call_student(self):
print('點名:')
for stu in self.students:
stu.answer()
def average_age(self):
sum1 = 0
count = 0
for stu in self.students:
sum1 += stu.age
count += 1
return sum1/count
py_class = Class('python')
print(py_class.class_name)
py_class.add_student('小明', 15, '1904001')
py_class.add_student('張三', 16, '1904002')
py_class.add_student('李四', 14, '1904003')
py_class.call_student()
print(py_class.average_age())
py_class.del_student('1904001')