python类继承用法实例分析


Posted in Python onMay 27, 2015

本文实例讲述了python类继承用法。分享给大家供大家参考。具体如下:

help('object') # test
class Class1(object):
  """
  Class1 inherits the most basic container class object (just a place holder)
  this is the newer class writing convention, adding (object) is "still" optional
  """
  k = 7
  def __init__(self, color='green'):
    """
    Special method __init__() is called first (acts as Constructor).
    It brings in data from outside the class like the variable color.
    (in this case color is also set to a default value of green)
    The first parameter of any method/function in the class is always self,
    the name self is used by convention. Assigning color to self.color allows it
    to be passed to all methods within the class. Think of self as a carrier,
    or if you want impress folks call it target instance object.
    The variable k is assigned a value in the class, but outside of the methods.
    You can access k in a method using self.k
    """
    self.color = color
  def Hello1(self):
    print "Hello from Class1!"
  def printColor(self):
    """in this case self allows color to be passed"""
    print "I like the color", self.color
  def __localHello(self):
    """
    A variable or function with a double underline prefix and no or max. single
    underline postfix is considered private to the class and is not inherited or
    accessible outside the class.
    """
    print "A hardy Hello only used within the class!"
 
class Class2(Class1):
  """
  Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)
  Class1 has to be coded before Class2 for this to work!!!
  Class2 can now use any method of Class1, and even the variable k
  """
  def Hello2(self):
    print "Hello from Class2!"
    print self.k, "is my favorite number"
   
# the color blue is passed to __init__()
c1 = Class1('blue')
# Class2 inherited method __init__() from Class1
# if you used c2 = Class2(), the default color green would be picked
c2 = Class2('red')
print '-'*20
print "Class1 says hello:"
c1.Hello1()
print '-'*20
print "Class2 says a Class1 hello:"
c2.Hello1()
print '-'*20
print "Class2 says its own hello:"
c2.Hello2()
print '-'*20
print "Class1 color via __init__():"
c1.printColor()
print '-'*20
print "Class2 color via inherited __init__() and printColor():"
c2.printColor()
print '-'*20
print "Class1 changes its mind about the color:"
c1 = Class1('yellow') # same as: c1.__init__('yellow')
c1.printColor()
print '-'*20
print "Wonder what Class2 has to say now:"
c2.printColor()
print '-'*20
# this would give an error! Class1 does not have a method Hello2()
if hasattr(Class1, "Hello2"):
  print c1.Hello2()
else:
  print "Class1 does not contain method Hello2()"
# check inheritance
if issubclass(Class2, Class1):
  print "Class2 is a subclass of Class1, or Class2 has inherited Class1"
# you can access variable k contained in Class1
print "Variable k from Class1 =", c1.k
print '-'*20
# this would give an error! You cannot access a class private method
if hasattr(Class1, "__localHello()"):
  print c1.__localHello()
else:
  print "No access to Class1 private method __localHello()"

运行结果如下:

Help on class object in module __builtin__:

class object
 | The most base type

--------------------
Class1 says hello:
Hello from Class1!
--------------------
Class2 says a Class1 hello:
Hello from Class1!
--------------------
Class2 says its own hello:
Hello from Class2!
7 is my favorite number
--------------------
Class1 color via __init__():
I like the color blue
--------------------
Class2 color via inherited __init__() and printColor():
I like the color red
--------------------
Class1 changes its mind about the color:
I like the color yellow
--------------------
Wonder what Class2 has to say now:
I like the color red
--------------------
Class1 does not contain method Hello2()
Class2 is a subclass of Class1, or Class2 has inherited Class1
Variable k from Class1 = 7
--------------------
No access to Class1 private method __localHello()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python中文乱码的解决方法
Nov 04 Python
Python bsddb模块操作Berkeley DB数据库介绍
Apr 08 Python
Python3搜索及替换文件中文本的方法
May 22 Python
python中OrderedDict的使用方法详解
May 05 Python
用Python实现KNN分类算法
Dec 22 Python
numpy.linspace函数具体使用详解
May 27 Python
使用Python检测文章抄袭及去重算法原理解析
Jun 14 Python
PyQt5通信机制 信号与槽详解
Aug 07 Python
python爬虫之爬取百度音乐的实现方法
Aug 24 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
May 10 Python
解决Django Haystack全文检索为空的问题
May 19 Python
python 安全地删除列表元素的方法
Mar 16 Python
python显示生日是星期几的方法
May 27 #Python
python中zip和unzip数据的方法
May 27 #Python
Python pickle模块用法实例分析
May 27 #Python
Python创建模块及模块导入的方法
May 27 #Python
Python类的用法实例浅析
May 27 #Python
Python socket编程实例详解
May 27 #Python
Python简单删除目录下文件以及文件夹的方法
May 27 #Python
You might like
PHP数字字符串左侧补0、字符串填充和自动补齐的几种方法
2014/05/10 PHP
getJSON跨域SyntaxError问题分析
2014/08/07 PHP
PHP编程获取音频文件时长的方法【基于getid3类】
2017/04/20 PHP
js类 from qq
2006/11/13 Javascript
javascript 常用功能总结
2012/03/18 Javascript
js自动闭合html标签(自动补全html标记)
2012/10/04 Javascript
Javascript图像处理—亮度对比度应用案例
2013/01/03 Javascript
js实现的简单radio背景颜色选择器代码
2015/08/18 Javascript
html、css和jquery相结合实现简单的进度条效果实例代码
2016/10/24 Javascript
JSON与js对象序列化实例详解
2017/03/16 Javascript
简单好用的nodejs 爬虫框架分享
2017/03/26 NodeJs
jQuery使用eraser.js插件实现擦除、刮刮卡效果的方法【附eraser.js下载】
2017/04/28 jQuery
jQuery输入框密码的显示隐藏【代码分享】
2017/04/29 jQuery
jQuery实现的简单动态添加、删除表格功能示例
2017/09/21 jQuery
详解如何将 Vue-cli 改造成支持多页面的 history 模式
2017/11/20 Javascript
vue项目中公用footer组件底部位置的适配问题
2018/05/10 Javascript
js实现页面多个日期时间倒计时效果
2019/06/20 Javascript
微信小程序如何利用getCurrentPages进行页面传值
2019/07/01 Javascript
基于vue手写tree插件的那点事儿
2019/08/20 Javascript
浅谈Vue.use到底是什么鬼
2020/01/21 Javascript
python使用wmi模块获取windows下硬盘信息的方法
2015/05/15 Python
python使用MySQLdb访问mysql数据库的方法
2015/08/03 Python
python 时间戳与格式化时间的转化实现代码
2016/03/23 Python
python3.6利用pyinstall打包py为exe的操作实例
2018/10/31 Python
洲际酒店集团大中华区:IHG中国
2016/08/17 全球购物
Oroton中国官网:澳洲知名奢侈配饰品牌
2017/03/26 全球购物
friso美素佳儿官方海外旗舰店:荷兰原产原罐
2017/07/03 全球购物
UGG英国官方网站:UGG UK
2018/02/08 全球购物
微软新西兰官方网站:Microsoft New Zealand
2018/08/17 全球购物
阿玛尼美妆英国官网:Giorgio Armani Beauty英国
2019/03/28 全球购物
数字化校园建设方案
2014/05/03 职场文书
2014年电教工作总结
2014/12/19 职场文书
网络销售员岗位职责
2015/04/11 职场文书
道歉短信大全
2015/05/12 职场文书
2015年食品安全宣传周活动总结
2015/07/09 职场文书
一封真诚的自荐信帮你赢得机会
2019/05/07 职场文书