物件導向小練習2

2022-06-30 11:09:07 字數 4201 閱讀 5603

# 1.類或物件是否能做字典的key

# 2.簡述python的私有成員是如何實現的

# 3.私有成員能在類的外部使用麼?能在子類中使用麼?

# 4.讀程式寫結果.(不執行)

'''class starkconfig(object):

def __init__(self,num):

self.num = num

def changelist(self,request):

print(self.num,request)

class roleconfig(starkconfig):

def changelist(self,request):

print('666')

'''#建立了乙個列表,列表中有三個物件(例項)

# config_obj_list = [starkconfig(1),starkconfig(2),roleconfig(3)]

# for item in config_obj_list:

# print(item.num)

# 5.讀程式寫結果.(不執行)

print("<===5===>")

'''class starkconfig(object):

def __init__(self,num):

self.num = num

def changelist(self,request):

print(self.num,request)

class roleconfig(starkconfig):

pass

'''#建立了乙個列表,列表中有三個物件(例項)

# config_obj_list = [starkconfig(1),starkconfig(2),roleconfig(3)]

# for item in config_obj_list:

# item.changelist(168)

# print(config_obj_list[0].num)

# 6.讀程式寫結果.(不執行)

'''class starkconfig(object):

def __init__(self,num):

self.num = num

def changelist(self,request):

print(self.num,request)

class roleconfig(starkconfig):

def changelist(self,request):

print(666,self.num)

config_obj_list = [starkconfig(1),starkconfig(2),roleconfig(3)]

for item in config_obj_list:

item.changelist(168)

'''# 7.讀程式寫結果.(不執行)

'''print("<====>")

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)

config_obj_list = [starkconfig(1),starkconfig(2),roleconfig(3)]

config_obj_list[1].run()

config_obj_list[2].run()

'''# 8.讀程式寫結果.(不執行)

'''print("<==8==>")

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):

self._registry[k] = v

site = adminsite()

print(len(site._registry))

site.register('range',666)

site.register('shilei',438)

print(len(site._registry))

site.register('lyd',starkconfig(19))

site.register('yjl',starkconfig(20))

site.register('fgz',roleconfig(33))

print(len(site._registry))

print(site._registry)

'''# 9.讀程式寫結果.(不執行)

print("<==9==>")

class starkconfig():

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():

def __init__(self):

self._registry = {}

def register(self,k,v):

self._registry[k] = v

site = adminsite()

site.register('lyd',starkconfig(19))

site.register('yjl',starkconfig(20))

site.register('fgz',roleconfig(33))

# print(len(site._registry))

for k,row in site._registry.items():

row.changelist(5)

for k,row in site._registry.items():

row.run()

# 10.讀程式寫結果.(不執行)

print("<==10==>")

class justcounter:

__secretcount = 0

def count(self):

self.__secretcount += 1

print(self.__secretcount)

def count3():

print(justcounter.__secretcount)

class bars(justcounter):

def count2(self):

print(self.__secretcount)

# 情況一

# counter1 = justcounter()

# counter1.count()

# counter1.count()

# 情況二

# counter2 = bars()

# counter2.count()

# counter2.count()

# 情況三

# justcounter.count3()

物件導向小練習

練習1 編寫乙個學生類,產生一堆學生物件 要求 有乙個計數器 屬性 統計總共例項了多少個物件 class student school luffycity 類的資料屬性 類變數 count 0 def init self,age,name 獨有的特徵 self.age age self.name n...

C 物件導向餘額寶小練習

100 100 分數 題目描述 請實現乙個 魚額寶 類,下面已給出 模板,請根據main函式中對該類的操作,補充類實現部分完成 魚額寶 類可以記錄賬戶餘額 存錢 取錢 計算利息。該類中有乙個私有靜態成員變數profitrate儲存 魚額寶 的利率,可以用共有靜態成員函式setprofitrate修改...

物件導向練習

定義boat和car兩個類,兩者都有私有成員weight屬性,定義兩者的乙個友員函式totalweight 計算兩個類的物件的重量和。請根據給定的main函式和totalweight 函式的定義,完善boat和car兩個類。友元函式的使用 include using namespace std cl...