python面向对象

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值