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()
问题:不知道为啥会报这样的错,我反复核实没有格式的错误啊,求指导!老师
老师,您好,PPT上写保存为文件执行时,解释器会做一部分优化,范围是(-5,任意正整数),我试了,把a和b都在这个范围内,但是我去的是小数,(浮点数),运行结果还是True,那为什么要强调任意整数呢?小数也OK。
老师请问,在.py文件里运行io,怎么输出修改后的字符串呢?
还有我直接打印b也都是返回<_io.StringIO object at 0x035F6A30>。
>>> a = "gedaqiwei" >>> import io >>> b = io.StringIO(a) >>> b.getvalue() 'gedaqiwei' >>> b.seek(7) 7 >>> b.write("E") 1 >>> b.getvalue() 'gedaqiwEi' >>> b <_io.StringIO object at 0x035F6A30> >>> print(b) <_io.StringIO object at 0x035F6A30> >>>
f = "我是{0},我的存款有{1.2f}" f.format("崔",6244.2212) print(f) 为什么运行不了啊
upper()是转换字符串大小写的意思吗?
s.upper()=='Q':
是指s这个变量里面如果获取到的是字符q或者Q都将他们转换一下大小写吗?还是说小写的q转换大写的就保留呢?
老师,如果在get语法中,输出两个键,为什么运行后只取到一个键值呢
老师,为什么这两个人f1()的结果不一样
里面这个1表示什么呢
老师 这个 不懂欸
老师如果我要让这个id属性每创建一个对象id加一要咋整????????
class Employee: def __init__(self,name,salary,id=1000): self.name = name self.id =id self.__salary = salary print("init") self.id += 1
a = 20 b = 20 c = 40 print(id(a),id(b),id(c)) print(a is b) print(a == b)
老师,a和b是我定义的不同的对象吧?为什么地址还是一样的?
为什么红色圈圈{:*>8}冒号前没有加1.蓝色圈圈{1:*^8}冒号前需要加1?两者有什么区别吗?
已经明白了,老师,不用回了,谢谢老师,一个小bug我没发现到,少了个括号
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637