Python高级培训第二次作业

import operator
class Cat(object):#创建类cat 继承与object
    def __init__(self,leg):#设置__init__就上一个参数leg
        self.leg = leg#设置leg的值为leg


    def run(self):#设置方法 获得奔跑方法
        print("cat have no way to run")#打印cat have no way to run


class Tiger(Cat):#创建类Tiger 继承与Cat
    def __init__(self,leg,name):#设置__init__有两个参数leg,name
        self.name=name#设置name的值为name
        Cat.__init__(self,leg)#重新使用下Cat的__init__()

    def run(self):#重写奔跑方法
        super(Tiger, self).run()#重写使用Cat的run()
        print("tiger konw how to run")#打印"tiger konw how to run"
        #print(self.leg)

    def __pow__(self, power, modulo=None):#重写pow方法 返还pow(self.leg,power)
        a=int(self.leg)
        #print(type(power))
        for i in range(power):
            a=a*power
        return a
    def __lt__(self, other):#重写__lt__ 比较大小
        if self.leg>other.leg:
            return True
        else:
            return False

#c1 = Cat(4)
c2=Tiger(leg=4,name="tiger")#实例化Tiger为变量c2 leg=4,name="tiger"
c2.run()#使用c2的run方法
print(pow(c2,4))#打印出pow(c2,4)
c3=Tiger(leg=5,name="tiger2")#实例化Tiger为变量c3 leg=5,name="tiger2"
print(operator.lt(c3, c2))#打印出lt(c3, c2) 的结果

property()

property(fget=None,fset=None,fdel1=None,doc=None)          property 封装

property(获得属性的方法,设置属性的方法,删除属性的方法)

class Myclass():

def __init__(self,attr = 10):

self.myattr = attr

def getoneattr(self):

return self.myattr

def setoneattr(self,attr):

self.myattr = attr

def deloneattr(self):

del self.attr

x=property(getoneattr,setoneattr,deloneattr)

c1 = Myclass()

c1.getoneattr()#--------------------------------<=10

c1.x#---------------------------------------------<=10

c1.x=20

c1.x#----------------------------------------------<=20

del c1.x

c1.myattr#------------------------------------------<=报错

 super()

super([子class],self).方法([,attr])#不能加self

Python中子类可以覆盖父类的方法

要想重新调用被覆盖的父类的方法可以用super()调用父类的方法

super()在改变子类的魔法函数时显得尤其的重要

后加:

def __init__(self):

super(base_class_name,self).__init__()

self.wuhu=1

可以既继承父类 又可以设计子类的__init__ 这里子类的self.wuhu被创建了

当然不用非要用__init__

例如

class Father():

    def __init__(self):

        print('父类')

        self.wuhu1=1

    def fun1(self,x=1):

        self.wuhu2=self.wuhu1+x

        print('wuhu2=',self.wuhu2)

        

class Child(Father):

    def __init__(self):

        print('子类')

        super(Child,self).__init__()

    def fun1(self):

        super(Child,self).fun1()

        self.wuhu3=self.wuhu2+self.wuhu1

c1=Child()

c1.fun1()

print(c1.wuhu3)

结果为:

子类

父类

wuhu2= 2

3

class Father():

def __init__(self):

print('父类被调用')

class Child(Father):

def __init__(self):
print('子类被调用')

super(Child,self).__init__()#------------------<=这里不能加self

c1 = Child()#----------------------------------------<=父类被调用

子类被调用

改写魔法方法:

例(add):

class Model:
    def __init__(self,x):
        self.x=x

    def __add__(self, other):
        return  Model(self.x+other.x)
    def __str__(self):
        return ("两个对象相加的值是{x}".format(x=self.x))

a=Model(5)
b=Model(7)
print(a+b)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值