Python物件導向編程

來源:互聯網
上載者:User

標籤:python

Color類

從一個非常簡單的類定義開始:

class Color(object):    ‘‘‘An RGB color,with red,green,blue component‘‘‘    pass

關鍵字def用於告訴Python我們定義了一個新函數,關鍵字class則表明我們定義了一個新類。(object)這部分內容說的是,類Color是一種對象;文檔字串描述了Color對象的功能,pass則說明該對象是空白的(即為存放任何資料,也沒有提供任何的新操作)。使用方式如下:

>>> black = Color()>>> black.red = 00>>> black.green = 00>>> black.blue = 00
方法

根據定義,顏色亮度就是其最強和最弱的RGB值得平均值對應到0和1之間的那個數值。若寫成函數,如下所示:

def lightness(color):    ‘‘‘Return the lightness of color.‘‘‘    strongest = max(color.red, color.green, color.blue)    weakest = min(color.red, color.green, color.blue)    return 0.5 * (strongest + weakest) / 255

若將函數lightness()作為Color類的一個方法,如下所示:

class Color(object):    ‘‘‘An RGB color,with red,green,blue component‘‘‘    def lightness(self):        ‘‘‘Return the lightness of color.‘‘‘        strongest = max(self.red, self.green, self.blue)        weakest = min(self.red, self.green, self.blue)        return 0.5 * (strongest + weakest) / 255

需要移除參數color,並將其替換成self參數。當Python在調用某個對象中的某個方法時,會自動將該對象的引用作為該方法的第一個參數傳進去。這就意味著,當我們調用lightness時,完全不需要給它傳遞任何參數。使用方式如下:

>>> purple = Color()>>> purple.red = 255>>> purple.green = 0>>> purple.blue = 255>>> purple.lightness()0.5

定義一個方法的時候,除了實際需要傳入的那些參數之外,還必須再多寫一個。相反,在調用某個方法的時候,實際提供的參數要比該方法定義中所需的少一個。

構造器

給Color類添加一個當建立新Color的時候就會被執行的方法。這種方法叫做構造器(constructor);在Python中,構造器就是__init__:

class Color(object):    ‘‘‘An RGB color,with red,green,blue component‘‘‘    def __init__(self, r, g, b):        ‘‘‘A new color with red value r, green value g, and blue value b. All        components are integers in the range 0-255.‘‘‘        self.red = r        self.green = g        self.blue = b

名稱兩邊的雙底線說明該方法對Python有著特殊的意義——這裡的意思就是說,建立新對象的時候,該方法就會被調用。

purple = Color(128, 0, 128)
特殊方法

當需要從某個對象得到一段簡單易懂的資訊時,就會調用__str__;當需要這段資訊更準確時,則會調用__repr__。在使用print時,__str__就會被調用。為了得到有意義的輸出,現在來寫個Color.__str__:

class Color(object):    ‘‘‘An RGB color,with red,green,blue component‘‘‘    def __init__(self, r, g, b):        ‘‘‘A new color with red value r, green value g, and blue value b. All        components are integers in the range 0-255.‘‘‘        self.red = r        self.green = g        self.blue = b    def __str__(self):        ‘‘‘Return a string representation of this Color in the form of an RGB tuple.‘‘‘        return‘(%s, %s, %s)‘ %(self.red, self.green, self.blue)

Python中還有很多特殊方法:Python的官方網站上給出了完成的列表。其中就有__add__、__sub__、__eq__等,它們分別在我們用“+”對對象做加法,用“-”對對象做減法、用“==”對對象做比較的時候調用:

class Color(object):    ‘‘‘An RGB color,with red,green,blue component‘‘‘    def __init__(self, r, g, b):        ‘‘‘A new color with red value r, green value g, and blue value b. All        components are integers in the range 0-255.‘‘‘        self.red = r        self.green = g        self.blue = b    def __str__(self):        ‘‘‘Return a string representation of this Color in the form of an RGB tuple.‘‘‘        return‘(%s, %s, %s)‘ %(self.red, self.green, self.blue)    def __add__(self, other_color):        ‘‘‘Return a new Color made from adding the red, green and blue components        of this Color to Color other_color‘s components. If the sum is greater than        255, the color is set to 255‘‘‘        return Color(min(self.red + other_color.red, 255),                     min(self.green + other_color.green, 255),                     min(self.blue + other_color.blue, 255))    def __sub__(self, other_color):        ‘‘‘Return a new Color made from subtracting the red, green and blue components        of this Color to Color other_color‘s components. If the difference is less than         255, the color is set to 0‘‘‘        return Color(min(self.red - other_color.red, 0),                     min(self.green - other_color.green, 0),                     min(self.blue - other_color.blue, 0))    def __eq__(self, other_color):        ‘‘‘Return True if this Color‘s components are equal to Color other_color‘s components.‘‘‘        return self.red == other_color.red and self.green == other_color.green             and self.blue == other_color.blue    def lightness(self):        ‘‘‘Return the lightness of color.‘‘‘        strongest = max(self.red, self.green, self.blue)        weakest = min(self.red, self.green, self.blue)        return 0.5 * (strongest + weakest) / 255

這些方法的具體用法:

purple = Color(128, 0, 128)white = Color(255, 255, 255)dark_grey = Color(50, 50, 50)print(purple + dark_grey)print(white - dark_grey)print(white == Color(255, 255, 255))

可以使用help(Color)擷取有關Color類的協助資訊:

Help on Color in module __main__ object:class Color(builtins.object) |  An RGB color,with red,green,blue component |   |  Methods defined here: |   |  __add__(self, other_color) |      Return a new Color made from adding the red, green and blue components |      of this Color to Color other_color‘s components. If the sum is greater than |      255, the color is set to 255 |   |  __eq__(self, other_color) |      Return True if this Color‘s components are equal to Color other_color‘s components. |   |  __init__(self, r, g, b) |      A new color with red value r, green value g, and blue value b. All |      components are integers in the range 0-255. |   |  __str__(self) |      Return a string representation of this Color in the form of an RGB tuple. |   |  __sub__(self, other_color) |      Return a new Color made from subtracting the red, green and blue components |      of this Color to Color other_color‘s components. If the difference is less than  |      255, the color is set to 0 |   |  lightness(self) |      Return the lightness of color. |   |  ---------------------------------------------------------------------- |  Data descriptors defined here: |   |  __dict__ |      dictionary for instance variables (if defined) |   |  __weakref__ |      list of weak references to the object (if defined) |   |  ---------------------------------------------------------------------- |  Data and other attributes defined here: |   |  __hash__ = None

Python物件導向編程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.