欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[python]为什么父类的值没有改变 Python 

程序员文章站 2022-05-10 21:49:14
...
在调式中已经发现第二次初始化worker的时候,worker的name已经为lisi了,为什么打印结果仍然是111,新手望拍砖指导。

父类
class worker(object):
    '''
    classdocs
    '''
    name="111"
    salary=""

    def __init__(self,name,salary):
        self.name=name
        self.salary=salary
        #print self.name,self.salary
        
    def work(self):
        print "I'm working"
        


子类
from worker import worker
class Tester(worker):
    '''
    classdocs
    '''
    name=""
    salary=""
    sex=""

    def __init__(self,name,salary,sex):
        worker(name,salary)
        self.name=name
        self.salary=salary
        self.sex=sex
        
    def boring(self):
        worker.work(self)
        print self.name,self.salary,self.sex
        



调用类:
from com.dianxinos.model.worker import worker
from com.dianxinos.model.Tester import Tester
class Hello:
    def __init__(self):
        #reload()
        oneworker= worker("zhangsan","5000")
        oneworker.work()
        print oneworker.name,oneworker.salary
    def test(self):
        tester=Tester("lisi","10000","man")
        tester.work()
        #tester.boring()
        print tester.name,tester.salary,tester.sex
        print super(Tester, tester).name
   
        
if __name__ == "__main__"  :
    h=Hello()
    h.test()


打印结果:
I'm working
zhangsan 5000
I'm working
lisi 10000 man
111
111 
相关标签: Python