1. 程式人生 > >python基礎:類的簡單使用

python基礎:類的簡單使用

class ini lse end self. choice sse .class 為我

#1.創建兩所學校實列化
#2.學校關聯老師,學生關聯班級
#3.具體功能不實現,只簡單輸出內容

#學校的類(要創建兩所學校)

class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.course_teacher = []
self.course_students = []
self.liunx_grade = []
self.python_grade = []
self.go_grade = []

#關聯課程老師
def creat_course(self,obj_teach_course):
print(‘添加了代課老師%s,是主教%s的!‘%(obj_teach_course.name,obj_teach_course.course))
self.course_teacher.append(obj_teach_course)

#關聯學生課程
def create_student(self,obj_students):
self.course_students.append(obj_students)

#開始分班級 ps:需要分配的是 你創建實例化的北京學堂和上海學堂的course_students列表
def classes(self):
for bei_val in Beijing_school.course_students:
if bei_val.subject == ‘python‘:
Beijing_school.python_grade.append(bei_val)
else:
Beijing_school.liunx_grade.append(bei_val)
for hai_val in Shanghai_school.course_students:
Shanghai_school.go_grade.append(hai_val)

def deduction(self): #統一扣款
pay(self.course_students)

#課程類
class Curriculum(object):
def __init__(self,course_name,course_price,course_cycle):
self.course_name = course_name
self.course_price = course_price
self.course_cycle = course_cycle


#學校的所有人的總類
class All_people(object):
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex

#老師的類

class Teacher(All_people):
def __init__(self,name,age,sex,course):
super(Teacher,self).__init__(name,age,sex)
self.course = course

def action(self):
pass


#學生的類
class Students(All_people):
def __init__(self,name,age,sex,subject,money):
super(Students,self).__init__(name,age,sex)
self.subject = subject
self.money = money

def choice(self):
if self.subject == ‘python‘:
Beijing_school.course_students.append(self)
elif self.subject == ‘liunx‘:
Beijing_school.course_students.append(self)
else:
Shanghai_school.course_students.append(self)

def pay_tuition(self):
pass






#兩所學校實列化
Beijing_school = School(‘北京學堂‘,‘朝陽區‘)
Shanghai_school = School(‘上海學堂‘,‘浦東新區‘)

#三種課程實例化
liunx = Curriculum(‘Liunx‘,5800,‘五個月‘)
python = Curriculum(‘Python‘,6800,‘八個月‘)
go = Curriculum(‘Go‘,3800,‘三個月‘)

#創建老師實列化
teacher_liunx = Teacher(‘張三老師‘,34,‘男‘,‘liunx‘)
teacher_python = Teacher(‘趙雪老師‘,28,‘女‘,‘python‘)
teacher_go = Teacher(‘王五老師‘,45,‘男‘,‘go‘)

#將老師關聯到了學校
Beijing_school.creat_course(teacher_liunx)
Beijing_school.creat_course(teacher_python)
Shanghai_school.creat_course(teacher_go)

#創建學生實列化
maodan = Students(‘毛蛋‘,18,‘男‘,‘python‘,7600)
erya = Students(‘二丫‘,19,‘女‘,‘liunx‘,5000)
sanpao = Students(‘三炮‘,17,‘男‘,‘go‘,6000)

#給選擇的學生分配學堂,然後再分班級
maodan.choice()
erya.choice()
sanpao.choice()

#開始分配班級,因為我在classes寫了全部的代碼,包含了北京學堂和上海學堂,所以這裏只有寫一次這個調用就行了,在協商上海學堂的話
#那麽就會出現重復的添加了
Beijing_school.classes()



#提供一個對所有人做一個統一的扣款接口
def pay(obj):
list_course = [liunx,python,go]
if obj.subject == ‘liunx‘:
if obj.money > liunx.course_price:
print(‘恭喜%s成功報名了為期%s的%s課程‘%(obj.name,liunx.course_cycle,liunx.course_name))
else:
print(‘不好意思%s,您的錢不夠報名您所想學的%s課程‘%(obj.name,liunx.course_name))
elif obj.subject == ‘python‘:
if obj.money > python.course_price:
print(‘恭喜%s成功報名了為期%s的%s課程‘%(obj.name,python.course_cycle,python.course_name))
else:
print(‘不好意思%s,您的錢不夠報名您所想學的%s課程‘%(obj.name,python.course_name))
else:
if obj.money > go.course_price:
print(‘恭喜%s成功報名了為期%s的%s課程‘%(obj.name,go.course_cycle,go.course_name))
else:
print(‘不好意思%s,您的錢不夠報名您所想學的%s課程‘%(obj.name,go.course_name))

pay(maodan)
pay(erya)
pay(sanpao)

python基礎:類的簡單使用