python面向对象学习-类、继承、多重继承、初始化函数__init__、super方法
2019-04-15 23:30
826 查看
本文主要涉及类的创建、私有、继承、检验父类的方法以及初始化函数即super方法的使用。
一、基础知识
#pyhton3.6 #类 class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print("hello,world! I'm %s." %self.name) foo = Person() bar = Person() foo.setName('l sk') bar.setName('a sl') foo.greet() foo.name out[13]:hello,world! I'm l sk. 'l sk' #私有 class Secretive: def __inaccessible(self): print ('But you cant see me ') def accessible(self): print ('the secret is ') self.__inaccessible( s = Secretive() s._Secretive__inaccessible() out[20]:But you cant see me # 继承 class Filter: def init(self): self.blocked = [1] def filter(self,sequence): return [x for x in sequence if x not in self.blocked] class SPAMFilter(Filter): def init(self): self.blocked = ['SPAM'] f = Filter() f.init() f.filter([1,2,3]) out[37]:[2, 3] #子类 s = SPAMFilter() s.init() s.filter(['SPAM','fda']) out[39]:['fda'] #检验一个类是否为另一个类的子类 issubclass(SPAMFilter,Filter) out[43]:True #查找类的父类 SPAMFilter.__bases__ out[44]:(__main__.Filter,) #检验对象是否为类的实例 isinstance(s,SPAMFilter) out[50]:True #从两个父类中继承--多重继承 class Calcutor: def calculate(self,expression): self.value = eval(expression) class Talker: def talk(self): print("this is :",self.value) class TalkingCalculator(Calcutor,Talker): print("two") tc = TalkingCalculator() tc.calculate('1+2') tc.talk() out[52]:this is : 3
二、含有初始化函数的继承
1如果子类没有定义自己的初始化函数,父类的初始化函数会被默认调用;
但是如果要实例化子类的对象,则只能传入父类的初始化函数对应的参数,否则会出错
主要作用是要求用户在实例化对象的时候提供初始化函数里面的相应参数
#pyhton3.6 class Parent(object): def __init__(self,data): self.data = data print("create an instance of:",self.__class__.__name__) print("data attribute is: ",self.data) class Child1(Parent): pass c1 = Child1("init Child") out[58]:create an instance of: Child1 data attribute is: init Child c2 = Child1() #不传参数报错 out[60]:TypeError: __init__() missing 1 required positional argument: 'data'
- 如果子类定义了自己的初始化函数,而没有显示调用父类的初始化函数,则父类的属性不会被初始化。
#pyhton3.6 class Child2(Parent): def __init__(self): print("i have __init__") c2 = Child2() out[60]:i have __init__
- 如果子类定义了自己的初始化函数,显示调用父类,子类和父类的属性都会被初始化
#pyhton3.6 class Child3(Parent): def __init__(self): print ("i have __init__") super(Child3,self).__init__("data from Child3,but __init__ from parent") c3 = Child3() out[60]:i have __init__ create an instance of: Child3 data attribute is: data from Child3,but __init__ from parent
三、super使用
在子类中,一般会定义与父类相同的属性(数据属性,方法),从而来实现子类特有的行为。也就是说,子类会继承父类的所有的属性和方法,子类也可以覆盖父类同名的属性和方法。有时候可能需要在子类中访问父类的一些属性:
#pyhton3.6 class Parent_super(object): def fun(self): print ("this fun is from parent") class Child4(Parent_super): def fun(self): print("this fun is from child") super(Child4,self).fun() # super(Child4,self).fun()可以理解为:首先找到Child4的父类Parent_super,然后调用父类的fun()方法, # 同时将Child的实例self传递给foo方法 child_super = Child4() child_super.fun() out[69]:this fun is from child this fun is from parent
相关文章推荐
- python 详解类class的继承、__init__初始化、super方法(五)
- python中类的继承,初始化,super()__init__()方法
- 【Python】学习笔记——-8.3、面向对象高级编程:3.多重继承
- 笨方法学Python—习题41:学习面向对象术语
- python面向对象学习(六)类属性、类方法、静态方法
- 面向对象之构造函数(可让程序运行前初始化一些东西)、函数(方法)重载
- java学习日记_34:面向对象之super关键词、this与super的区别和继承中构造方法的关系。08.16~17
- JavaScript学习--Item8 函数,方法,构造函数调用 标签: javascript构造函数面向对象函数call
- python面向对象学习(三)私有属性和私有方法
- python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
- 黑马程序员_学习笔记:4) 面向对象2:继承、this、super、finally、Abstract、模板设计模式、接口
- Python面向对象学习笔记-类、属性、方法
- python学习笔记6.4-类的多重继承(super()函数)
- JAVA面向对象基础:继承、变量隐藏、方法重写、包、修饰符、this、super
- java面向对象总结(三)继承【This和Super、覆盖(复写,重写)、final、抽象类: abstract、模板方法设计模式】
- python学习第十五天 -面向对象之继承和多态
- Python 面向对象中的__init__、__new__以及super()
- java学习之旅39--面向对象_12_继承_方法的重写
- java学习之路 之 高级类特性1-面向对象特征之二:继承及方法重写(override)
- python学习之路3之面向对象与继承