博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day13-类的重写和类的私有方法
阅读量:7043 次
发布时间:2019-06-28

本文共 3060 字,大约阅读时间需要 10 分钟。

类的重写

在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.

以上的是重写一般方法!

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

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 += 110         self.publicCount += 111         print self.__secretCount12 13 counter = JustCounter()14 counter.count()15 counter.count()16 print counter.publicCount17 print counter.__secretCount  # 报错,实例不能访问私有变量

 

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

你可能感兴趣的文章
js刷新页面的方法
查看>>
Vue-入门
查看>>
delegate 和 block 的区别
查看>>
网格贴图合并
查看>>
如何读/写论文???
查看>>
Code Kata:大整数四则运算—乘法 javascript实现
查看>>
python学习-Pillow图像处理
查看>>
ThreadLocal
查看>>
学习myBatis - 如何配置myBatis
查看>>
LOJ #6053. 简单的函数
查看>>
[PA2014]Druzyny
查看>>
MapReduce - reduce function problem
查看>>
mysql 获取 汉字字段首字母(转)
查看>>
最佳加法表达式
查看>>
springboot profile 日志配置
查看>>
C++typedef的详细用法
查看>>
UML学习总结(2)——StartUML 各种类图的例子
查看>>
Spring MVC学习总结(2)——Spring MVC常用注解说明
查看>>
Java加密算法(一)——BASE64与单向加密算法MD5&SHA&MAC
查看>>
Django REST framework 自定义(认证、权限、访问频率)组件
查看>>