把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码。

# a simple example of a class inheritance
# tested with Python24 vegaseat 10aug2005 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 "Class1 says hello:"
c1.Hello1() print "Class2 says a Class1 hello:"
c2.Hello1() print "Class2 says its own hello:"
c2.Hello2() print "Class1 color via __init__():"
c1.printColor() print "Class2 color via inherited __init__() and printColor():"
c2.printColor() print "Class1 changes its mind about the color:"
c1 = Class1('yellow') # same as: c1.__init__('yellow')
c1.printColor() print "Wonder what Class2 has to say now:"
c2.printColor() # 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 # 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()"

  

python 类继承演示范例的代码的更多相关文章

  1. python类继承

    面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...

  2. Python 类继承,__bases__, __mro__, super

    Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...

  3. 第7.7节 案例详解:Python类继承机制

    本节实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识.例子以车.汽车为例,车为父类.汽车为子类. 一.    定义父类Vehicle class Vehicle():    def __ ...

  4. Python类继承(转发)

    目录 一.概述 二.类的继承 2.1 继承的定义 2.2 构造函数的继承 2.3 子类对父类方法的重写 三.类继承的事例 回到顶部 一.概述 面向对象编程 (OOP) 语言的一个主要功能就是“继承”. ...

  5. python 类 - 继承

    继承 什么是继承? 编写类时,并非总要从空白开始.如果要编写的类是另一个现成类的特殊版本,可使用继承. 一个类继承另一个类时,将自动获得另一个类的所有属性和方法.现有的类称为父类,而新类称为子类. 子 ...

  6. python类继承的重写和super

    给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...

  7. python 类继承与子类实例初始化

    From: https://blog.csdn.net/cs0301lm/article/details/6002504?utm_source=blogxgwz4 [ 先贴参考书籍原文(中文英文对照) ...

  8. python 类继承

    #!/usr/bin/python # Filename: inherit.py class SchoolMember: '''Represents any school member.''' def ...

  9. python类继承中构造子的调用

    python面向对象中的继承关系中,子类对父类的构造方法的调用有两种方法: 父类名.__init__(self,参数) #注意名字是父类 super(本子类名,self)__init__(其他参数) ...

随机推荐

  1. [Swift]LeetCode313. 超级丑数 | Super Ugly Number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  2. Storm学习笔记 - Storm初识

    Storm学习笔记 - Storm初识 1. Strom是什么? Storm是一个开源免费的分布式计算框架,可以实时处理大量的数据流. 2. Storm的特点 高性能,低延迟. 分布式:可解决数据量大 ...

  3. PHP实现登录注册

    一.首先实现一个PHP的简单登录注册的话 我们要简单的与后端定义一下接口和传输数据的方式 并且我们要有一个phpStudy服务器. 第一步:当我们点击注册按钮的时候数据库要接收到客户端请求的数据  第 ...

  4. JavaScript基础之值传递和引用传递

    js的值传递和引用(地址)传递 首先总述一下:js的5种基本数据类型 number,string,null,undefined,boolean 在赋值传递时是值传递,js的引用数据类型(object, ...

  5. 【Impala篇】---Hue从初始到安装应用

    一.前述 Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能.基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 是CDH平台首选的PB ...

  6. Python内置函数(49)——pow

    英文文档: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (co ...

  7. Java如何在运行时识别类型信息?

    在日常的学习工作当中,有一些知识是我们在读书的时候就能够习得:但有一些知识不是的,需要在实践的时候才能得到真知——这或许就是王阳明提倡的“知行合一”. 在Java中,并不是所有的类型信息都能在编译阶段 ...

  8. JVM基础系列第1讲:Java 语言的前世今生

    Java 语言是一门存在了 20 多年的语言,其年纪比我自己还大.虽然存在了这么长时间,但 Java 至今都是最大的工业级语言,许多大型互联网公司均采用 Java 来实现其业务系统.大到国际电商巨头阿 ...

  9. Windows Server 2012安装mysql5.7.24记录

    系统环境: 一.下载mysql5.7.24安装包 地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下载解压到相应的目录,我的路径 ...

  10. 文本主题模型之潜在语义索引(LSI)

    在文本挖掘中,主题模型是比较特殊的一块,它的思想不同于我们常用的机器学习算法,因此这里我们需要专门来总结文本主题模型的算法.本文关注于潜在语义索引算法(LSI)的原理. 1. 文本主题模型的问题特点 ...