Python學習筆記五(物件導向-類-方法-屬性)

來源:互聯網
上載者:User

這裡所列的都是從C#的角度來看的,可能覺得不是很恰當。但是對於抽象的概念,更方便理解。

函數的定義
class
Python中的類沒有什麼public、private、protect

建構函式、解構函式
__init__(self)
__del__(self)

類的靜態變數
class Student
  name="abc"
這東西其實就是相當於C#中的靜態變數,但這裡要注意是,初始化類的靜態變數是這樣的(DiveIntoPython中的例子)
class counter:
    count = 0                    
    def __init__(self):
      self.__class__.count += 1

執行個體的成員變數
class Student
  def __init__(self)
      self.name = "abc"

屬性定義(類從object繼承,但好像不是必須的,因為下面的代碼也可以正常使用)
class Student:
    def __init__(self):
        self.__name="abc"
    def GetName(self):
        return self.__name
    def SetName(self,value):
        self.__name = value
    def DelName(self):
        del self.__name
    Name = property(GetName,SetName,DelName,'Student name')
說實話,當我寫完這段代碼的時候,我就不想使用屬性了。這樣定義起來也太不方便了,還要從object中繼承。而且現在C#中,可以很簡單的通過get、set來實現屬性了。目前沒有找到好的理由使用屬性

唯讀屬性(類必須從object繼承,否則就不是唯讀)
class Parrot(object):
    def __init__(self):
        self._voltage = 100000
    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

私人變數
class Student:
    def __init__(self):
        self.__name="abc"
很簡單就是通過__ 兩個底線開頭,但不是結尾的。就是私人了

私人方法
class Student:
    def __Getage(self):
        pass
和私人的變數一樣,你可以嘗試一下直接調用,編譯器會有相應的提示

強制訪問私人方法、變數
"私人"事實上這隻是一種規則,我們依然可以用特殊的文法來訪問私人成員。
上面的方法,我們就可以通過_類名來訪問
aobj = Student()
aobj._Student__name
aobj._Student__Getage()

靜態方法
class Class1: 
    @staticmethod 
    def test(): 
      print "In Static method..." 

方法重載
python是不支援方法重載,但是你代碼了可以寫。Python會使用位置在最後的一個。我覺得這可能以Python儲存這些資訊通過__dict__ 有關,它就是一個Dict。key是不能相同的。所以也就沒辦法出現兩個GoGo 方法調用

class Student:
    def GoGo(self,name):
        print name
    def GoGo(self):
        print "default"

調用的時候你只能使用 obj.GoGo()這個方法。

一些特殊方法

__init__(self)  建構函式
__del__ (self)  解構函式
__repr__( self)   repr()
__str__( self)    print語句 或 str()

運算子多載
__lt__( self, other)
__le__( self, other)
__eq__( self, other)
__ne__( self, other)
__gt__( self, other)
__ge__( self, other)

這東西太多了。大家還是自己看Python內建協助吧。

一些特殊屬性
當你定義一個類和調用類的執行個體時可以獲得的一些預設屬性
class Student:
    '''this test class'''
    name = 'ss'
    def __init__(self):
        self.name='bb'
    def Run(self):
        '''people Run'''
    @staticmethod 
    def RunStatic(): 
        print "In Static method..."
print Student.__dict__ #類的成員資訊
print Student.__doc__  #類的說明
print Student.__name__ #類的名稱
print Student.__module__ #類所在的模組
print Student.__bases__ #類的繼承資訊
obj = Student()

print dir(obj)
print obj.__dict__ #執行個體的成員變數資訊(不太理解Python的這個模型,為什麼Run這個函數確不再dict中)
print obj.__doc__  #執行個體的說明
print obj.__module__ #執行個體所在的模組
print obj.__class__ #執行個體所在的類名

參考文章:

http://www.xwy2.com/article.asp?id=119

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.