简单场景的类继承、复杂场景的类继承(钻石继承)、super、类的方法
1、python子类调用父类成员有2种方法,分别是普通方法和super方法
class Base(object): #基类的表示方法可以多种,Base(),Base,Base(object),即新式类与经典类
def __init__(self):
print('Base create')
class childA(Base):
def __init__(self):
print("\n")
print('creat A ')
Base.__init__(self) #简单调用父类成员
class childB(Base):
def __init__(self):
print("\n")
print('creat B ')
super(childB, self).__init__()#super调用父类成员
#使用super()继承时不用显式引用基类
base = Base()
a = childA()
b = childB()
输出:
Base create creat A
Base create creat B
Base create
2、在子类中重写父类方法,采用super()继承
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print('Parent')
def bar(self, message):
print(message, 'from Parent') class FooChild(FooParent):
def __init__(self):
super(FooChild, self).__init__()
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
print('Child')
def bar(self, message):
super(FooChild, self).bar(message) #重写父类的方法
print('Child bar fuction')
print(self.parent)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
输出:
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.
3、复杂场景的类继承(钻石继承)
# 砖石继承采用super(),防止了基类被初始化两次
# 其先采用广度搜索,找到基类,顺序为:Leaf()——>Medium1——>Medium2——>Base
# 其初始化的顺序为:Base——>Medium2——>Medium1——>Leaf
class Base(object):
def __init__(self):
print("Base") class Medium1(Base):
def __init__(self):
super(Medium1, self).__init__()
print("Medium1") class Medium2(Base):
def __init__(self):
super(Medium2, self).__init__()
print("Medium2") class Leaf(Medium1, Medium2):
def __init__(self):
super(Leaf, self).__init__()
print("Leafinit")
Leaf()
输出:
Base
Medium2
Medium1
Leafinit
4、类的方法
# 1、实例方法:只能通过实例调用,实例方法第一个定义的参数只能是实例本身引用 class Myclass:
def foo(self):
print(id(self),'foo') a = Myclass() # 既然是实例对象,那就要创建实例
a.foo() # 输出类里的函数地址
print(id(a)) # 输出类对象的地址
# 结果地址一样 print("******************************") # 2、类方法:定义类方法,要使用装饰器@classmethod,定义的第一个参数是能是类对象的引用,
# 可以通过类或者实例直用
class Myclass:
@classmethod # 类装饰器
def foo2(cls):
print(id(cls),'foo2')
#类对象,直接可以调用,不需要实例化
print(id(Myclass),'classmethod')
Myclass.foo2()#直接使用类名可以调用 print("******************************") # 3、静态方法:定义静态方法使用装饰器@staticmethod,没有默认的必须参数,
# 通过类和实例直接调用
class Myclass:
@staticmethod # 静态方法
def foo3():
print('foo3')
Myclass.foo3() # 没有参数
# 结果foo3 输出:
2197397372264 foo
2197397372264
******************************
2197777487112 classmethod
2197777487112 foo2
******************************
foo3
简单场景的类继承、复杂场景的类继承(钻石继承)、super、类的方法的更多相关文章
- 继承+派生+分类+钻石继承(day20)
目录 昨日内容 面对对象编程 类 定义类时发生的事情 调用类时发生的事情 init 对象 对象查找类的属性的顺序 对象的绑定方法 python中万物皆对象 今日内容 继承 什么是继承 为什么要继承 如 ...
- 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)
组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...
- cocos2d-X学习之主要类介绍:场景(CCScene)
场景(CCScene) 类结构: CCScene主要有以下两个函数: bool init () //初始化函数 static CCScene * node (void) //生CCScene 作为 ...
- python 全栈开发,Day20(object类,继承与派生,super方法,钻石继承)
先来讲一个例子 老师有生日,怎么组合呢? class Birthday: # 生日 def __init__(self,year,month,day): self.year = year self.m ...
- python开发面向对象基础:接口类&抽象类&多态&钻石继承
一,接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数名)且并未实 ...
- C++ Pirmer : 第十五章 : 面向对象程序设计之基类和派生的定义、类型转换与继承与虚函数
基类和派生类的定义以及虚函数 基类Quote的定义: classs Quote { public: Quote() = default; Quote(cosnt std::string& bo ...
- 场景:A-->B-->C 跳转到C时,要关掉B的处理方法
场景:A-->B-->C 跳转到C时,要关掉B的处理方法:相当于从A跳转到C UIViewController *preController = [self.navigationContr ...
- java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种 ...
- 4-13 object类,继承和派生( super) ,钻石继承方法
1,object 类 object class A: ''' 这是一个类 ''' pass a = A() print(A.__dict__) # 双下方法 魔术方法 创建一个空对象 调用init方法 ...
随机推荐
- MySQL数据库MyISAM存储引擎转为Innodb
MySQL数据库MyISAM存储引擎转为Innodb 之前公司的数据库存储引擎全部为MyISAM,数据量和访问量都不是很大,所以一直都没什么问题.但是最近出现了MySQL数据表经常被锁的情况,直接导 ...
- shell的正则表达式
正则表达式处理文件的内容,shell处理文件本身 grep *匹配0到n个 .(点儿)能匹配任意字符----8.8.8.8用于测试外网是否通畅 egrep
- Oracle之:查询锁表,删除锁表
-- 查询当前哪个表被锁 select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo. ...
- Spark配置详解
Spark提供三个位置用来配置系统: Spark属性:控制大部分的应用程序参数,可以用SparkConf对象或者Java系统属性设置 环境变量:可以通过每个节点的 conf/spark-env.sh脚 ...
- http 异步 接收 回传 数据文字和文件流
public void HttpListenerStar() { try { HttpListener httpListener = new HttpListener(); httpListener. ...
- Git入门学习和使用
#开篇废话 开篇废话又回来了,离开博客算是有一年了,之间曾经痛下很多次决心,继续写博客,后来都失败了,前年为了申请个CSDN专家,每天发博客,那个高产的状态,现在已然不行了,时过境迁,当时为了吃口饱饭 ...
- [JOI2012春季合宿]Rotate (链表)
题意 题解 又是一道神仙题-- 显然的做法是大力splay,时间复杂度\(O((N+Q)N\log N)\), 可以卡掉. 正解: 使用十字链表维护矩阵,在周围增加第\(0\)行/列和第\((n+1) ...
- docker启动、关闭、重启命令
docker启动命令,docker重启命令,docker关闭命令 启动 systemctl start docker守护进程重启 sudo systemctl daemon-relo ...
- python3爬取拉钩招聘数据
使用python爬去拉钩数据 第一步:下载所需模块 requests 进入cmd命令 :pip install requests 回车 联网自动下载 xlwt 进入cmd命令 :pip install ...
- How to correctly set application badge value in iOS 8?
o modify the badge under ios8 you have to ask for permissions let settings = UIUserNotificationSetti ...