• python学习笔记之---重写与重载


    重写

    重写是指子类重写父类的成员方法。子类可以改变父类方法所实现的功能, 但子类中重写的方法必须与父类中对应的方法具有相同的方法名。也就是说 要实现重写,就必须存在继承。

    #重写:子类实现父类的同名方法。

    实例1:

    class Person():
        def print_info(self):
            print("*************")
    
    
    class ChinesePerson(Person):
        def print_info(self):  #子类重写父类的print_info方法
            print("________")
    
    p= ChinesePerson() #子类实例
    p.print_info()  #子类调用重写方法
    p1=Person()  #父类实例
    p1.print_info()  #父类调用重写方法

    E:>py -3 a.py
    ________
    *************

    实例2:

    class Person():
      
        def __init__(self,name):
            self.name  = name
    
        def get_name(self):
            return self.name
    
        def set_name(self,name):
            if len(name)>5:
                return
            self.name = name
    
    class ChinesePeople(Person):
        def __init__(self,name,nation):
            Person.__init__(self,name)
            self.nation = nation
    
        def get_nation(self):
            return self.nation
    
        def set_nation(self,nation):
            self.nation = nation
    
        def set_name(self,name):
            if len(name)>10:
                return
            self.name = name
            
    p = ChinesePeople("李老师","")
    p.set_name("abcdehed1111")
    print(p.name)
    
    p.set_name("ab")
    print(p.name)

    E:>py -3 a.py
    李老师
    ab

    #如果想在子类里面调用父类的同名方法:
    class Person():
      
        def __init__(self,name):
            self.name  = name
     
        def get_name(self):
            return self.name
     
        def set_name(self,name):
            if len(name)>5:
                return
            self.name = name
     
    class ChinesePeople(Person):
        def __init__(self,name,nation):
            Person.__init__(self,name)
            self.nation = nation
     
     
        def get_nation(self):
            return self.nation
     
     
        def set_nation(self,nation):
            self.nation = nation
     
     
        def set_name(self,name):
    #子类中明确的调用父类中被重写的方法 Person.set_name(self,name)
    #写法一 #super(ChinesePeople,self).set_name(name) #写法二
    #super.set_name() #写法三
    p = ChinesePeople("吴老师","") p.set_name("abcd") print(p.name)
    E:>py -3 a.py
    abcd

     #运算符重写

    实例:

    #coding=utf-8
    class Vector(object) :
      def __init__(self, a, b) :
        self.a = a
        self.b = b
    
      def __str__(self):
        return 'Vector (%d, %d)' % (self.a, self.b)
    
      def __add__(self,other) :
        return Vector(self.a + other.a, self.b + other.b)
    
    x =  Vector(3,7)
    y =  Vector(1, -10)
    print (x + y)
    print (str(x))
    C:UsersdellDesktop练习6>py -3 0609.py
    Vector (4, -3)
    Vector (3, 7)

    重载

    重载方法的名称是相同的,但在方法的声明中一定要有彼此不相同的成
    份,以使编译器能够区分这些方法。重载的方法必须遵循下列原则:
    ➢方法的参数必须不同,包括参数的类型或个数,以此区分不同方法
    体;
    ➢方法的返回类型、修饰符可以相同,也可以不同。

    实例:两个同名的函数,传参不同

    def a(x):
        return x
     
    def a(x,y):
        return x+y
     
    print(a(1,2))
    print(a(1))
    C:UsersdellDesktop练习6>py -3 0609.py
    3
    Traceback (most recent call last):
      File "0609.py", line 8, in <module>
        print(a(1))
    TypeError: a() missing 1 required positional argument: 'y'
     
    总结:python是没有重载的,第二个同名的函数会把第一个覆盖掉
    第一因为python没有类型,第二python有可变参数
     
     
     
     
    重写:把父类的方法覆盖掉,使用子类的同名方法。
    重载:多个方法名一样,但是他们的参数类型,还有参数个数不一样(java)。
    python不支持重载,因为python可以传可变参数,也没有参数类型的定义。
  • 相关阅读:
    Java_Eclipse_Android安装
    deep_learning_Function_os.makedirs()
    deep_learning_Function_ Matplotlib 3D 绘图函数 plot_surface 的 rstride 和 cstride 参数
    deep_learning_Function_ numpy.random.randn()
    deep_learning_Function_list变量前面加星号,字典变量前面加两个星号
    deep_learning_Function_ lambda函数详解
    Unity 3D中C#的性能优化小陷阱
    1.Bacula各组件说明
    vmware修改虚拟机名称
    AWS IoT Greengrass 入门-模块3(第 1 部分):AWS IoT Greengrass 上的 Lambda 函数
  • 原文地址:https://www.cnblogs.com/wenm1128/p/11733901.html
Copyright © 2020-2023  润新知