1.类的内置方法

Python内部类:

所谓内部类,就是在类的内部定义的类,主要目的是为了更好的抽象现实世界。

例子:

汽车是一个类,汽车的底盘轮胎也可以抽象为类,将其定义到汽车内中,而形成内部类,

更好的描述汽车类,因为底盘轮胎是汽车的一部分。

内部类实例化方法:

方法1:直接使用外部类调用内部类

方法2:先对外部类进行实例化,然后再实例化内部类

out_name = outclass_name()

in_name = out_name.inclass_name()

in_name.method()

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

print("I am chinese")

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

jack = People.Chinese()

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

jack = People.Chinese() #外部类调用内部类

print jack.name #外部类调用内部类对象

另一种方法,外部类调用内部类对象

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

ren = People() #实例化外部类

jack = ren.Chinese() #实例化内部类

print jack.name #打印内部类属性

print People.Chinese.name

print People.Chinese().name

魔术方法:

str(self)

构造函数与析构函数

构造函数:

用于初始化类的内部状态,Python提供的构造函数是__init__():

__init__():方法是可选的,如果不提供,python会给出一个默认的__init__方法。

析构函数:

用于释放对象占用的资源,python提供的析构函数是__del__():

__del__():也是可选的,如果不提供,则python会在后台提供默认析构函数。

构造函数str

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def __str__(self):

return "This is People class"

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

ren = People() #实例化外部类

print ren #默认执行__str__

__init__(self)初始化类:

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def __str__(self):

return "This is People class"

def __init__(self,c='white'): #类实例化时自动执行

self.color = c

self.think()

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

jack = People('green')

ren = People() #实例化外部类

print ren.color #通过对象访问属性是初始化后的值

print People.color #通过类访问还是原来的值

[root@localhost 20180110]# python test1.py

I am a black

I am a thinker

30

black

yellow

析构函数__del__():释放资源

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def __str__(self):

return "This is People class"

def __init__(self,c='white'): #类实例化时自动执行

print ("initing...")

self.color = c

self.think()

f = open('test.py')

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

def __del__(self):

print ("del....")

self.f.close()

jack = People('green')

ren = People() #实例化外部类

print ren.color #通过对象访问属性是初始化后的值

print People.color #通过类访问还是原来的值

垃圾回收机制:

Python采用垃圾回收机制来清理不再使用的对象;python提供gc模块释放不再使用的对象。

Python采用“引用计数”的算法方式来处理回收,即:当然某个对象在其作用域内不再被其

他对象引用的时候,python就自动化清除对象。

gc模块collect()可以一次性收集所有待处理的对象(gc.collect)

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

__age = 30 #私有属性

class Chinese(object):

name ="I am a Chinese."

def __str__(self):

return "This is People class"

def __init__(self,c='white'): #类实例化时自动执行

print ("initing...")

self.color = c

self.think()

f = open('test.py')

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

print self.__age

def __talk(self):

print "I am talking with Tom"

@classmethod #调用类的方法

def test(self):

print ("this is class method")

@staticmethod #调用类的方法

def test1():

print ("this is static method")

def __del__(self):

print ("del....")

self.f.close()

print gc.collect() 如果是0是没有回收的。

jack = People('green')

ren = People() #实例化外部类

print ren.color #通过对象访问属性是初始化后的值

print People.color #通过类访问还是原来的值

2.类的继承

类的继承

继承是面向对象的重要特性之一,

继承关系继承是相对两个类而言的父子关系

子类继承了父类的所有公有属性和方法,

继承,实现了代码重用

使用继承

继承可以重用已经存在的数据和行为,减少代码的重复编写,

Python在类名后使用一对括号来表示继承关系,括号中的即类为父类

class Myclass(ParentClass),

如果父类定义了__init__方法,子类必须显式调用父类的__init__方法,

ParentClass.__init__(self,[args...])

如果子类需要扩展父类的行为,可以添加__init__方法的参数.

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def think(self):

self.color = "black"

print "I am a %s " % self.color

print ("I am a thinker")

class Chinese(People):

pass

cn = Chinese()

print cn.color

cn.think()

父类中有构造函数:

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def __init__(self):

print "Init..."

self.dwell = 'Earth'

def think(self):

print "I am a %s " % self.color

print ("I am a thinker")

class Chinese(People):

pass

cn = Chinese()

print cn.dwell

cn.think()

参数大于两个:

Super 函数

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def __init__(self,c):

print "Init..."

self.dwell = 'Earth'

def think(self):

print "I am a %s " % self.color

print ("I am a thinker")

class Chinese(People):

def __init__(self):

People.__init__(self,'red')

pass

cn = Chinese()

class A(object):

def __init__(self):

print "enter A"

print "leave A"

class B(object):

def __init__(self):

print "enter B"

super(B,self),__init__()

print "leave B"

b = B()

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def __init__(self,c):

print "Init..."

self.dwell = 'Earth'

def think(self):

print "I am a %s " % self.color

print ("I am a thinker")

class Chinese(People):

def __init__(self):

super(Chinese,self).__init__('red')

pass

cn = Chinese()

cn.think()

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def __init__(self,c):

print "Init..."

self.dwell = 'Earth'

def think(self):

print "I am a %s " % self.color

print ("I am a thinker")

class Chinese(People):

def __init__(self):

super(Chinese,self).__init__('red')

def talk(self):

print "I like taking."

cn = Chinese()

cn.think()

cn.talk()

多重继承

Python支持多重继承,第一个类可以继承多个父类

语法:

class class_name(Parent_c1,Parent_c2,...)

注意:

当父类中出现多个自定义的__init__的方法时,

多重继承,只执行第一个累的__init_方法,其他不执行。

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

color = 'yellow'

def __init__(self):

print "Init..."

self.dwell = 'Earth'

def think(self):

print "I am a %s " % self.color

print ("My home is %s ") % self.dwell

class Martian(object):

color = 'red'

def __init__(self):

self.dwell = 'Martian'

class Chinese(People,Martian):

def __init__(self):

People.__init__(self)

cn = Chinese()

cn.think()

#!/usr/bin/env python

#-*- coding:utf-8 -*-

class People(object):

def __init__(self):

self.dwell = 'Earth'

self.color = 'yellow'

def think(self):

print "I am a %s " % self.color

print ("My home is %s ") % self.dwell

class Martian(object):

color = 'red'

def __init__(self):

self.dwell = 'Martian'

def talk(self):

print "I like talking"

class Chinese(Martian,People):

def __init__(self):

People.__init__(self)

cn = Chinese()

cn.think()

cn.talk()

python类中的函数调用关系_Python中类的内置方法与继承关系实例相关推荐

  1. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

  2. python在匿名函数作和_python之路——内置函数和匿名函数

    阅读目录 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的 ...

  3. python基础30个常用代码大全-Python3列表内置方法大全及示例代码小结

    Python中的列表是简直可说是有容乃大,虽然看似类似C中的数组,但是Python列表可以接受任意的对象元素,比如,字符串,数字,布尔值,甚至列表,字典等等,自由度提升到一个新的高度,而Python也 ...

  4. Python入门学习-DAY27- isinstance与issubclass、反射、内置方法

    isinstance与issubclass issubclass:判断子类是否属于父类,是则返回True,否则返回False isinstance:判断对象是否属于类,是则返回True,否则返回Fal ...

  5. python 类成员变量是否存在_python中类变量与成员变量的使用注意点总结

    前言 最近在用python写一个项目,发现一个很恶心的bug,就是同由一个类生成的两个实例之间的数据竟然会相互影响,这让我非常不解.后来联想到java的类有类变量也有实例变量,因此翻阅了相关资料,发现 ...

  6. python内置方法怎么使用_python的常用内置方法

    __author__ = 'coco' ''' python内置函数 ''' # all() 全为真,才为真 print(all([0,-2,3])) # False print(all([1,-2, ...

  7. python slice start比end小_Python - lambda与内置函数

    lambda的表达式 对于简单的函数,也存在一种简便的表示方式,即:lambda表达式 #普通函数 def func(a): return a+1 print ('test1_func0:',func ...

  8. python22个字符串长度_python字符串处理内置方法一览表

    序号 方法及描述 1 capitalize() 将字符串的第一个字符转换为大写 2 center(width, fillchar) 返回一个指定的宽度 width 居中的字符串,fillchar 为填 ...

  9. python类的内置方法_python面向对象之类中的内置方法

    __setattr__,__delattr__,__getattr__,__getattribute__以及标准类型的二次加工 __setattr__,__delattr__,__getattr__的 ...

最新文章

  1. pypthon3精要(13)-变量命名规则之下划线
  2. Vue Vuex todo举例
  3. 第四章:Django 模型 —— 设计系统表
  4. 腾讯alloyteam团队前端代码规范(记录)
  5. ROS学习手记 - 5 理解ROS中的基本概念_Services and Parameters
  6. 数据结构与算法笔记(五) 链表的应用
  7. 一招生成定制版二次元人脸头像,还能“模仿”你的表情!
  8. Android kernel源码下载与编译
  9. JavaScript上传图片方式
  10. 龙芯2k1000-pmon(5)- pmon无法修改环境变量的问题
  11. 三星s7562刷android+2,三星s7562 4.1.2 rom刷机包(精简省电版)
  12. python爬虫爬取微信公众号的阅读数、喜爱数、文章标题和链接等信息
  13. 前端实现视频录制功能
  14. 2021-11-25【数据结构/严蔚敏】【Dijkstra】【代码实现算法7.15】
  15. 穿越火线老是卡在正在连接服务器,修复cf经常提示网络出现异常与服务器断开连接的方法...
  16. 2021年安全员-B证最新解析及安全员-B证考试平台
  17. 什么是3C认证?3C认证的介绍
  18. opencv cvRound函数cvClone
  19. 1,WLAN的基本概念
  20. 联想LSE存高危漏洞 重装系统也无法消除

热门文章

  1. EntityFramework 4.1 如何加入项目
  2. 电脑上玩 Google纵横
  3. 手把手·从零开始撸Docker 系列一
  4. 从零打造 Vue 聊天组件
  5. JS 基础知识点及常考面试题(二)
  6. LibLinear(SVM包)使用说明之(二)MATLAB接口
  7. java比较时间戳大小_JAVA中两个String类型的时间戳怎么样比较大小?
  8. MySQL模糊匹配查询LIKE,REGEXP,IN
  9. iis windows phpstudy安装redis扩展
  10. Homework 1_SQL Server中由于外键约束而删除数据失败