老师请问,第三周以后的课程安排从哪里下载,我看就到第三周没了
老师,制表符怎么敲出来?就是让他直接空4个格的
单例模式讲的太浅了,,没太明白,比如a这个实例的时候,是调用object 这个根类的__new__方法吗?那b调用的时候类属性__obj还是空吗,如果还是空,那么不应该在创建一个新的对象吗,内存地址怎么是一样呢。
老师,请问这里a怎么解释,实质搞不懂。也看了相关同学提的相同的问题,但还是没有理解。
请问
python什么时候要缩进什么时候不用?
"""定义发动机类 Motor、底盘类 Chassis、座椅类 Seat,车辆外壳类 Shell,并使用组合 关系定义汽车类。其他要求如下: 定义汽车的 run()方法,里面需要调用 Motor 类的 work()方法,也需要调用座椅 类 Seat 的 work()方法,也需要调用底盘类 Chassis 的 work()方法。""" class Montor: def work(self): print("发动机") class Chassis: def work(self): print("底盘") class Seat: def work(self): print("座椅") class Shell: def work(self): print("车辆外壳") #定义Car类里的run方法能调用Motor, Chassis, Seat的work()方法 class Car: def __init__(self,Montor,Chassis,Seat,Shell): self.Montor = Montor self.Chassis = Chassis self.Seat = Seat self.Shell = Shell # 定义run()方法 def run(self): self.Montor.work() self.Chassis.work() self.Seat.work() self.Shell.work() print("该{0}由{1},{2},{3}组成".format(self.Montor.work(),self.Chassis.work(),self.Seat.work(),self.Shell.work())) m = Montor() c = Chassis() se = Seat() sh = Shell() car1 = Car(m, c, se, sh) car1.run()
'''使用工厂模式、单例模式实现如下需求: (1) 电脑工厂类 ComputerFactory 用于生产电脑 Computer。工厂类使用单例模式,也就是说只能有一个工厂对象。 (2) 工厂类中可以生产各种品牌的电脑:联想、华硕、神舟 (3) 各种品牌的电脑使用继承实现: (4) 父类是 Computer 类,定义了 calculate 方法 (5) 各品牌电脑类需要重写父类的 calculate''' 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 __init__(self): # 单例模式初始化,只初始化一次 if ComputerFactory.__init_flag: print("模具初始化完成单例模式初始化,只初始化一次") ComputerFactory.__init_flag = False def creat_computer(self, brand): # 工厂模式 if brand == "联想": return Lenovo() # 调用Lenovo类,需要传参 elif brand == "华硕": return AUAS() elif brand == "神州": return HASEE() else: return print("未知品牌,无法创建!") class Computer: # 定义父类Computer def __init__(self, name): self.name = name def calculate(self): # 定义父类Computer的calculate方法 print("生产该电脑") class Lenovo(Computer): # 定义Levono类,继承父类Computer def __init__(self, name): # 显式继承父类的构造函数 Computer.__init__(self, name) def calculate(self): # 重写calculate方法 print('生产{0}'.format(self.name)) class AUAS(Computer): # 定义AUAS类,继承父类Computer # 当子类与父类构造函数相同,默认继承 def calculate(self): print('生产{0}'.format(self.name)) class HASEE(Computer): # 定义HASEE类,继承父类Computer def calculate(self): print('生产{0}'.format(self.name)) # 子类AUAS类运行测试1 s1 = HASEE("小米") s1.calculate() print("--------------------------------------------------------") # 组合模式运行测试2 c1 = ComputerFactory() x1 = c1.creat_computer("华硕") x2 = c1.creat_computer("神州") x3 = c1.creat_computer("联想") print(x1) print(x2) print(x3) print("--------------------------------------------------------") c2 = ComputerFactory() print(c1) print(c2)
运行报错是一个原因,第二是
s1 = HASEE("小米")
这行代码,给神舟传小米为什么海内输出结果;我对单例模式还有工厂模式看了好几遍。还是不懂
老师,为什么当所有的bucket的大小和结构相等时,才能通过偏移量来读取bucket
老师,关于图中画红线部分,课程里面是class B(A):,而我这边输入class B:也能正常输出结果,A类也不是B类的父类,为什么可以引用A呢?
老师,在编码中有时候参数和参数之间用逗号,但又有时候用的是小数点,也有有的参数或者函数后面不加括号
但又有的时候是加括号的,所以请问老师,这些符号在什么样的情况下需要加,什么样的情况下不需要加?
老师:是否可以帮我解释一下,整个m是如何继承Man这个类的,感觉是突然跳过去的,当中的逻辑不是很清楚
turtle MyRectangle: (x = y = width = height = ): .x = x .y = y .width = width .height = height (): area = .width * .height (area) (): perimeter = * (.width + .height) (perimeter) (): turtle.penup() turtle.goto(.x.y) turtle.pendown() turtle.goto(.x.y - .height) turtle.goto(.x + .width.y - (.height)) turtle.goto(.x + .width.y) turtle.goto(.x.y) turtle.done() t = MyRectangle(-) (t.getArea()) (t.getPerimeter()) t.draw()
结果为啥有两个空值
turtle turtle.color() turtle.width() turtle.circle() turtle.penup() turtle.goto(,) turtle.down() turtle.color() turtle.circle() turtle.penup() turtle.goto(,) turtle.down() turtle.color() turtle.circle() turtle.penup() turtle.goto(,-) turtle.down() turtle.color() turtle.circle() turtle.penup() turtle.goto(,-) turtle.down() turtle.color() turtle.circle()
def __new__(cls, *args, **kwargs): if cls.__obj == None: cls.__obj = object.__new__(cls) return cls.__obj
老师,这里 __new__调用一次之后是不是cls.__obj的值已经为非None了?
def __add__(self, other): if isinstance(other,Person): return '{0}--{1}'.format(self.name,other.name) p1 = Person("高淇") p2 = Person("高希希") x=p1+p2 print(x)
老师:
在计算p1+p2的时候,是否可以这样考虑:首先p1已经存在,进行+号运算,调用__add__()方法,判断+号后面的p2是否属于othername?如果是返回--{1}即--高希希。此时{0}是什么?是+号前面的p1高淇吗?
如果不是,该怎么理解return "{0}--{1}".format(self.name,other.name)这句话
return
"{0}--{1}"
.
format
(
self
.name,other.name)这句话
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637