3.0 类的内置方法
python 内部类
所谓内部类,就是在类的内部定义的累,主要目的是为了更好的抽象显示世界。
例子:
汽车是个类,汽车的底盘,轮胎也可以抽象为类,将其定义到汽车类中,则形成内部类,更好的描述汽车类,因为底盘、轮胎是汽车的一部分。
内部类的实例化方法
- 方法1:直接使用外部类调用内部类
object_name=outclass_name.inclass_name()
- 方法2:先对外部类进行实例化,然后再实例化内部类
out_name = outclass_name()
in_name = outname.inclass_name()
in_name.method()
案例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/12/5 0005 22:35
# @Author : Jason_Yan
# @File : lei.py
class People(object):
color = 'yellow'
__age = 30
class Chinese(object):
name = "I am Chinese"
def think(self):
self.color = "black"
print("I an a %s" % self.color)
print("I am a thinker")
print(self.__age)
def __talk(self):
print("I am talking wiht Tom")
@classmethod
def test(self):
print('this is class method')
@staticmethod
def test1():
print("this is static method")
#jack = People.Chinese() #1、直接外部类调用内部类
#print(jack.name)
#ren = People() #2、通过先实例化外部类
#jack = ren.Chinese() #再实例化内部类
#print(jack.name)
print(People.Chinese.name) #直接实例化,就是1方法
print(People.Chinese().name) #通过方法进行访问
类的内置方法(魔术方法)
str(self)
构造函数与析构函数
构造函数:
- 用于初始化类的内部状态,python提供的构造函数是__init__();
- __init__方法是可选的,如果不提供,python会给出一个默认的__init__方法
析构函数:
- 用于施放对象占用的资源,python提供的析构函数是__del__();
- del()也是可选的,如果不提供,则python会在后台提供默认的析构函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/12/5 0005 22:35
# @Author : Jason_Yan
# @File : lei.py
class People(object):
color = 'yellow'
__age = 30
class Chinese(object):
name = "I am Chinese"
def __str__(self):
return "This is People class" #这个里面不能使用print,只能使用return
def __init__(self,c='wtih'): #为这个函数设置一个默认值
# self.color = 'black'
self.color = c
#self.think() 这里也可以加入方法,相当于直接执行了think方法,因为他们将它加入到了初始化里面
def think(self):
self.color = "black"
print("I an a %s" % self.color)
print("I am a thinker")
print(self.__age)
def __talk(self):
print("I am talking wiht Tom")
@classmethod
def test(self):
print('this is class method')
@staticmethod
def test1():
print("this is static method")
ren = People('green') #这里给类传一个值
ren1 = People()
# print(ren.__str__()) #不写.__str__也会默认调用
print(ren.color) #这里就会使用实例化对象的值
print(ren1.color) #通过对象访问的时候,就会使用初始化的值
print(People.color) #通过类进行访问,值还是默认的值
垃圾回收机制
-
python采用垃圾回收机制来清理不再使用的对象;python提供gc模块施放不再使用的对象
-
python采用“引用计数”的算法方式来处理回收,即当某个对象在其作用域内不再被其他对象引用的时候,python就会自动清除对象;
-
gc模块的collect()可以一次性收集所有待处理的对象(gc.collect)
3.1 类的继承(1)
继承是面向对象的重要特性之一;
-
继承关系:继承是相对类而言的父子关系,子类继承了父类的所有公有属性和方法
-
继承实现了代码重用
使用继承
继承可以重用已经存在的数据和行为,减少代码的重复编写。python在类名后使用一对括号来表示继承关系,括号中的类即为父类。
class Myclass(ParentClass)
如果父类定义了__int__方法,子类必须显示调用父类的__init__方法:
- ParentClass.init(self,[args...])
- 如果之类需要扩展父类的行为,可以添加__init__方法的参数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/12/5 0005 22:35
# @Author : Jason_Yan
# @File : lei.py
class People(object):
color = 'yellow'
def __init__(self,c): #假设这里定义了两个参数,需要在子类函数里面显式的调用
print("Init...")
self.dwell = 'Earth'
def think(self):
self.color = "black"
print("I an a %s" % self.color)
print("I am a thinker")
class Chinese(People): #这里为继承父类,所以必须表名父类
def __init__(self): #显式的调用方法
People.__init__(self,'red') #
pass
#cn = Chinese() #实例化子类函数
#print(cn.color) #调用父类函数里面的方法
#cn.think()
#print(cn.dwell) #这里也能访问到__init__方法
cn = Chinese() #显式的调用__init__方法以后这里才会有结果,不然会报错
super函数(内置函数)也可以继承父类
第一种格式:
class Chinese(People): #这里为继承父类,所以必须表名父类
def __init__(self): #显式的调用方法
super(Chinese,self).__init__('red') #使用super函数继承父类函数
pass
cn = Chinese()
cn.talk()
第二种格式
class Chinese(People): #这里为继承父类,所以必须表名父类
def __init__(self): #显式的调用方法
People.__init__(self,'red')
def talk(self):
print("I lie talking")
cn = Chinese()
cn.talk()
如果子类方法的名字和父类的冲突,那么调用方法的时候将会调用自己的方法
3.2 类的继承(2)
多重继承
Python支持多重继承,即一个类可以继承多个父类:
语法:
cl```ass class_name(Parent_c1,Parent_c2,....)
注意:
当父类总出现多个自定义的__init__方法时,多重继承只执行第一个类的__init__方法,其他的不执行
class People(object):
color = 'yellow'
def __init__(self):
print("Init...")
self.dwell = 'Earth'
def think(self):
print("I an a %s" % self.color)
print("My home is %s" % self.dwell)
class Martin(object):
color = 'red'
def __init__(self):
self.dwell="Martian"
class Chinese(Martin, People):
def __init__(self):
People.__init__(self)
def talk(self):
print("I lie talking")
cn = Chinese()
cn.think()
多重继承,谁写在子类里面的第一位,就优先使用谁的方法
<>
很多时候,我们只需要继承一个类就行了。