class ComputerFactory: __obj = None __init_flag = True def __new__(cls, *args, **kwargs): if cls.__obj == None: cls.__obj == object.__new__(cls) return cls.__obj def creat_pc(self,brand): if brand == "联想": print("造了一个联想电脑") return Lianxiang() elif brand == "华硕": print("造了一个华硕电脑") return Huashuo() elif brand == "神州": print("造了一个神州电脑") return Shenzhou() else : return "请输入正确的电脑品牌" def __init__(self): if ComputerFactory.__init_flag: print("初始化代码执行了一次") ComputerFactory.__init_flag = False class Computer: def calcute(self): print("我是电脑父类的calcute方法...") class Huashuo(Computer): def calcute(self): print("我是华硕的calcute方法...") class Lianxiang(Computer): def calcute(self): print("我是联想的calcute方法...") class Shenzhou(Computer): def calcute(self): print("我是神州的calcute方法...") fac = ComputerFactory() lianxiang1 = fac.creat_pc("联想") lianxiang1.calcute()
老师 能帮忙看下这个错是怎么回事吗:
lianxiang1 = fac.creat_pc("联想")
AttributeError: 'NoneType' object has no attribute 'creat_pc'
老师为什么我的多分枝结构语句分数输出不出来??
class A: def say(self): print("A",self ) class B(A): def say(self): #A.say(self) super().say() print("B",self) B().say() 老师,调用子类方法时,必须在子类名后加()是吗? 就像 B().say() 一样
一个对象只有一个类型还是可以有多个类型?
代码:
import turtle as t class MyRectangle: def __init__(self,x=0,y=0,width=100,height=100): self.x=x self.y=y self.width=width self.height=height def getArea(self): return self.width*self.height def getPerimeter(self): return 2*(self.width+self.height) def draw(self): t.penup() t.goto(self.x,self.y) t.pendown() t.goto(self.x,self.y-self.height) t.goto(self.x+self.width,self.y-(self.height)) t.goto(self.x+self.width,self.y) t.goto(self.x,self.y) t.done() yida=MyRectangle(-50,-50,200,280) print(yida.getArea()) print(yida.getPerimeter()) yida.draw()
老师,先用draw()方法以后就打印不出来面积和周长了,这是为什么? 必须 getArea ,getPerimeter方法放在draw方法前面才能打印出来,这是因为turtle.done()的某些机制吗? 求老师帮忙
为什么会出现这种错误? 我换成到1的话就打印出来
第一张图
第二张图
老师我不明白为什么,根据代码走完 第二张图的代码 没有返回 def test01(n)的条件啊。不明白!!!
我现在用的是试用期30天,但是我想用破解版一劳永逸,这个破解版现在在能30天的基础上安装?还是在30天期满之后才能安装上?
老师 nonlocal b为什么要在pinrt("inner b:",b)上面,在下面就报错是怎么回事
#测试运算符的重载 class Person: def __init__(self,name): self.name=name def __add__(self,other): if isinstance(other,Person): return "{0}--{1}".format(self.name,other.name) else: return "不是同类,不能相加" def __mul__(self,other): if isinstance(other,int): return self.name*other else: return "不是同类,不能相乘" p1=Person('高琪') p2=Person("高希希") x=p1+p2 print(x) print(p1*3)
老师
if isinstance(other,Person):和if isinstance(other,int):这两句代码不明白。int是什么?
class Animal: def shout(self): print("动物叫了一声") class Dog(Animal): def shout(self): print("小狗,汪汪汪") class Cat(Animal): def shout(self): print("小猫,喵喵喵") def animalShout(a): if isinstance(a,Animal): a.shout() #传入的对象不同,shout 方法对应的实际行为也不同。 animalShout(Dog()) animalShout(Cat()) animalShout(Animal()) 老师isinstance是什么意思?
a={'expenditure_amount':300.15,'date':2018-10-18,'person':'高小七'} print(a)
为什么打印出来后日期变了? 是我写错了吗 不太明白。
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637