面向物件 類與類之間的關係 (依賴,組合,聚合,等)
阿新 • • 發佈:2018-11-08
-----------------------------困難是一位嚴厲的老師,他產生於克服困難的努力中。
# ---------------------------------------------------------------------------------#
# class Page:
# def __init__(self,lst,pagesize):
# self.lst =lst
# self.pagesize = pagesize
#
# def start(self,a):
# return a
#
# def end(self,b): #[返回最後一頁]
# return b
# def index(self,x): #[返回指定頁面]
# return x
#
# ye = input("輸入要查詢的資料的頁碼-->")
# ---------------------------------------------------------------------------------#
#
#
# class Elphent:
# def open(self,bx):
# print("走到冰箱")
# self.kai
#
# def zhuang(self):
# print()
#
# def guan(self,bx):
# print()
#
# class Bxiang:
# def kai(self):
# print("開!")
# def guan(self):
# print("關!")
#
# dx=Elphent()
# dx.open()
# dx.kai
# ---------------------------------------------------------------------------------#
# class Plant:
# def __init__(self, name, atk):
# self.name = name
# self.atk = atk
#
# def killzom(self):
# print("植物%s攻擊了殭屍%s" % (self.name,munaiyi.name))
#
# class Zombie:
# def __init__(self,name,hp):
# self.name=name
# self.hp=hp
#
# def re_hp(self):
# print("殭屍受到傷害掉血")
#
# xiangrikui = Plant("小葵花",20)
# munaiyi =Zombie("木乃伊",1000)
#
# while 1:
# xiangrikui.killzom()
# print(munaiyi.re_hp()-xiangrikui.atk)
# ---------------------------------------------------------------------------------#
# class Wufan():
# def __init__(self,caiming,tangming,canju=None):
# self.caiming=caiming
# self.tangming =tangming
# self.canju = canju
#
# def chi(self):
# if self.canju:
# self.canju.addmore()
# print("吃!")
# else:
# print("吃個剷剷")
#
# class kuaizi:
# def __init__(self,name):
# self.name = name
#
# def addmore(self):
# print("再來一雙")
#
# pig = Wufan("隆江豬腳飯","紫菜蛋花")
# kz=kuaizi("筷子")
#
# pig.canju = kz
# pig.chi()
# pig.canju = None
# pig.chi()
# ---------------------------------------------------------------------------------#
# class Teach:
# def __init__(self, name, nist=None):
# self.name = name
# if nist == None: #判斷傳遞過來的引數是否是空
# self.nist = []
# else: #傳遞過來的是一個列表
# self.nist = nist
#
# #執行新增學生操作
# def add(self, student):
# self.nist.append(student)
# pass
#
# def display(self):
# for s in self.nist: #s 是老師的學生
# print(s.name)
#
#
# class Students:
# def __init__(self, num, name, teacher=None):
# self.num = num
# self.name = name
# self.teacher = teacher
#
# t =Teach("王大大")
# s1 = Students(1,"康師傅")
# s2 = Students(2,"康帥傅")
# s3 = Students(3,"康大師傅")
# s4 = Students(4,"康康大冒險")
# ---------------------------------------------------------------------------------#
# 1. 看程式碼寫結果;
class F3(object):
def f1(self):
ret = super().f1() # super 執行下一個操作
print(ret)
return 123
class F2(object):
def f1(self):
print('123')
class F1(F3, F2):
pass
obj = F1()
obj.f1() # --> 123 123
# ---------------------------------------------------------------------------------#
# 2. 看程式碼寫結果
class F1(object):
def __init__(self, a1):
self.a1 = a1
def f2(self, arg):
print(self.a1, arg)
class F2(F1):
def f2(self, arg):
print('666')
obj_list = [F1(1), F2(2), F2(3)]
for obj in obj_list:
obj.f2("喜羊羊")
# F1(1) --> 1 喜羊羊
# F2(2) --> 666
# F3(3) --> 666
# ---------------------------------------------------------------------------------#
# 3.看程式碼寫結果
class F1(object):
def __init__(self, a1):
self.a1 = a1
def f2(self, arg):
print(self.a1, arg)
class F2(F1):
def f2(self, arg):
print('666')
obj_list = [F1(1), F2(2), F2(3)]
for obj in obj_list:
print(obj.f2("喜羊羊"))
# F1(1) --> 1,喜羊羊
# F2(2) --> 666
# F2(3) --> 666
# ---------------------------------------------------------------------------------#
# 4.看程式碼寫結果
class F1(object):
def __init__(self,num):
self.num = num
def func(self,request):
print(self.num,request)
def run(self):
self.func(999)
class F2(F1):
def func(self,request):
print(666,self.num)
objs = [F1(1),F2(2),F2(3)]
objs[1].run() #==> F2(2).run() --> 666 2
objs[2].run() #==> F2(3).run() --> 666 3
# ---------------------------------------------------------------------------------#
# 5.看程式碼寫結果
class UserInfo(object):
pass
class Department(object):
pass
class StarkConfig(object):
def __init__(self, num):
self.num = num
def changelist(self, request):
print(self.num, request)
def run(self):
self.changelist(999)
class RoleConfig(StarkConfig):
def changelist(self, request):
print(666, self.num)
class AdminSite(object):
def __init__(self):
self._registry = {}
def register(self, k, v): #UserInfo #StarkConfig
self._registry[k] = v(k) #self._registry[UserInfo] = StarkConfig(UserInfo)
site = AdminSite()
site.register(UserInfo, StarkConfig) # self._registry = {UserInfo,StarkConfig}
site.register(Department, RoleConfig) # self._registry = {Department,RoleConfig}
for k, row in site._registry.items():
row.run()
# ---------------------------------------------------------------------------------#
# 6.看程式碼寫結果
class UserInfo(object):
pass
class Department(object):
pass
class StarkConfig(object):
def __init__(self, num):
self.num = num
def get_vals(self):
v = [11, 22, 33]
extra = self.extra_vals()
if extra:
v.extend(extra)
return v
def extra_vals(self):
pass
def run(self):
return self.get_vals()
class RoleConfig(StarkConfig):
def extra_vals(self):
return [99, 88]
class AdminSite(object):
def __init__(self):
self._registry = {}
def register(self, k, v):
self._registry[k] = v(k)
site = AdminSite()
site.register(UserInfo, StarkConfig)
site.register(Department, RoleConfig)
for k, row in site._registry.items():
print(row.run())
# -- > 同第5題
# ---------------------------------------------------------------------------------#
# 7. 背寫你瞭解的所有特殊方法並附示例;
# ---------------------------------------------------------------------------------#
# 8. 看程式碼寫結果;
class StarkConfig(object):
def __init__(self, num):
self.num = num
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(self.num)
v1 = StarkConfig(1)
v2 = RoleConfig(11)
v1() #--> 1 , 11
v2() # --> 11
# ---------------------------------------------------------------------------------#
# 9. 看程式碼寫結果;
class StarkConfig(object):
def __init__(self, num):
self.num = num
def run(self):
self()
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(345)
v1 = StarkConfig(1)
v2 = RoleConfig(11)
print(v1.run()) # 1
print(v2.run()) # 11 345
# ---------------------------------------------------------------------------------#
# 10. 看程式碼寫結果。
class StarkConfig(object):
def __init__(self, num):
self.num = num
def run(self):
self()
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(345)
def __getitem__(self, item): #wupeiqi
return self.num[item]
v1 = RoleConfig('alex')
v2 = StarkConfig("wupeiqi")
print(v1[1]) #--> 345 alex
print(v2[2]) #--> none 或者error
# ---------------------------------------------------------------------------------#
# 11. (升級題)
'''
1.完成學生選課系統
學生選課
學生:
資訊:學號,姓名,住址選的課程列表,
功能:
檢視:檢視該學生所有課程資訊
新增課程:把選好的課程新增到課程列表中(傳參)
課程:
資訊: 課程編號,課程名稱,老師
功能:
檢視:檢視該課程的全部資訊
設定老師:給當前課程設定一個老師.(傳參)
老師:
資訊:老師編號,老師名稱,老師電話
功能:無
請完成以上三個類.建立6個課程,6個老師.給課程安排好老師.然後建立30個學生
每個學生隨機被分配3個課程.最終顯示出這三十個學生的選課情況以及任課老師的電話
'''
class student:
def __init__(self,nom,name,adress):
pass
def skill(self,read,add_lesson):
pass
class lesson:
def __init__(self,nom,leson_name,teacher):
pass
class Teacher:
def __init__(self,nom,name,phone):
pass
# class Page:
# def __init__(self,lst,pagesize):
# self.lst =lst
# self.pagesize = pagesize
#
# def start(self,a):
# return a
#
# def end(self,b): #[返回最後一頁]
# return b
# def index(self,x): #[返回指定頁面]
# return x
#
# ye = input("輸入要查詢的資料的頁碼-->")
# ---------------------------------------------------------------------------------#
#
#
# class Elphent:
# def open(self,bx):
# print("走到冰箱")
# self.kai
#
# def zhuang(self):
# print()
#
# def guan(self,bx):
# print()
#
# class Bxiang:
# def kai(self):
# print("開!")
# def guan(self):
# print("關!")
#
# dx=Elphent()
# dx.open()
# dx.kai
# ---------------------------------------------------------------------------------#
# class Plant:
# def __init__(self, name, atk):
# self.name = name
# self.atk = atk
#
# def killzom(self):
# print("植物%s攻擊了殭屍%s" % (self.name,munaiyi.name))
#
# class Zombie:
# def __init__(self,name,hp):
# self.name=name
# self.hp=hp
#
# def re_hp(self):
# print("殭屍受到傷害掉血")
#
# xiangrikui = Plant("小葵花",20)
# munaiyi =Zombie("木乃伊",1000)
#
# while 1:
# xiangrikui.killzom()
# print(munaiyi.re_hp()-xiangrikui.atk)
# ---------------------------------------------------------------------------------#
# class Wufan():
# def __init__(self,caiming,tangming,canju=None):
# self.caiming=caiming
# self.tangming =tangming
# self.canju = canju
#
# def chi(self):
# if self.canju:
# self.canju.addmore()
# print("吃!")
# else:
# print("吃個剷剷")
#
# class kuaizi:
# def __init__(self,name):
# self.name = name
#
# def addmore(self):
# print("再來一雙")
#
# pig = Wufan("隆江豬腳飯","紫菜蛋花")
# kz=kuaizi("筷子")
#
# pig.canju = kz
# pig.chi()
# pig.canju = None
# pig.chi()
# ---------------------------------------------------------------------------------#
# class Teach:
# def __init__(self, name, nist=None):
# self.name = name
# if nist == None: #判斷傳遞過來的引數是否是空
# self.nist = []
# else: #傳遞過來的是一個列表
# self.nist = nist
#
# #執行新增學生操作
# def add(self, student):
# self.nist.append(student)
# pass
#
# def display(self):
# for s in self.nist: #s 是老師的學生
# print(s.name)
#
#
# class Students:
# def __init__(self, num, name, teacher=None):
# self.num = num
# self.name = name
# self.teacher = teacher
#
# t =Teach("王大大")
# s1 = Students(1,"康師傅")
# s2 = Students(2,"康帥傅")
# s3 = Students(3,"康大師傅")
# s4 = Students(4,"康康大冒險")
# ---------------------------------------------------------------------------------#
# 1. 看程式碼寫結果;
class F3(object):
def f1(self):
ret = super().f1() # super 執行下一個操作
print(ret)
return 123
class F2(object):
def f1(self):
print('123')
class F1(F3, F2):
pass
obj = F1()
obj.f1() # --> 123 123
# ---------------------------------------------------------------------------------#
# 2. 看程式碼寫結果
class F1(object):
def __init__(self, a1):
self.a1 = a1
def f2(self, arg):
print(self.a1, arg)
class F2(F1):
def f2(self, arg):
print('666')
obj_list = [F1(1), F2(2), F2(3)]
for obj in obj_list:
obj.f2("喜羊羊")
# F1(1) --> 1 喜羊羊
# F2(2) --> 666
# F3(3) --> 666
# ---------------------------------------------------------------------------------#
# 3.看程式碼寫結果
class F1(object):
def __init__(self, a1):
self.a1 = a1
def f2(self, arg):
print(self.a1, arg)
class F2(F1):
def f2(self, arg):
print('666')
obj_list = [F1(1), F2(2), F2(3)]
for obj in obj_list:
print(obj.f2("喜羊羊"))
# F1(1) --> 1,喜羊羊
# F2(2) --> 666
# F2(3) --> 666
# ---------------------------------------------------------------------------------#
# 4.看程式碼寫結果
class F1(object):
def __init__(self,num):
self.num = num
def func(self,request):
print(self.num,request)
def run(self):
self.func(999)
class F2(F1):
def func(self,request):
print(666,self.num)
objs = [F1(1),F2(2),F2(3)]
objs[1].run() #==> F2(2).run() --> 666 2
objs[2].run() #==> F2(3).run() --> 666 3
# ---------------------------------------------------------------------------------#
# 5.看程式碼寫結果
class UserInfo(object):
pass
class Department(object):
pass
class StarkConfig(object):
def __init__(self, num):
self.num = num
def changelist(self, request):
print(self.num, request)
def run(self):
self.changelist(999)
class RoleConfig(StarkConfig):
def changelist(self, request):
print(666, self.num)
class AdminSite(object):
def __init__(self):
self._registry = {}
def register(self, k, v): #UserInfo #StarkConfig
self._registry[k] = v(k) #self._registry[UserInfo] = StarkConfig(UserInfo)
site = AdminSite()
site.register(UserInfo, StarkConfig) # self._registry = {UserInfo,StarkConfig}
site.register(Department, RoleConfig) # self._registry = {Department,RoleConfig}
for k, row in site._registry.items():
row.run()
# ---------------------------------------------------------------------------------#
# 6.看程式碼寫結果
class UserInfo(object):
pass
class Department(object):
pass
class StarkConfig(object):
def __init__(self, num):
self.num = num
def get_vals(self):
v = [11, 22, 33]
extra = self.extra_vals()
if extra:
v.extend(extra)
return v
def extra_vals(self):
pass
def run(self):
return self.get_vals()
class RoleConfig(StarkConfig):
def extra_vals(self):
return [99, 88]
class AdminSite(object):
def __init__(self):
self._registry = {}
def register(self, k, v):
self._registry[k] = v(k)
site = AdminSite()
site.register(UserInfo, StarkConfig)
site.register(Department, RoleConfig)
for k, row in site._registry.items():
print(row.run())
# -- > 同第5題
# ---------------------------------------------------------------------------------#
# 7. 背寫你瞭解的所有特殊方法並附示例;
# ---------------------------------------------------------------------------------#
# 8. 看程式碼寫結果;
class StarkConfig(object):
def __init__(self, num):
self.num = num
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(self.num)
v1 = StarkConfig(1)
v2 = RoleConfig(11)
v1() #--> 1 , 11
v2() # --> 11
# ---------------------------------------------------------------------------------#
# 9. 看程式碼寫結果;
class StarkConfig(object):
def __init__(self, num):
self.num = num
def run(self):
self()
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(345)
v1 = StarkConfig(1)
v2 = RoleConfig(11)
print(v1.run()) # 1
print(v2.run()) # 11 345
# ---------------------------------------------------------------------------------#
# 10. 看程式碼寫結果。
class StarkConfig(object):
def __init__(self, num):
self.num = num
def run(self):
self()
def __call__(self, *args, **kwargs):
print(self.num)
class RoleConfig(StarkConfig):
def __call__(self, *args, **kwargs):
print(345)
def __getitem__(self, item): #wupeiqi
return self.num[item]
v1 = RoleConfig('alex')
v2 = StarkConfig("wupeiqi")
print(v1[1]) #--> 345 alex
print(v2[2]) #--> none 或者error
# ---------------------------------------------------------------------------------#
# 11. (升級題)
'''
1.完成學生選課系統
學生選課
學生:
資訊:學號,姓名,住址選的課程列表,
功能:
檢視:檢視該學生所有課程資訊
新增課程:把選好的課程新增到課程列表中(傳參)
課程:
資訊: 課程編號,課程名稱,老師
功能:
檢視:檢視該課程的全部資訊
設定老師:給當前課程設定一個老師.(傳參)
老師:
資訊:老師編號,老師名稱,老師電話
功能:無
請完成以上三個類.建立6個課程,6個老師.給課程安排好老師.然後建立30個學生
每個學生隨機被分配3個課程.最終顯示出這三十個學生的選課情況以及任課老師的電話
'''
class student:
def __init__(self,nom,name,adress):
pass
def skill(self,read,add_lesson):
pass
class lesson:
def __init__(self,nom,leson_name,teacher):
pass
class Teacher:
def __init__(self,nom,name,phone):
pass