1、对象的创建
创建对象的过程称之为实例化;当一个对象被创建后,包含三个方面的特性:对象句柄、属性和方法。
句柄用于区分不同的对象
对象的属性和方法与类中的成员变量和成员函数对应
obj=MyClass() //创建类的一个实例(对象)通过对象来调用方法和属性
2、类的属性
类的属性按使用范围:公有属性和私有属性,
类的属性范围取决于属性的名称。
公有属性:在类中和类外都能调用属性。
私有属性:不能在类外及被类意外的函数调用。
定义方式:以“_”双下划线开始的成员变量就是私有属性
私有属性,可以通过instance._classname_attribute方式访问。
内置属性:有系统在定义类的时候默认添加的,由前后双下划线构成,dict,module.
#!/usr/bin/env python
#-*-coding:utf-8-*-
#@Time : 2018/1/11 15:48
#!@Auther:bestsky
#!@File :demon1-class attributes.py
class People(object):
color = "yellow"
__age = 30 #私有属性
def think(self):
self.color = "black"
print ("I am a %s" %self.color)
# print self.__age
ren = People()
print ren
print ren.color
ren.think()
print ren._People__age #私有属性调用方法,建议:仅程序测试使用
3、类的方法
方法的定义和函数一样,但是需要self作为第一个参数
- 类的方法为:
- 公有方法
公有方法:在类中和类外都能调用的方法 - 私有方法
私有方法:不能被类外部调用,在方法前面加上“__“双下划线就是私有方法; - 类方法
类方法:被classmethod()函数处理过的函数,能被类所调用,也能被对象所调用(是继承的关系) - 静态方法
静态方法:相当于“全局函数”,可以被类直接调用,可以被所有实例化对象共享,通过staticmethod()定义, 静态方法没有“self”参数。
- 公有方法
self参数
用于区分函数和类的方法(必须有一个self),self参数表示执行对象本身。
装饰器:
- @classmethod
- @staticmethod
#!/usr/bin/env python
#-*-coding:utf-8-*-
#@Time : 2018/1/11 17:39
#!@Auther:bestsky
#!@File :demon2-类的方法.py
class People(object):
color = "yellow"
__age = 30 #私有属性
def think(self):
self.color = "black"
print ("I am a %s" %self.color)
print self.__age
def __talk(self):
print("I am talking with Tom")
# def test(self):
# print self.color
@classmethod #装饰器,只对下面的一个函数起作用
def test(self):
# print("this is func")
print("this is class method")
# print People.color #访问类里面其它成员
# cm = classmethod(test)
# sm = staticmethod(test) #静态方法的,需要通过函数后才行
@staticmethod #装饰器,只对下面的一个函数起作用
def test1():
print ("this is static method")
David = People()
# People.cm()
# People.sm()
People.test()
People.test1()
4、python内部类
所谓的内部类,就是在类的内部定义的类(类的嵌套),主要目的是为了更好的抽象现实世界。
内部类的实例化方法
方法1:直接使用外部类调用内部类
object_name=outclass_name.inclass_name()例如:
jack=People.Chinese() #People是外部类,Chinese是内部类方法2:先对外部类进行实例化,然后在实例化内部类
out_name=outclass_name()
in_name=out_name.inclass_name()
in_name.method()例如:
ren=People() #People是外部类
jack=ren.Chinese() #Chinese是内部类
print jack.namestr(self)
构造函数与析构函数
构造函数
用于初始化类的内部状态,Python提供的构造函数是init();
init()方法是可选的,如果不提供,Python会给出一个默认的的init方法析构函数
用于释放对象占用的资源,Python提供的析构函数是del();
del()也是可选的,如果不提供,则Python会在后台提供默认析构函数
垃圾回收机制
Python采用垃圾回收机制来清理不再使用的对象;Python提供gc模块释放不再使用的对象。
Python采用“引用计数”的算法方式来处理回收,即:当某个对象在其作用域内不再被其它对象引用的时候,Python就自动清除对象;
gc模块的collect()可以一次性收集所有待处理的对象(gc.collect)
#!/usr/bin/env python
#-*-coding:utf-8-*-
#@Time : 2018/1/14 22:04
#!@Auther:bestsky
#!@File :demon3-类的内置.py
import gc
class People(object):
color = 'yellow'
__age = 20
class Chinese(object):
name = "I am chinese"
def __str__(self):
return "This is People class"
def __init__(self): #构造函数
self.color = 'black'
def __del__(self): #回收,脚本执行的最后执行
print "Del……" #显示执行顺序
self.fd.close()
# ren = People()
# # print ren #跟下一句执行结果一样
# # print ren.__str__()
# print ren.color #通过对象访问,会根据传入值变化
# print People.color #通过类访问,是类定义的不会变
print gc.collect() #数字0表示,没有可回收的;需要引入gc模块
jack = People()
print jack.color
print People.color
print 'Main end'