zoukankan      html  css  js  c++  java
  • day13-类的重写和类的私有方法

    类的重写

    在python中 有时需要进行重写,重写是继承机制中的一个重要部分, 可以重写一般方法也可以重写构造方法,构造方法是用来初始化新创建对象的状态。

    class A :  
        def hello(self):  
            print('Hello,i am A.')  
    class B(A):  
        pass  

    >>>a = A()

    >>>b =B()

    >>>a.hello()

    Hello, i am A.

    >>>b.hello()

    Hello, i am A.

    为什么会这样呢? 因为B类没有定义自己的hello方法,故当hello被调用时,原始信息就被打印出来了。

    B类也可以重写这个hello方法。

    class B(A):  
        def hello(self):  
            print('Hello,i am B.')  

    >>>b = B()

    >>>b.hello()

    Hello,i am B.

    以上的是重写一般方法!

    如果特殊的构造方法被重写的话,将会导致原始基类的方法无法被调用。

    <pre name="code" class="python">class Bird:  
        def __init__(self):  
            self.hungry = True  
        def eat(self):  
            if self.hungry:  
                print('Aaaah...')  
                self.hungry = False  
            else:  
                print('No,thanks!')  
    
    

    >>>b = Bird()

    >>>b.eat()

    Aaaah...

    >>>b.eat()

    No,thanks!

    现在为子类SongBird .

    class SongBird(Bird):  
        def __init__(self):  
            self.sound = 'Squawk!'  
        def sing(self):  
            print('self.sound')  


    >>>sb = SongBird()

    >>>sb.sing()

    Squawk!

    >>>sb.eat()

    报错,提示SongBird没有hungry特性。

    如何解决这个问题呢?有两种方法:1、调用未绑定的基类构造方法  2、使用super函数

    1调用未绑定的基类构造方法是怎么样呢? 让我们用实例来说明:

    class SongBird(Bird):  
        def __init__(self):  
            Bird.__init__(self):  
            self.sound = 'Squawk!'  
        def sing(self):  
            print('self.sound')  


    只添加一行代码Bird.__init__(self).

    >>>sb = SongBird()

    >>>sb.sing()

    Squawk!

    >>>sb.eat()

    Aaaah...

    >>>sb.eat()

    No.thanks!

    2.使用super函数


    class Bird:  
        def __init__(self):  
            self.hungry = True  
        def eat(self):  
            if self.hungry:  
                print('Aaaah...')  
                self.hungry = False  
            else:  
                print('No,thanks!')  
     
    class SongBird(Bird):  
        def __init__(self):  
            Super(SongBird,self).__init__()  
            self.sound = 'Squawk!'  
        def sing(self):  
            print('self.sound')  

    >>>sb = SongBird()

    >>>sb.sing()

    Squawk!

    >>>sb.eat()

    Aaaah...

    >>>sb.eat()

    No.thanks!

    类的私有属性和方法

    在Python中可以通过在属性变量名前加上双下划线定义属性为私有属性

    特殊变量命名

    1、 _xx 以单下划线开头的表示的是protected类型的变量。即保护类型只能允许其本身与子类进行访问。若内部变量标示,如: 当使用“from M import”时,不会将以一个下划线开头的对象引入 。

    2、 __xx 双下划线的表示的是私有类型的变量。只能允许这个类本身进行访问了,连子类也不可以用于命名一个类属性(类变量),调用时名字被改变(在类FooBar内部,__boo变成_FooBar__boo,如self._FooBar__boo)

    3、 __xx__定义的是特列方法。用户控制的命名空间内的变量或是属性,如init , __import__或是file 。只有当文档有说明时使用,不要自己定义这类变量。 (就是说这些是python内部定义的变量名)

    在这里强调说一下私有变量,python默认的成员函数和成员变量都是公开的,没有像其他类似语言的public,private等关键字修饰.但是可以在变量前面加上两个下划线"_",这样的话函数或变量就变成私有的.这是python的私有变量轧压(这个翻译好拗口),英文是(private name mangling.) **情况就是当变量被标记为私有后,在变量的前端插入类名,再类名前添加一个下划线"_",即形成了_ClassName__变量名.**

    Python内置类属性

    __dict__ : 类的属性(包含一个字典,由类的数据属性组成)

    __doc__ :类的文档字符串

    __module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod)

    __bases__ : 类的所有父类构成元素(包含了一个由所有父类组成的元组)

    实例:

     1 #!/usr/bin/python
     2 # -*- coding: UTF-8 -*-
     3 
     4 class JustCounter:
     5     __secretCount = 0  # 私有变量
     6     publicCount = 0    # 公开变量
     7 
     8     def count(self):
     9         self.__secretCount += 1
    10         self.publicCount += 1
    11         print self.__secretCount
    12 
    13 counter = JustCounter()
    14 counter.count()
    15 counter.count()
    16 print counter.publicCount
    17 print counter.__secretCount  # 报错,实例不能访问私有变量
  • 相关阅读:
    时隔 4 年!ElasticJob 3.0.0 发布,王者归来。。
    Git 代码防丢指南,再也不怕丢失代码了!
    排查线上问题必须掌握的 6 个 Linux 命令!
    Leaflet添加图片图层:ImageOverlay
    js图片数据流
    arcgis查找与要素相交的内容 by 空间连接查询
    Leaflet添加图片图层:使用Axios请求arcgis server rest api的exportmap,获取图片并进行显示
    esri-leaflet: unique value renderer唯一值渲染-dynamiclayers参数
    arcgis server缓存切片
    城市给排水法律法规
  • 原文地址:https://www.cnblogs.com/wxp997/p/7795762.html
Copyright © 2011-2022 走看看