亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 面向对象编程完全指南:从基础到进阶

Python 从诞生起就是面向对象语言,创建和使用类、对象非常容易。本章助你精通 Python OOP 支持。

若无 OOP 经验,建议先阅读入门教程掌握基本概念。下面简要介绍 OOP 术语:

OOP 术语概览

创建类

class 语句创建新类定义,类名紧跟关键字 class 后加冒号:

class ClassName:
    'Optional class documentation string'
    class_suite

示例

class Employee:
    'Common base class for all employees'
    empCount = 0
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
    def displayCount(self):
        print("Total Employee %d" % Employee.empCount)
    def displayEmployee(self):
        print("Name : ", self.name, ", Salary: ", self.salary)

创建实例对象

调用类名并传入 __init__ 接受的参数即可创建实例:

emp1 = Employee("Zara", 2000)
emp2 = Employee("Manni", 5000)

访问属性

用点号访问对象属性,类变量用类名访问:

emp1.displayEmployee()
emp2.displayEmployee()
print("Total Employee %d" % Employee.empCount)

完整示例:

#!/usr/bin/python
class Employee:
    'Common base class for all employees'
    empCount = 0
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
    def displayCount(self):
        print("Total Employee %d" % Employee.empCount)
    def displayEmployee(self):
        print("Name : ", self.name, ", Salary: ", self.salary)
emp1 = Employee("Zara", 2000)
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print("Total Employee %d" % Employee.empCount)

输出:

Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2

可随时动态增删改属性:

emp1.age = 7   # 添加属性
emp1.age = 8   # 修改属性
del emp1.age   # 删除属性

也可用内置函数操作属性:

hasattr(emp1, 'age')
getattr(emp1, 'age')
setattr(emp1, 'age', 8)
delattr(emp1, 'age')

内置类属性

每个 Python 类都有以下内置属性,可像普通属性一样通过点号访问:

访问示例:

#!/usr/bin/python
class Employee:
    'Common base class for all employees'
    empCount = 0
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
    def displayCount(self):
        print("Total Employee %d" % Employee.empCount)
    def displayEmployee(self):
        print("Name : ", self.name, ", Salary: ", self.salary)
print("Employee.__doc__:", Employee.__doc__)
print("Employee.__name__:", Employee.__name__)
print("Employee.__module__:", Employee.__module__)
print("Employee.__bases__:", Employee.__bases__)
print("Employee.__dict__:", Employee.__dict__)

输出:

Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount': , 'empCount': 2, 'displayEmployee': , '__doc__': 'Common base class for all employees', '__init__': }

销毁对象(垃圾回收)

Python 自动删除无用对象(内置类型或类实例)释放内存。垃圾回收器在程序运行期间周期性回收引用计数归零的内存块。

对象引用计数随别名数量变化:赋新名、放入容器(列表、元组、字典)计数增 1;del 删除、引用重赋值、引用超出作用域计数减 1。计数归零时自动回收。

a = 40      # 创建对象 <40>
b = a       # 增加 <40> 引用计数
c = [b]     # 增加 <40> 引用计数
del a       # 减少 <40> 引用计数
b = 100     # 减少 <40> 引用计数
c[0] = -1   # 减少 <40> 引用计数

通常不会注意到垃圾回收器销毁孤儿实例。但类可实现 __del__() 析构器,在实例即将销毁时调用,用于清理非内存资源。

示例

#!/usr/bin/python
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
    def __del__(self):
        class_name = self.__class__.__name__
        print(class_name, "destroyed")
pt1 = Point()
pt2 = pt1
pt3 = pt1
print(id(pt1), id(pt2), id(pt3))
del pt1
del pt2
del pt3

输出:

3083401324 3083401324 3083401324
Point destroyed

注:最好在单独文件定义类,主程序用 import 导入。

类继承

通过在新类名后括号列出父类实现继承。子类继承父类属性,可像自有属性一样使用,也可覆盖数据成员和方法。

语法

class SubClassName (ParentClass1[, ParentClass2, ...]):
    'Optional class documentation string'
    class_suite

示例

#!/usr/bin/python
class Parent:
    parentAttr = 100
    def __init__(self):
        print("Calling parent constructor")
    def parentMethod(self):
        print('Calling parent method')
    def setAttr(self, attr):
        Parent.parentAttr = attr
    def getAttr(self):
        print("Parent attribute :", Parent.parentAttr)
class Child(Parent):
    def __init__(self):
        print("Calling child constructor")
    def childMethod(self):
        print('Calling child method')
c = Child()
c.childMethod()
c.parentMethod()
c.setAttr(200)
c.getAttr()

输出:

Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200

多重继承示例:

class A: ...
class B: ...
class C(A, B): ...

用 issubclass() 或 isinstance() 检查类/实例关系:

重写方法

子类可随时重写父类方法,以实现特殊功能。

示例

#!/usr/bin/python
class Parent:
    def myMethod(self):
        print('Calling parent method')
class Child(Parent):
    def myMethod(self):
        print('Calling child method')
c = Child()
c.myMethod()

输出:

Calling child method

基础重载方法

下表列出可在自定义类中重写的通用功能:

序号方法、说明及调用示例
1__init__(self [,args...]):构造器。调用:obj = ClassName(args)
2__del__(self):析构器。调用:del obj
3__repr__(self):可求值字符串表示。调用:repr(obj)
4__str__(self):可打印字符串表示。调用:str(obj)
5__cmp__(self, x):对象比较。调用:cmp(obj, x)

运算符重载

自定义 Vector 类表示二维向量,直接用 + 相加会报错。定义 __add__ 方法实现向量加法,+ 即按预期工作:

示例

#!/usr/bin/python
class Vector:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def __str__(self):
        return 'Vector (%d, %d)' % (self.a, self.b)
    def __add__(self, other):
        return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2, 10)
v2 = Vector(5, -2)
print(v1 + v2)

输出:

Vector(7,8)

数据隐藏

属性名前加双下划线前缀,外部不可见。

示例

#!/usr/bin/python
class JustCounter:
    __secretCount = 0
    def count(self):
        self.__secretCount += 1
        print(self.__secretCount)
counter = JustCounter()
counter.count()
counter.count()
print(counter.__secretCount)

输出:

1
2
Traceback (most recent call last):
  File "test.py", line 12, in 
    print(counter.__secretCount)
AttributeError: JustCounter instance has no attribute '__secretCount'

Python 内部将名称改为包含类名。可通过 object._ClassName__attrName 访问。将最后一行改为:

print(counter._JustCounter__secretCount)

输出:

1
2
2

Python

  1. 树莓派 OpenCV 自动视觉跟踪:云台舵机实时锁定彩色目标
  2. C# 类与对象:面向对象编程的基石
  3. 精通Python数据类型:实战指南
  4. 精通Python运算符:全面指南
  5. 精通Python Pass语句:实战指南
  6. 精通 Python 字典:创建、操作与进阶技巧
  7. 精通 Python 自定义异常:实战指南
  8. 精通 Python 面向对象编程:类、继承、封装与多态
  9. 精通 Python 继承:概念、语法与实战示例
  10. 理解 Python type() 与 isinstance():实战示例详解
  11. Java 单例模式详解:安全实现与使用指南
  12. 精通 Java:理解对象、类与核心 OOP 概念