广告
返回顶部
首页 > 资讯 > 后端开发 > Python >day25-python之继承组合
  • 534
分享到

day25-python之继承组合

组合python 2023-01-31 00:01:08 534人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

1.上节回顾 1 class School: 2 x=1 3 def __init__(self,name,addr,type): 4 self.Name=name 5 self

1.上节回顾

 1 class School:
 2     x=1
 3     def __init__(self,name,addr,type):
 4         self.Name=name
 5         self.Addr=addr
 6         self.Type=type
 7 
 8     def tell_info(self):
 9         print('学校的详细信息是:name:%s addr:%s' %(self.Name,self.Addr))
10 
11 
12 # s1=School('oldboy','沙河','私立')
13 
14 # print(s1.__dict__)
15 #
16 # print(School.__dict__)
17 #
18 # s1.tell_info()
19 # School.tell_info(s1)

2.静态属性

 1 class Room:
 2     tag=1
 3     def __init__(self,name,owner,width,length,heigh):
 4         self.name=name
 5         self.owner=owner
 6         self.width=width
 7         self.length=length
 8         self.heigh=heigh
 9 
10     @property
11     def cal_area(self):
12         # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
13         return  self.width * self.length
14     @property
15     def cal1_area(self):
16         return self.width * self.length
17 
18 
19     def test(self):
20         print('from test',self.name)
21 
22     @claSSMethod
23     def tell_info(cls,x):
24         print(cls)
25         print('--》',cls.tag,x)#print('--》',Room.tag)
26     # def tell_info(self):
27     #     print('---->',self.tag)
28     @classmethod
29     def tell_info1(cls,x):
30         print(cls)
31         print('-->>',cls.tag,x)
32 
33 # print(Room.tag)
34 
35 # Room.test(1) #1.name
36 # r1=Room('厕所','alex',100,100,100000)
37 # Room.tell_info(10)
38 
39 r1=Room('厕所','alex',100,100,100000)
40 r2=Room('公共厕所','yuanhao',1,1,1)
41 # print('%s 住的 %s 总面积是%s' %(r1.owner,r1.name,r1.width*r1.length))
42 # print('%s 住的 %s 总面积是%s' %(r2.owner,r2.name,r2.width*r2.length))
43 # r1.cal_area()
44 # r2.cal_area()
45 print(r1.cal_area)
46 print(r2.cal_area)
47 print(r1.name)
48 print(r2.name)

3.类方法

 1 class Room:
 2     tag=1
 3     def __init__(self,name,owner,width,length,heigh):
 4         self.name=name
 5         self.owner=owner
 6         self.width=width
 7         self.length=length
 8         self.heigh=heigh
 9 
10     @property
11     def cal_area(self):
12         # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
13         return  self.width * self.length
14     @property
15     def cal_area1(self):
16         return self.width * self.length
17 
18 
19     def test(self):
20         print('from test',self.name)
21 
22     @classmethod
23     def tell_info(cls,x):
24         print(cls)
25         print('--》',cls.tag,x)#print('--》',Room.tag)
26     # def tell_info(self):
27     #     print('---->',self.tag)
28 
29     @classmethod
30     def tell_info1(cls,x):
31         print(cls)
32         print('-->>',cls.tag,x)
33 
34 # print(Room.tag)
35 
36 # Room.test(1) #1.name
37 r1=Room('厕所','alex',100,100,100000)
38 # Room.test(r1)
39 Room.tell_info(10)

4.静态方法

 1 class Room:
 2     tag=1
 3     def __init__(self,name,owner,width,length,heigh):
 4         self.name=name
 5         self.owner=owner
 6         self.width=width
 7         self.length=length
 8         self.heigh=heigh
 9 
10     @property
11     def cal_area(self):
12         # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
13         return  self.width * self.length
14 
15     @classmethod
16     def tell_info(cls,x):
17         print(cls)
18         print('--》',cls.tag,x)#print('--》',Room.tag)
19     # def tell_info(self):
20     #     print('---->',self.tag)
21 
22     @staticmethod
23     def wash_body(a,b,c):
24         print('%s %s %s正在洗澡' %(a,b,c))
25 
26     @staticmethod
27     def wash_body1(a,b,c):
28         print('%s %s %s正在洗澡'%(a,b,c))
29 
30     def test(x,y):
31         print(x,y)
32 
33 # Room.wash_body('alex','yuanhao','wupeiqi')
34 
35 # print(Room.__dict__)
36 
37 
38 r1=Room('厕所','alex',100,100,100000)
39 #
40 # print(r1.__dict__)
41 # r1.wash_body('alex','yuanhao','wupeiqi')
42 
43 # Room.test(1,2)
44 r1.test(1)

5.组合

  1 # class Hand:
  2 #     pass
  3 #
  4 # class Foot:
  5 #     pass
  6 #
  7 # class Trunk:
  8 #     pass
  9 #
 10 # class Head:
 11 #     pass
 12 
 13 
 14 # class Person:
 15 #     def __init__(self,id_num,name):
 16 #         self.id_num=id_num
 17 #         self.name=name
 18 #         self.hand=Hand()
 19 #         self.foot=Foot()
 20 #         self.trunk=Trunk()
 21 #         self.head=Head()
 22 # p1=Person('111111','alex')
 23 #
 24 #
 25 # print(p1.__dict__)
 26 
 27 # class School:
 28 #     def __init__(self,name,addr):
 29 #         self.name=name
 30 #         self.addr=addr
 31 #
 32 #     def zhao_sheng(self):
 33 #         print('%s 正在招生' %self.name)
 34 #
 35 # class Course:
 36 #     def __init__(self,name,price,period,school):
 37 #         self.name=name
 38 #         self.price=price
 39 #         self.period=period
 40 #         self.school=school
 41 # #
 42 # #
 43 # #
 44 # s1=School('oldboy','北京')
 45 # s2=School('oldboy','南京')
 46 # s3=School('oldboy','东京')
 47 #
 48 # # c1=Course('linux',10,'1h','oldboy 北京')
 49 # c1=Course('linux',10,'1h',s1)
 50 # #
 51 # print(c1.__dict__)
 52 # print(c1.school.name)
 53 # print(s1)
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 class School:
 68     def __init__(self,name,addr):
 69         self.name=name
 70         self.addr=addr
 71 
 72 
 73     def zhao_sheng(self):
 74         print('%s 正在招生' %self.name)
 75 
 76 class Course:
 77     def __init__(self,name,price,period,school):
 78         self.name=name
 79         self.price=price
 80         self.period=period
 81         self.school = school
 82 
 83 
 84 
 85 s1=School('oldboy','北京')
 86 s2=School('oldboy','南京')
 87 s3=School('oldboy','东京')
 88 #
 89 # c1=Course('linux',10,'1h','oldboy 北京')
 90 c1=Course('linux',10,'1h',s1)
 91 
 92 msg='''
 93 1 老男孩 北京校区
 94 2 老男孩 南京校区
 95 3 老男孩 东京校区
 96 '''
 97 while True:
 98     print(msg)
 99     menu={
100         '1':s1,
101         '2':s2,
102         '3':s3
103     }
104     choice=input('选择学校>>: ')
105     school_obj=menu[choice]
106     name=input('课程名>>: ')
107     price=input('课程费用>>: ')
108     period=input('课程周期>>: ')
109     new_course=Course(name,price,period,school_obj)
110     print('课程【%s】属于【%s】学校' %(new_course.name,new_course.school.name))

6.继承

 1 class Dad:
 2     '这个是爸爸类'
 3     money=10
 4     def __init__(self,name):
 5         print('爸爸')
 6         self.name=name
 7     def hit_son(self):
 8         print('%s 正在打儿子' %self.name)
 9 
10 class Son(Dad):
11     money = 1000000000009
12     def __init__(self,name,age):
13         self.name=name
14         self.age=age
15 
16     def hit_son(self):
17         print('来自儿子类')
18 # print(Son.money)
19 # Son.hit_son(Son('andy',12))
20 # print(Dad.__dict__)
21 # print(Son.__dict__)
22 s1=Son('alex',18)
23 # s1.hit_son()
24 print(s1.money)
25 print(Dad.money)
26 print(s1.name)
27 print(s1.money)
28 print(s1.__dict__)
29 s1.hit_son()

7.接口继承

 1 import abc
 2 class All_file(metaclass=abc.ABCMeta):
 3     @abc.abstractmethod
 4     def read(self):
 5         pass
 6 
 7     @abc.abstractclassmethod
 8     def read1(self):
 9         pass
10 
11     @abc.abstractmethod
12     def write(self):
13         pass
14 
15     @abc.abstractclassmethod
16     def write1(self):
17         pass
18 
19 class Disk(All_file):
20     def read(self):
21         print('disk read')
22 
23     def write(self):
24         print('disk write')
25 
26 class Cdrom(All_file):
27     def read(self):
28         print('cdrom read')
29 
30     def write(self):
31         print('cdrom write')
32 
33 
34 class Mem(All_file):
35     def read(self):
36         print('mem read')
37 
38     def write(self):
39         print('mem write')
40 
41 class Mem1(All_file):
42     def read(self):
43         print('mem read')
44 
45     def write(self):
46         print('mem write')
47 #
48 m1=Mem1()
49 m1.read()
50 m1.write()

8.继承顺序

 1 #coding:utf-8
 2 class A:
 3     def test(self):
 4         print('A')
 5     pass
 6 class B(A):
 7     # def test(self):
 8     #     print('B')
 9 
10     pass
11 class C(A):
12     def test(self):
13         print('C')
14     pass
15 
16 class D(B):
17     # def test(self):
18     #     print('D')
19     pass
20 
21 class E(C):
22     # def test(self):
23     #     print('E')
24     pass
25 
26 class F(D,E):
27     # def test(self):
28     #     print('F')
29     pass
30 f1=F()
31 f1.test()   #经典类:F->D->B->A-->E-->
32 
33 print(F.__mro__)
34 
35 #F-->D->B-->E--->C--->A新式类

9.在子类中调用父类的方法

 1 class Vehicle:
 2     Country='China'
 3     def __init__(self,name,speed,load,power):
 4         self.name=name
 5         self.speed=speed
 6         self.load=load
 7         self.power=power
 8     def run(self):
 9         print('开动啦')
10         print('开动啦')
11 class Subway(Vehicle):
12         def __init__(self,name,speed,load,power,line):
13            Vehicle.__init__(self,name,speed,load,power)
14            self.line=line
15 
16         def show_info(self):
17             print(self.name,self.speed,self.load,self.power,self.line)
18 
19         def run(self):
20             Vehicle.run(self)
21             print('%s %s 线,开动啦' %(self.name,self.line))
22 line13=Subway('北京地铁','10km/s',1000000000,'电',13)
23 
24 line13.show_info()
25 
26 line13.run()

10.super方法的使用

 1 class Vehicle1:
 2     Country='China'
 3     def __init__(self,name,speed,load,power):
 4         self.name=name
 5         self.speed=speed
 6         self.load=load
 7         self.power=power
 8     def run(self):
 9         print('开动啦')
10         print('开动啦')
11 class Subway(Vehicle1):
12         def __init__(self,name,speed,load,power,line):
13            # Vehicle.__init__(self,name,speed,load,power)
14            # super().__init__(name,speed,load,power)  #super(__class__,self).__init__(name,speed,load,power)
15            super(Subway,self).__init__(name,speed,load,power)
16            self.line=line
17         def show_info(self):
18             print(self.name,self.speed,self.load,self.power,self.line)
19         def run(self):
20             # Vehicle.run(self)
21             super().run()
22             print('%s %s 线,开动啦' %(self.name,self.line))
23 line13=Subway('北京地铁','10km/s',1000000000,'电',13)
24 line13.show_info()
25 line13.run()
26 
27 print(line13.__class__)

 

--结束END--

本文标题: day25-python之继承组合

本文链接: https://www.lsjlt.com/news/182433.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • day25-python之继承组合
    1.上节回顾 1 class School: 2 x=1 3 def __init__(self,name,addr,type): 4 self.Name=name 5 self...
    99+
    2023-01-31
    组合 python
  • 多继承 , 组合 , 菱形继承 , 接口
    一, 复习 属性的的正确存放位置: 类中应该存储所有对象公共的内容 对象中存储都是每个对象独有的(都不同) 初始化函数: 给对象的属性赋初值 , 可以保证只要对象被创建就一定有相应的属性 节省了重复代码 ...
    99+
    2023-01-31
    组合 菱形 接口
  • Java继承与组合
    系列文章目录 你真的知道怎样用java敲出Hello World吗?—初识JAVA 你知道为什么会划分数据类型吗—JAVA数据类型与变量 10 > 20 && 10 / 0 == 0等于串联小灯泡—J...
    99+
    2023-09-06
    java
  • JavaScript组合继承详解
    目录1、前言2、原型链继承3、构造函数继承4、组合继承1、前言 首先学习继承之前,要对原型链有一定程度的了解。 不了解可以去先阅读我另一篇文章,里面对原型链有一个较为详细的说明:Ja...
    99+
    2022-11-12
  • C++中的类扩展之继承和组合详解
    目录相关术语一、继承二、组合相关术语 继承:继承父类后可以拥有父类对应的属性和方法。 组合:将类作为成员对象,基类可以直接调用派生类对应的属性和方法。 一、继承 继承是指在一个已有的...
    99+
    2023-05-17
    C++类扩展 C++继承 C++组合
  • javascript中什么是组合继承
    javascript中什么是组合继承?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1、说明用原型链实现原型属性和方法的继承,借用构造函数技术实现实例属性的继承。2、缺点组合模式...
    99+
    2023-06-15
  • JavaScript进阶教程之非extends的组合继承详解
    目录前言一:call() 的作用与使用 1.1 使用 call() 来调用函数 1.2 使用 call() 来改变 this 的指向 &nbs...
    99+
    2022-11-13
    js extends继承 javascript组合继承 js继承机制
  • C++数据结构继承的概念与菱形继承及虚拟继承和组合
    目录继承的概念继承的定义基类和派生类对象之间的赋值转换继承中的作用域派生类的默认成员函数继承中的两个小细节继承和友元继承和静态成员单继承和多继承(菱形继承)虚拟继承概念虚拟继承的原理...
    99+
    2022-11-13
  • Maven的使用之继承与聚合
    目录一、实验:继承1、概念2、作用3、举例4、操作4.1 创建父工程4.2 创建模块工程4.3 查看被添加新内容的父工程 pom.xml4.4 解读子工程的pom.xml4.5 在父...
    99+
    2023-05-17
    Maven的使用 Maven继承与聚合 Maven继承 Maven聚合
  • python 面向对象之继承
    文章目录 前言继承的概念单继承多继承子类重写父类的同名方法和属性子类调用父类同名的方法和属性多层继承私有权限 前言 前面我们已经学习了 python 面向对象的类和对象,那么今天我将为大...
    99+
    2023-09-01
    python 开发语言
  • JavaScript组合继承的示例分析
    这篇文章主要为大家展示了“JavaScript组合继承的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript组合继承的示例分析”这篇文章吧。原型链继承父类实例作为子类的原型...
    99+
    2023-06-25
  • C++数据结构继承的概念与菱形继承及虚拟继承和组合分析
    这篇“C++数据结构继承的概念与菱形继承及虚拟继承和组合分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C++数据结构继承...
    99+
    2023-06-29
  • javascript组合继承的意思是什么
    这篇文章主要介绍“javascript组合继承的意思是什么”,在日常操作中,相信很多人在javascript组合继承的意思是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”javascript组合继承的意思...
    99+
    2023-06-20
  • python3--类的组合,初始类的继承
    面向对象的组合用法软件重用的重要方式除了继承之外还有另外一种方式,即:组合组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合例1# 人狗大战 class Person:    ...
    99+
    2023-01-30
    组合
  • python如何继承命名元组
    这篇文章将为大家详细讲解有关python如何继承命名元组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。继承命名元组>>> class ...
    99+
    2022-10-19
  • 简单谈谈JavaScript寄生式组合继承
    组合继承 组合继承也被称为伪经典继承,它综合了我们昨天说的原型链和盗用构造函数,将俩者的有点结合在了一起。它的基本思想是使用原型链继承原型上的属性和方法,通过盗用构造函数继承实例属...
    99+
    2022-11-12
  • JavaScript如何实现寄生式组合继承
    小编给大家分享一下JavaScript如何实现寄生式组合继承,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!寄生式组合继承的实现?...
    99+
    2022-10-19
  • javascript寄生式组合继承怎么实现
    这篇文章主要讲解了“javascript寄生式组合继承怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“javascript寄生式组合继承怎么实现”吧!说明寄生组合继承通过盗用构造函数继...
    99+
    2023-06-20
  • python 面向对象之类的继承
    python中什么是继承:新类不必从头编写新类从现有的类继承,就自动拥有了现有类的所有功能新类只需要编写现有类缺少的新功能继承的好处:复用已有代码自动拥有了现有类的所有功能只需要编写缺少的新功能继承的特点:子类和父类是is关系python继...
    99+
    2023-01-30
    面向对象 python
  • python 面向对象之继承顺序
    先来看一个经典类class A:     def __init__(self):         p...
    99+
    2023-01-30
    面向对象 顺序 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作