|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Worker:
def info(self):
class Wm:
__maner = []
def add_w(self,someb):
Wm.__maner.append(someb)
# print(Wm.maner)
def show(self,n):
if isinstance(Wm.__maner[n-1],Pr):
print("程序员{0}底薪是{1}".format(Wm.__maner[n-1].nam,Wm.__maner[n-1].bs))
elif isinstance(Wm.__maner[n-1],Se):
print("销售{0}员底薪是{1}".format(Wm.__maner[n-1].nam,Wm.__maner[n-1].bs))
class Pr(Worker):
def __init__(self,nam,bs,pb):
self.nam = nam
self.bs = bs
self.pb = pb
def info(self):
return [self.nam ,self.bs,self.pb,self.pb+self.bs]
class Se(Worker):
def __init__(self,nam,bs):
self.nam = nam
self.bs = bs
def info(self):
return [self.nam ,self.bs,self.bs*1.05]
p1=Pr("sw",4000,6000)
Wm.add_w(2,p1)
Wm.show(1,1)
s1=Se("hy",6000)
Wm.add_w(2,s1)
Wm.show(1,2)
新手请教:
1.为什么Wm.add_w(2,p1)里面一定要有个多余的参数,要是写成Wm.add_w(p1) 的话就会出现这样的提示 TypeError: add_w() missing 1 required positional argument: 'someb'
2.Wm.show(1,2)也是同样的问题,要是少了第一个参数写成Wm.show(1) 的话就会出现这样的提示TypeError: show() missing 1 required positional argument: 'n'
写成下面的就没有问题,但是我看不出来上面的写法和下面的写法有什么实质差异,实在是弄不明白啊,求指点迷津!!
class WoMa:
def __init__(self):
self.__graphics = []
def add_w(self, graphic):
# if isinstance(graphic, Graphic):
self.__graphics.append(graphic)
# else:
# raise ValueError()
# print(self.__graphics)
def get_s(self,n):
if isinstance(self.__graphics[n], Pr):
return print("程序员",self.__graphics[n].name,"底薪是",self.__graphics[n].bs,"项目分红是",self.__graphics[n].pb,"的总薪水是",self.__graphics[n].bs+self.__graphics[n].pb)
elif isinstance(self.__graphics[n], Se):
return ["销售员",self.__graphics[n].name,"底薪是",self.__graphics[n].bs,"总薪水是",self.__graphics[n].bs*1.05]
class Wo:
def info(self):
# 如果子类不重写,则异常.
raise NotImplementedError()
#-----------------------------------
class Pr(Wo):
def __init__(self,name,bs,pb):
self.name = name
self.bs = bs
self.pb = pb
def info(self):
# print([self.name,self.bs,self.pb,self.bs+self.pb])
return [self.name,self.bs,self.pb,self.bs+self.pb]
class Se(Wo):
def __init__(self,name,bs):
self.name = name
self.bs = bs
def info(self):
# print([self.name,self.bs,self.bs*1.05])
return [self.name,self.bs,self.bs*1.05]
c01 = Pr("qs",4000,5000)
manager = WoMa()
manager.add_w(c01)
re = manager.get_s(0)
print(re)
本帖最后由 Stubborn 于 2022-1-12 12:21 编辑
如果你想静态调用,可以在函数加上staticmethod装饰器 @staticmethod
def add_w(someb):
# Wvm = Wm() 可以不实例化对象,直接使用
p1=Pr("sw",4000,6000)
Wm.add_w(p1)
如果你想正常调用,请先实例化对象,在调用
# 没有staticmethod静态装饰器,必须先实例化Wm对象,用实例化对象进行调用function
Wvm = Wm()
p1=Pr("sw",4000,6000)
Wvm.add_w(p1)
|
|