python面向对象
类型不属于引用属于对象
- 类变量和实例变量
class fun: colour="white"#类变量:属于类中共享变量 def __init__(self,colour):#实例变量:每个实例对应不同的值 self.colour=colour print(colour) c1=fun("blue") c2=fun("black") print(fun.colour) fun.colour="green" print(fun.colour) print("dddddddddddddd") class fun1: colour="white"#类变量:属于类中共享变量 def __init__(self):#实例变量:没有赋值会调用类变量 print(self.colour) c1=fun1() c2=fun1() print(fun.colour) fun1.colour="green" print(fun.colour) c3=fun1() print(c3.colour)
- 继承
class father: print("father") class uncle: print("uncle") class son(father,uncle)://可以多继承 print("sss") s=son() print(s)
- 重载
- 异常处理
def fun(a,b): try: s = a / b return s except ZeroDivisionError: print("除数不能为0") except TypeError: print("类型异常") except: print("捕获异常") print(fun(3,"d")) s=fun(5,0)