会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第一阶段:Python入门/Python入门(动画版) 4456楼
Python 全系列/第一阶段:Python入门/面向对象 4462楼

class ComputerFactory:
    __obj = None
    __init_flag = True
    __countLenovo = 0
    __countAsus = 0
    __countHasee = 0
    def __new__(cls, *args, **kwargs):  #单例模式
        if cls.__obj==None:
            cls.__obj = object.__new__(cls)
        return cls.__obj  #new方法创建完成一定要返回我们的实例对象
    def __init__(self):   #单例模式
        if  ComputerFactory.__init_flag:
            print('构造初始化')
            ComputerFactory.__init_flag =  False
    def produce(self,brand):  #工厂模式
        if brand =='联想':
            ComputerFactory.__countLenovo += 1
            return Lenovo()
        elif brand == '华硕':
            ComputerFactory.__countAsus += 1
            return Asus()
        elif brand == '神舟':
            ComputerFactory.__countHasee += 1
            return Hasee()
    def calculate(self):
        a = [ComputerFactory.__countAsus,ComputerFactory.__countLenovo,ComputerFactory.__countHasee]
        print('本公司一共生产了{0}台电脑'.format(sum(a))
class Computer:
    __count = 0
    def __init__(self):
        Computer.__count +=1
    def calculate(self):
        print('生产了{0}台电脑'.format(Computer.__count))
class Lenovo(Computer):
    __num = 0
    def __init__(self):
        # Computer.__init__(self)
        Lenovo.__num +=1
    def calculate(self):
        print('共生产了{0}个联想电脑'.format(Lenovo.__num-1))
class Asus(Computer):
    __num = 0
    def __init__(self):
        # Computer.__init__(self)
        Asus.__num += 1
    def calculate(self):
        print('共生产了{0}个联想电脑'.format(Asus.__num-1))
class Hasee(Computer):
    __num = 0
    def __init__(self):
        # Computer.__init__(self)
        Hasee.__num += 1
    def calculate(self):
        print('共生产了{0}个联想电脑'.format(Hasee.__num-1))
t = ComputerFactory()
h = Computer()
t.produce('联想')
h.calculate()
Lenovo().calculate()
t.calculate()
Lenovo().calculate()


问题:不知道为啥会报这样的错,我反复核实没有格式的错误啊,求指导!老师

blob.png


Python 全系列/第一阶段:Python入门/面向对象 4463楼
Python 全系列/第一阶段:Python入门/控制语句 4464楼
Python 全系列/第一阶段:Python入门/函数和内存分析 4468楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637