python学习第五章
1.继承
即是一个派生的类(derived class)继承基类(base class)的字段和方法,继承也允许把一个 派生类的对象作为 一个基类
对象对待。通俗来讲就是方便,继承前人的代码,减少工作量,当然这一切是为实现多态解决解决多继承的尴尬。具体实现如下:
class A:
def __init__(self):
print("the frist number is 1")
def __init__(self):
print("the second number is 2")
class B:
def __init__(self):
print("the three number is 3")
def __init__(self):
print("the four number is 4")
class C(A,B):
def __init__(self):
A.__init__(self)
B.__init__(self)
super(A,self).__init__()
print("C的改造方法")
c=C() class person:
def __init__(self,name=None,age=None):
self.name=name
self.age=age
def __str___(self):
return "我是{0},今年 {1}".format(self.name,self.age)
def __add__(self, other):
return person(self.name+other.name,self.age+other.age)
def __lt__(self, other):
if self.name==other.name:
return other.age<other.age
else:
return self.name<other.name
# def __cmp__(self, other):
# return self.name
p=person("JXD",20)
p2=person("dongdong",23)
print(p+p2)
print("-----------------")
p3=[person("dongdong",100),person("dong4",123),]
for p in p3:
print(p)
print("------------------")
for p in sorted(p3):
print(p)
2.类中内置的方法
在python中有一些内置的方法,这些方法命名都有着比较特殊的地方(其方法名是以2个下划线开始然后以2个下滑线结束。)
类中最常见的构造方法和析构方法。
构造方法:__int__(self,....)在生成对象时调用,可以用来进行一些初始化的操作,不需要显示去调用,系统会默认去执行。
构造方法支持重载,如果用户没有重新定义构造方法,系统就会自动执行默认的构造方法
析构方法:__del__(self,...)在释放对象时调用,支持重载,可以在里面进行一些释放资源的操作,不需要显示调用。
__str__(self):在使用print语句时被调用
__len__(self): 在调用内联函数len()时被调用
__cmp__(src,dst): 比较俩个数的src和dst时被调用
3.python 调用内部类的两种方法:
Class Car:#外部类
class Whell#内部类
def run(self):
print(‘car run’)
class Door:
def open(self):
print(‘open door’)
if __name__=="__main__":
car=Car()#实例化外部类
backDoor=Car.Door()#实例化内部类 第一种方法
frontDoor=car.Door()#再次实化Car的内部类 第2种方法
backDoor.open()
frontDoor.open()
wheel=car.whell() Whell()再次实例化内部类
whell.run()#调用内部类的方法
python学习第五章的更多相关文章
- python学习第五次笔记
python学习第五次笔记 列表的缺点 1.列表可以存储大量的数据类型,但是如果数据量大的话,他的查询速度比较慢. 2.列表只能按照顺序存储,数据与数据之间关联性不强 数据类型划分 数据类型:可变数据 ...
- Python学习第五堂课
Python学习第五堂课推荐电影:华尔街之狼 被拯救的姜哥 阿甘正传 辛德勒的名单 肖申克的救赎 上帝之城 焦土之城 绝美之城 #上节内容: 变量 if else 注释 # ""& ...
- python学习(五) 条件、循环和其他语句
第五章 条件.循环和其他语句 5.1 print和import的更多信息 5.1.1 使用逗号输出 >>> print('age',43,45) // 可以用逗号隔开 ...
- Java基础知识二次学习--第五章 数组
第五章 数组 时间:2017年4月26日15:11:30~2017年4月26日15:15:54 章节:05章_01节 视频长度:09:30 内容:一维数组的内存分析 心得: Java中数组是引用类型 ...
- C#高级编程 (第六版) 学习 第五章:数组
第五章 数组 1,简单数组 声明:int[] myArray; 初始化:myArray = new int[4]; 为数组分配内存. 还可以用如下的方法: int[] myArray = new in ...
- python 教程 第五章、 函数
第五章. 函数 定义语句后面要加冒号 1) 定义函数 def sayHello(): print 'Hello World!' sayHello() 2) 变量作用域 LEGB原则 L本地 ...
- Python爬虫学习==>第五章:爬虫常用库的安装
学习目的: 爬虫有请求库(request.selenium).解析库.存储库(MongoDB.Redis).工具库,此节学习安装常用库的安装 正式步骤 Step1:urllib和re库 这两个库在安装 ...
- 流畅的python第十五章上下文管理器和else块学习记录
with 语句和上下文管理器for.while 和 try 语句的 else 子句 with 语句会设置一个临时的上下文,交给上下文管理器对象控制,并且负责清理上下文.这么做能避免错误并减少样板代码, ...
- 进击的Python【第五章】:Python的高级应用(二)常用模块
Python的高级应用(二)常用模块学习 本章学习要点: Python模块的定义 time &datetime模块 random模块 os模块 sys模块 shutil模块 ConfigPar ...
随机推荐
- mysql数据库存中文字段
mysql数据默认编码是拉丁,而我们更多的使用utf8, 在创建库的时候执行参数即可: CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET ...
- Error creating bean with name 'transactionManager'
查看数据库是否连通,看错误的具体信息 看ssm配置文件是否被正确加载,上次我的错误是beans之类的错误,就是spring文件没有被加载,因为 而文件是applicationConfig.xml
- leetcode10
class Solution { public boolean isMatch(String s, String p) { if (s == null || p == null) { return f ...
- 1、detail页面 /items/detail/:id
<template> <div class="item_detail"> <van-swipe :autoplay="3000" ...
- netstat 常用参数总结
netstat 是一个机器网络查看工具 . 网络连接有哪些参数?
- ppt复制文本框文字到word的方法
打开ppt按Alt+F11,插入--模块, 选中“工具”--“引用”--MicroSoft Word .. 复制代码: Sub Main() On Error Resume Next Dim tem ...
- 绑定checkedComboBox
using System; namespace CommonLib{ /// <summary> /// CommonCode 的摘要说明. /// </summary> [S ...
- Python基础-python数据类型之元祖、字典(四)
元祖 Python的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号. tuple=(1,2,3,4) print(tuple) 访问元祖 通过索引访问,也可以进行切片操 ...
- swift - UIButton按钮有图片是点击高亮 有灰色动画
取消 高亮的 动画 btn.adjustsImageWhenHighlighted = false btn.layer.removeAllAnimations()
- win10安装MongoDB提示 the domain,user name and/or password are incorrect. Remember to use "." for the domain if the account is on the local machine.
好心塞,提示输入不合法. 后来发现这样可以解决.退出安装.重新打开()因为我第一次打开时是没有卡在这一步的,只不过返回上一页时就一直卡在验证的页面了),默认,默认,默认,