python 类继承演示范例的代码
把做工程过程重要的代码片段备份一次,下面的资料是关于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 类继承演示范例的代码的更多相关文章
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- Python 类继承,__bases__, __mro__, super
Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...
- 第7.7节 案例详解:Python类继承机制
本节实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识.例子以车.汽车为例,车为父类.汽车为子类. 一. 定义父类Vehicle class Vehicle(): def __ ...
- Python类继承(转发)
目录 一.概述 二.类的继承 2.1 继承的定义 2.2 构造函数的继承 2.3 子类对父类方法的重写 三.类继承的事例 回到顶部 一.概述 面向对象编程 (OOP) 语言的一个主要功能就是“继承”. ...
- python 类 - 继承
继承 什么是继承? 编写类时,并非总要从空白开始.如果要编写的类是另一个现成类的特殊版本,可使用继承. 一个类继承另一个类时,将自动获得另一个类的所有属性和方法.现有的类称为父类,而新类称为子类. 子 ...
- python类继承的重写和super
给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...
- python 类继承与子类实例初始化
From: https://blog.csdn.net/cs0301lm/article/details/6002504?utm_source=blogxgwz4 [ 先贴参考书籍原文(中文英文对照) ...
- python 类继承
#!/usr/bin/python # Filename: inherit.py class SchoolMember: '''Represents any school member.''' def ...
- python类继承中构造子的调用
python面向对象中的继承关系中,子类对父类的构造方法的调用有两种方法: 父类名.__init__(self,参数) #注意名字是父类 super(本子类名,self)__init__(其他参数) ...
随机推荐
- MySQL 数据库在 Windows 下修复 only_full_group_by 的错误
本机上新安装了个MySQL数据库,在插入数据的时候一直提示这个错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY ...
- [Swift]LeetCode893. 特殊等价字符串组 | Groups of Special-Equivalent Strings
You are given an array A of strings. Two strings S and T are special-equivalent if after any number ...
- 机器学习入门18 - 生产机器学习系统(Production ML Systems)
除了实现机器学习算法之外,机器学习还包含许多其他内容.生产环境机器学习系统包含大量组件.无需自行构建所有内容,而是应该尽可能重复使用常规机器学习系统组件.通过了解机器学习系统的一些范例及其要求,可以明 ...
- 面向对象-Java MOOC翁恺老师第一次作业
由于看这个慕课的时候已经结课了,没有办法提交查看代码是否正确...先保存一下,以后再提交改错 欢迎批评指正! 题目链接:https://www.icourse163.org/learn/ZJU-100 ...
- nginx 关于client_max_body_size client_body_buffer_size配置
最近生产环境在这两个参数之间遇到过几次坑.这里记录下.client_max_body_sizeclient_max_body_size 默认 1M,表示 客户端请求服务器最大允许大小,在“Conten ...
- Solr 09 - SolrJ操作Solr单机服务 (Solr的Java API)
目录 1 SolrJ是什么 2 SolrJ对索引的CRUD操作 2.1 创建Maven工程(打包方式选择为jar) 2.2 配置pom.xml文件, 加入SolrJ的依赖 2.3 添加和修改索引 2. ...
- [工具]PyCharm激活、注册码无效解决办法
前言 我是个 Pythoner,开发工具一直使用的 JetBrains 的 PyCharm.我师傅告诉过我:一个程序员一定要有一个用的很 6 的 IDE,你的开发效率会提高很多,很多... 我从小白的 ...
- SpringBoot是如何动起来的
SpringBoot是如何动起来的 程序入口 SpringApplication.run(BeautyApplication.class, args); 执行此方法来加载整个SpringBoot的环境 ...
- Linux服务器时间相关命令记录
前言 以往安装服务器时间都是正常,但是最近服务器的时间经常出现问题,所以在安装配置完成服务器之后需要对服务器的时间进行测试,如果服务器时间异常,那么当程序去取系统时间的时候就会出现问题. 时间相关命令 ...
- RDIFramework.NET平台代码生成器V3.2版本全新发布(提供下载-免费使用)
回顾V3.1版本更新内容如下: 1.增加对Oracle表创建语句的查看. 2.新增对MySql的代码生成支持. 3.全面重构对多线程的支持,改变以前会无故退出的现象. 本次在V3.1版本的基础上,增加 ...