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  # 报错,实例不能访问私有变量

 

转载于:https://www.cnblogs.com/wxp997/p/7795762.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值