Python面向对象编程

简介: Color类从一个非常简单的类定义开始:class Color(object): '''An RGB color,with red,green,blue component''' pass关键字def用于告诉Python我们定义了一个新函数,关键字class则表明我们定义了一个新类。(object)这部分内容说的是,类Color是一种对象;文

Color类

从一个非常简单的类定义开始:

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

    pass

关键字def用于告诉Python我们定义了一个新函数,关键字class则表明我们定义了一个新类。(object)这部分内容说的是,类Color是一种对象;文档字符串描述了Color对象的功能,pass则说明该对象是空白的(即为存放任何数据,也没有提供任何的新操作)。使用方式如下:

>>> black = Color()
>>> black.red = 0
0
>>> black.green = 0
0
>>> black.blue = 0
0

方法

根据定义,颜色亮度就是其最强和最弱的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

小结

  • 在面向对象编程语言中,定义新类型的手段是:创建新类。类支持封装,换句话说,类将数据以及相关的操作放在一起,以便使程序的其他部分可以忽略相关的实现细节。
  • 类还支持多态。如果两个类拥有相同用法的方法,则他们的实例可以互相替换,且不会影响到程序的其他部分。这就意味着我们能够进行“即插即用型”编程,即代码能够根据所处理的具体类型而执行不同的操作。(多态的意思就是说,某个含有变量的表达式可以根据该变量所引用的对象的实际类型得到不同的结果。)
  • 新类还可以通过“继承现有类”的方式来进行定义:class child(parent)。系类不仅可以重写父类的功能特点,而且还可以添加新的功能特点。
  • 当方法被定义在类中时,其第一个参数必须是一个特殊的变量,该变量表示的是调用该方法的那个对象。按照惯例,这个参数成为self。
  • 在Python中,部分方法拥有着特殊的预定义含义:为了有所区别,他们的名称以双下划线开头和结尾。在这些方法中,有的在构造对象时调用(__init__),有的在将对象转换为字符串时调用(__str__和__repr__),有的则用于模拟算数运算(比如__add__和__sub__)。
目录
相关文章
|
4天前
|
存储 Java 程序员
30天拿下Python之面向对象编程
30天拿下Python之面向对象编程
|
5月前
|
Python
Python编程作业五:面向对象编程
Python编程作业五:面向对象编程
61 1
|
14天前
|
Java Python
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
【9月更文挑战第18天】在 Python 中,虽无明确的 `interface` 关键字,但可通过约定实现类似功能。接口主要规定了需实现的方法,不提供具体实现。抽象基类(ABC)则通过 `@abstractmethod` 装饰器定义抽象方法,子类必须实现这些方法。使用抽象基类可使继承结构更清晰、规范,并确保子类遵循指定的方法实现。然而,其使用应根据实际需求决定,避免过度设计导致代码复杂。
|
16天前
|
Python
全网最适合入门的面向对象编程教程:Python函数方法与接口-函数与方法的区别和lamda匿名函数
【9月更文挑战第15天】在 Python 中,函数与方法有所区别:函数是独立的代码块,可通过函数名直接调用,不依赖特定类或对象;方法则是与类或对象关联的函数,通常在类内部定义并通过对象调用。Lambda 函数是一种简洁的匿名函数定义方式,常用于简单的操作或作为其他函数的参数。根据需求,可选择使用函数、方法或 lambda 函数来实现代码逻辑。
|
5月前
|
Python
【Python进阶(三)】——面向对象编程
【Python进阶(三)】——面向对象编程
|
2月前
|
Python
Python 中的面向对象编程 (OOP)
【8月更文挑战第29天】
25 7
|
2月前
|
机器学习/深度学习 PHP 开发者
探索PHP中的面向对象编程构建你的首个机器学习模型:以Python和scikit-learn为例
【8月更文挑战第30天】在PHP的世界中,面向对象编程(OOP)是一块基石,它让代码更加模块化、易于管理和维护。本文将深入探讨PHP中面向对象的魔法,从类和对象的定义开始,到继承、多态性、封装等核心概念,再到实战中如何应用这些理念来构建更健壮的应用。我们将通过示例代码,一起见证PHP中OOP的魔力,并理解其背后的设计哲学。
|
2月前
|
Python
|
2月前
|
存储 Java C#
详解 Python 中的面向对象编程(2)
详解 Python 中的面向对象编程(2)
39 10
|
2月前
|
存储 程序员 索引
详解 Python 中的面向对象编程(1)
详解 Python 中的面向对象编程(1)
31 2
下一篇
无影云桌面