类的继承、菱形继承、派生、多态

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

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):
  pass
f2=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就是经典类)

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、深度优先(经典类)

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

五、类的多态

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

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

import abc
class 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):
        pass
f=People(100,200)
f.sleep()
#接口
def func(obj):
    obj.eat()
func(Dog)
func(Zhu)

原文地址:https://www.cnblogs.com/chuwanliu/p/11056473.html

时间: 2024-07-31 12:38:14

类的继承、菱形继承、派生、多态的相关文章

C++中的类继承之单继承&amp;多继承&amp;菱形继承

 C++中的类继承之单继承&多继承&菱形继承 单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或以上直接父类时称这个继承关系为多继承.这种继承方式使一个子类可以继承多个父类的特性.多继承可以看作是单继承的扩展.派生类具有多个基类,派生类与每个基类之间的关系仍可看作是一个单继承.多继承下派生类的构造函数与单继承下派生类构造函数相似,它必须同时负责该派生类所有基类构造函数的调用.同时,派生类的参数个数必须包

C++对象内存分布(2) - 菱形继承(non virtual)

1.前言 本篇文章的所有代码例子,如果是windows上编译运行,则使用的是visual studio 2013.如果是RHEL6.5平台(linux kernal: 2.6.32-431.el6.i686)上编译运行,则其gcc版本为4.4.7,如下所示: [[email protected] ~]# gcc --version gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) 2.菱形继承类的内存分布 2.1.类的结构 菱形继承 - 重复继承 2.2.实现

菱形继承问题

#coding:utf-8 # python2中指定文件头'''1.菱形继承 当一个子继承多个父类时,多个父类最终继承了同一个类,称之为菱形继承 2.菱形继承的问题: python2区分经典类与新式类,如果子的继承是一个菱形继承,那么经典类与形式的区别为? 经典类下查找属性:深度优先查找 深度查找:比如A(B,C,D),深度查找就是A沿着B一路查找下去,直到世界尽头(最后一个父类), 然后才会跳到C查找,找到最后再跳到C 新式类下查找属性:广度优先查找 A(B,C,D)沿着A-B 不插着最后一个

c++ 植物类 继承多态 菱形继承

#pragma once//头文件 #include <iostream> #include<string> using namespace std; // // 1.实现以下几个类的成员函数 // 2.实现一个虚函数的覆盖及调用 // 3.处理菱形继承问题. // // 植物 class Botany { public: Botany(const string&  name); virtual ~Botany(); virtual void Display(); Bota

面向对象-继承,顺序查找,派生,子类访问父类,菱形继承

OOP的三大特征(优势) 1.封装 2.继承 3.多态 继承:是两个对象之间产生的一种关系 a继承b 在OOP的程序中继承是描述类与类之间的一种关系继承的好处:程序中 一个类a继承另一个类b a就可以使用B的属性和方法具体的说:继承极大的提高了代码的重用性继承描述的是 什么 是什么的关系 注意:在使用集成时,要自己分析.类与类之间的关系 不应该违反显示生活中的原则在使用继承的时候一定是先抽象 在继承抽象:抽取一堆类共同拥有的内容 形成一个新的抽象的概念(也称为公共基类) 这个过程就叫做 抽象注意

C++构造函数 &amp; 拷贝构造函数 &amp; 派生类的构造函数 &amp; 虚继承的构造函数

构造函数 ,是一种特殊的方法 .主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 .特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载.(摘自百度百科构造函数). 一.最基本的构造函数 1 class Base 2 { 3 public: 4 Base(int var) : m_Var(var) 5 { 6 } 7 private: 8 int m_Var; 9 }; 以上构造函数的执行过程:

类 (3) - 继承和多态

继承用来描绘现实情境中的is-a关系,即某物属于某种类别.c#不支持多重继承,但可以通过接口实现多重继承.通过继承,子类可以扩充父类的内容. 多态指的是根据类型的不同,相同的请求(相同的方法)可以做出不同的相应. C#实现多态最重要的方式就是接口.一个接口可能包括任意多个虚拟或者抽象方法,此时,继承了(实现了)该接口的类必须给出一个自己的实现(通过重写虚拟或者抽象方法).例如基类拥有虚拟方法speak,其没有实现,此时所有的派生类都要提供一个自己的实现,然后,对于任意的派生类,speak都对应着

C++基础学习教程(七)----类编写及类的两个特性解析---&gt;多态&amp;继承

类引入 到目前为止我们所写的自定义类型都是关键字struct,从现在起我们将采用class方式定义类,这种方式对于学习过其他高级语言包括脚本(Such as Python)的人来说再熟悉不过了. 但是在写之前我们还是需要比较一下用struct和class之间有什么区别. 首先对于struct,在C兼容性方面很重要,尽管C++是有别于C的另一门语言,但许多程序还是必须与C交互,C++有两个重要功能,可以方便的与C交互.其中之一的就是POD,即是Plain Old Data(简单旧式数据)的缩写.

处理菱形继承问题&&实现一个虚函数的覆盖及调用&&实现以下几个类的成员函数

#include <iostream> #include <string> using namespace std; 1.实现以下几个类的成员函数 2.实现一个虚函数的覆盖及调用 3.处理菱形继承问题. 植物 class Botany { public: //(const string& name) // const char* name Botany(const char* name = "") :_name(name) //构造函数 { //cout