歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python面向對象編程學習筆記

Python面向對象編程學習筆記

日期:2017/3/1 9:34:39   编辑:Linux編程

一、類和對向

面向過程和面向對象的編程

面向過程的編程:函數式編程,C程序等

面向對象的編程:C++,JAVA,Python等

類和對象:是面向對象中的2個重要概念

類:是事物的抽象,比如汽車;

對象:是類的一個實例,比如QQ汽車,大客車。

范例說明:

汽車模型可以對汽車的特征和行為進行抽象,然後可以實例化為一台真實的汽車實體出來。

二、Python類定義

Python類的定義使用class關鍵字定義一個類,且類名的首字母要大寫;

當程序員創建的類型不能用簡單類型表示時,就要創建類;

類把需要的變量和函數組合在一起,這種包含也稱為“封裝”。

Python類的結構:

class 類名:
    成員變量
    成員函數

類的方法中至少有一個參數self

對象的創建

創建對象的過程稱之為實例化;當一個對象被創建後,包含3個方面的特性:對象的句柄,屬性和方法。

句柄(對象的名字)用於區分不同的對象。

對象的屬性和方法與類中的成員變量和成員函數相對應。

----------------------------------------

方法

公共方法

私有方法

類方法

靜態方法

#!usr/bin/python
#coding:utf8

class Milo():
    name = 'csvt'

    def fun1(self):
        print self.name
        print 'public'
        self.__fun2()
    def __fun2(self):
        print 'private'

    @classmethod
    def classFun(self):
        print 'class'

    @staticmethod
    def staticFun(self):
        print 'static'

zou = Milo()
print Milo.name
Milo.classFun()

----------------------------------------

一、內部類

內部類就是在類的內部定義的類,主要目的是為了更好的抽象現實世界。

二、魔術方法(構造函數和析構函數)

 1 #!usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     class Test():
 6         var1 = "neibulei"
 7     name = 'csvt'
 8 
 9     def __init__(self,n = 'baby'):
10         self.name = n
11         print "initializing......"
12 
13     def fun1(self):
14         print self.name
15         print 'public'
16         self.__fun2()
17     def __fun2(self):
18         print 'private'
19 
20     @classmethod
21     def classFun(self):
22         print 'class'
23 
24     @staticmethod
25     def staticFun(self):
26         print 'static'
27 
28     def __del__(self):
29         print 'releasing sources......'
30 
31 zou = Milo()

三、垃圾回收機制

Python采用垃圾回收機制清理不再使用的對象;

Python提供gc模塊釋放不再使用的對象;

Python采用“引用計數”的算法方式來處理回收,即:當某個對象在其作用域內不再被其他對象引用的時候,Python就自動清除對象;

Python的函數collect()可以一次性收集所有待處理的對象(gc.collect())。

--------------------------------------分割線 --------------------------------------

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved