博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
类的继承、菱形继承、派生、多态
阅读量:4444 次
发布时间:2019-06-07

本文共 4479 字,大约阅读时间需要 14 分钟。

一、类的继承(查找顺序先自己再父类)

class ParentFoo:    def __init__(self,first_name,car,money,house):        self.first_name=first_name        self.car=car        self.money=money        self.house=house    def teach(self):        print("%s教人"%self.first_name)# f1=ParentFoo('zhu','tesla',10,'Haozhai')class SonFoo(ParentFoo):  passf2=SonFoo('zhu','tesla',10,'Haozhai')f2.teach()#zhu教人
class Animal:    def __init__(self,name,height,weight):        self.height=height        self.weight=weight        self.name=name    def jiao(self):        print('%sjiao'%self.name)class People(Animal):    # def __init__(self):    #     pass    def read(self):        print('read')f1=People('alex',110,100)f1.jiao()
class Boo:    def f1(self):        print('我是f1')        print(self)        self.f2()class Foo(Boo):    def f2(self):        print('woshi f2')ff=Foo()ff.f1()#我是f1<__main__.Foo object at 0x10d611d68>woshi f2

二、类的派生(继承父类属性的情况下也使用自身属性)

#v1版本(通过父类调用自己的属性)class Animal:    def __init__(self,height,weight):        self.height=height        self.weight=weight    def jiao(self):        print('%sjiao')class People():    def __init__(self,name,age):        self.age=age        self.name=name    def read(self):        print('read')f1=People('alex',19)Animal.__init__(f1, 180, 100)print(f1.__dict__)#{'height': 180, 'age': 19, 'weight': 100, 'name': 'alex'}#v2版本(在子类的__init__中调用父类,这种跟继承没关系)class Animal:    def __init__(self,height,weight):        self.height=height        self.weight=weight    def jiao(self):        print('%sjiao')class People(Animal):    def __init__(self,name,age):        Animal.__init__(self, 180, 100)        self.age=age        self.name=name    def read(self):        print('read')f1=People('alex',19)print(f1.__dict__)#{'height': 180, 'weight': 100, 'age': 19, 'name': 'alex'}#v3版本(*************************)class Animal:    def __init__(self,height,weight):        self.height=height        self.weight=weight    def jiao(self):        print('%sjiao')class People(Animal):    def __init__(self,name,age):        super().__init__(180, 100)        self.age=age        self.name=name    def read(self):        print('read')f1=People('alex',19)print(f1.__dict__)#{'name': 'alex', 'weight': 100, 'height': 180, 'age': 19}#v4版本(不在继承条件下报错)class Animal:    def __init__(self,height,weight):        self.height=height        self.weight=weight    def jiao(self):        print('%sjiao')class People():    def __init__(self,name,age):        super().__init__(180, 100)        self.age=age        self.name=name    def read(self):        print('read')f1=People('alex',19)print(f1.__dict__)#报错

三、类派生应用

class People:    def __init__(self,name,age,gender):        self.name=name        self.age=age        self.gender=gender    def speak(self):        print('%s开始说话了'%self.name)class Student(People):    def __init__(self,name,age,gender,school,course):        super().__init__(name,age,gender)        self.school=school        self.course=course    def choose_course(self):        print('%s选择了%s的%s课程'%(self.name,self.school,self.course))class Teacher(People):    def __init__(self,name,age,gender,course):        super().__init__(name,age,gender)        self.course=course    def mark(self,student_name,score):        print("%s给%s学生打了%s分"%(self.name,student_name.name,score))f1=Student('owen',18,'man','oldboy','python')print(f1.__dict__)f2=Teacher('alex',20,'woman','python')print(f2.__dict__)f2.mark(f1,20)

四、菱形继承

1、新式类(只要默认继承了object类,python3默认继承了object)

2、经典类(没有默认继承object,python2就是经典类)

image-20190619114855582

class G:  def test(self):    print('from G')class E(G):  def test(self):    print('from E')class F(G):  def test(self):    print('from F')class E(G):  def test(self):    print('from E')class B(E):  def test(self):    print('from B')class C(F):  def test(self):    print('from C')class D(G):  def test(self):    print('from D')class A(B,C,D):  def test(self):    print('from A')f=A()f.test()

3、深度优先(经典类)

image-20190619115117989

4、广度优先(新式类,只出现在菱形继承中)

image-20190619115217893

五、类的多态

(只有拥有Animal的方法才能使用Animal内的类方法)

符合动物类规定的规则才是动物

import abcclass Animal(metaclass=abc.ABCMeta):    def __init__(self,height,weight):        self.height=height        self.weight=weight    @abc.abstractmethod    def speak(self):        print('开始叫了')    @abc.abstractmethod    def eat(self):        print('开始吃了')    def sleep(self):        print('开始睡觉',self)class People(Animal):    def speak(self):        pass    def eat(self):        passf=People(100,200)f.sleep()#接口def func(obj):    obj.eat()func(Dog)func(Zhu)

转载于:https://www.cnblogs.com/chuwanliu/p/11056473.html

你可能感兴趣的文章
PSP
查看>>
20165208 2017-2018-2 《Java程序设计》第九周学习总结
查看>>
Masonry的使用
查看>>
关于户口
查看>>
Web
查看>>
函数名应用,闭包,装饰器初识
查看>>
JavaScript Date Format
查看>>
【Python】python基础语法 编码
查看>>
springcloud---how2java--记录零碎的信息
查看>>
K-th largest element in an array
查看>>
并发编程之秒杀
查看>>
Windows 下面 redis 发布为服务的官方方法
查看>>
HDU 2066 一个人的旅行
查看>>
如何卸载Windows 7中的IE10并还原到IE9
查看>>
更新WordPress4.0访问速度慢问题解决办法
查看>>
金蝶 K/3 Cloud 服务端控件编程模型
查看>>
那些容易忽略的事(1) -变量与运算符+
查看>>
九度oj 题目1252:回文子串
查看>>
面向对象
查看>>
移动端调用电话、短信、唤起QQ和使用百度地图
查看>>