面向对象OOP:object oriented programming
Python 是动态语言,解释执行,运行速度慢;
文件名可以是同名不同包
包/模块/ 比如:p1/util.py p2/util.py
包下面必须有__init__.py 这样才能把包当成目录处理
Python的网络服务器有TCPServer、UDPServer、UnixStreamServer、UnixDatagramServer,而服务器运行模式有 多进程ForkingMixin 和 多线程ThreadingMixin两种。
Python学习OOP
一.基本介绍
OOP基本思想:类和实例
1.类:程序员 (一个群体)
对象:你,我。。
属性:姓名,性别,年龄。。
功能:写代码,修电脑。。(隐藏实现细节–封装)
实例的方法就是在类中定义的函数,它的第一个参数永远是 self,指向调用该方法的实例本身,其他参数和一个普通函数是完全一样的。
实例的属性优先级高于类属性的优先级
2.oop:继承封装和多态
3.继承:python可以实现多继承
程序员:
前端程序员
后端程序员:python程序员,Java程序员
球迷python程序员:python程序员+足球迷
二.Python定义类
类组成:属性+功能
class ClassName(object):
#class statement
Pass
//构造函数 设置类的属性
def __init__(self):
pass
//析构函数: 销毁对象的时候调用的,即垃圾回收的时候被调用
def __del__(self):
pass
两个内建函数:
dir()//返回对象的属性
type()//获取对象的类型
新式类和旧式类的区别:
class OldStyle:
def __init__(self,name,description):
self.name=name
self.description=description
class NewStyle(object):
def __init__(self,name,description):
self.name=name
self.description=description
if __name__=='__main__':
old=OldStyle('old','Old style class')
print old
print type(old)
print dir(old)
print '-------------'
new=NewStyle('new','New style class')
print new
print type(new)
print dir(new)
输出:
<__main__.OldStyle instance at 0x7f1242ae7488>
<type 'instance'>
['__doc__', '__init__', '__module__', 'description', 'name']
-------------
<__main__.NewStyle object at 0x7f1242adeb90>
<class '__main__.NewStyle'>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex_