class Student: company="sxt" def __init__(self,name): self.name=name @classmethod def printdel(cls): print(cls.company) @staticmethod def add(a,b): print(a+b) return a+b Student.printdel()
老师,为什么我这个代码运行后会报错:
Traceback (most recent call last):
File "D:\python exec\py.01\py.面向对象\mypy05.py", line 16, in <module>
Student.printdel()
AttributeError: type object 'Student' has no attribute 'printdel'
class CarFactory: __obj = None __init_flag = True def __new__(cls, *args, **kwargs): if cls.__obj == None: cls.__obj = object return cls.__obj def __init__(self): if CarFactory.__init_flag: CarFactory.__init_flag = False def createCar(self,brand): if brand=="奔驰": return Benz() elif brand=="宝马": return BMW() elif brand=="比亚迪": return BYD() else: return("未知品牌,无法创建") class Benz: pass class BMW: pass class BYD: pass factory=CarFactory() factory2=CarFactory() print(id(factory)) print(id(factory2)) c1=factory.createCar("奔驰") c2=factory.createCar("宝马") print(c1) print(c2)
高洪老师在演示中没有报错,为什么我的报错了,说type object 'object' has no attribute 'createCar'
子类重写父类的方法是不改变原父类方法,只在重写的子类生效吗?还是原父类的方法也被更改了?
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)#这里为什么用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)
老师,改写add方法中,return中第二个参数为什么用other.name?
class A: pass a=A() print(a)#打印<__main__.A object at 0x0000017BD217CE80>
老师,这直接打印实例对象,这个结果是什么东西,a的类型是A类,没有返回值,打印不应该是None么
老师我没理解这个第二行的m什么意思,为啥我只写m就变成这样了
老师,这个结果怎么让它用空格隔开?怎么换行得出结果?我在format后面输入用横向制表符的形式也是如图中用","隔开
同心圆的颜色还有其他方法做吗,视频里这个不太能理解
def f1(): print("f1") f2() def f2(): print("f2") f1()
请问老师,这部分逻辑是怎么运行的啊。为什么最后生成的是f1
f2
而不是
f1
这里我引用的是同一个对象is not 判断怎么是False,不理解
class Car: def __call__(self,age,money): print("call方法") print("车龄{0},金额{1}".format(age,money)) c=Car() c(3,200000)
老师,第四行中,format格式内,age和money为什么不写成self.age和self.money
老师,我有点迷糊,在上面的推导式里面引用了x,为什么下面还用可以x变量gnt?
老师,您好我的代码运行turtle,画布总是一闪而过,
e={x for x in range(2,100) if x%5==0} print(e)
老师说集合推导式打印出来是无序的,为什么我打印出来看起来是有顺序的
老师,请问 reverse=True 是起到什么作用,可以没有吗?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637