python类用法(三)

简介: python类用法(三)

python类用法(三)

、类的多态性

多态性是面向对象编程的三大特性之一,它指的是不同的对象对同一消息做出不同的响应。在Python中,由于它是动态类型语言,多态性自然地被支持。

python复制代码

 

class Shape:

 

def area(self):

 

pass 

 

 

 

class Circle(Shape):

 

def __init__(self, radius):

 

self.radius = radius

 

 

 

def area(self):

 

return 3.14 * self.radius ** 2 

 

 

 

class Rectangle(Shape):

 

def __init__(self, width, height):

 

self.width = width

 

self.height = height

 

 

 

def area(self):

 

return self.width * self.height

 

 

 

def calculate_area(shape):

 

return shape.area() # 多态性体现在这里,无论是Circle还是Rectangle对象,都可以调用area方法

 

 

 

circle = Circle(5)

 

rectangle = Rectangle(4, 6)

 

print(calculate_area(circle)) # 输出圆的面积

 

print(calculate_area(rectangle)) # 输出矩形的面积

、元类与类的创建

元类(Metaclass)是创建类的“类”。在Python中,类本身也是对象,元类就是用来创建这些类对象的类。通常情况下,我们不需要直接操作元类,但在某些高级用法,如自动注册类、控制类的创建等场景中,元类非常有用。

python复制代码

 

class Meta(type):

 

def __init__(cls, name, bases, dct):

 

print(f"Creating class {name}")

 

super().__init__(name, bases, dct)

 

# 在这里可以对类进行额外操作,如注册、修改属性等

 

 

 

class MyClass(metaclass=Meta):

 

pass 

 

 

 

# 当MyClass被定义时,会触发Meta类的__init__方法,输出:Creating class MyClass

、属性装饰器

Python还提供了几个内置的属性装饰器,如@property@classmethod@staticmethod,它们用于改变方法的行为,使其表现得像数据属性、类方法或静态方法。

1. @property

将方法转换为只读属性。

python复制代码

 

class Circle:

 

def __init__(self, radius):

 

self._radius = radius

 

 

 

@property

 

def radius(self):

 

return self._radius

 

 

 

@radius.setter

 

def radius(self, value):

 

if value < 0:

 

raise ValueError("Radius cannot be negative")

 

self._radius = value

 

 

 

circle = Circle(5)

 

print(circle.radius) # 读取属性值

 

circle.radius = 10 # 设置属性值

2. @classmethod

将方法绑定到类而不是实例,不需要实例即可调用,且第一个参数约定为表示类本身的cls

python复制代码

 

class MyClass:

 

@classmethod

 

def class_method(cls):

 

print(f"This is a class method of {cls.__name__}")

 

 

 

MyClass.class_method() # 调用类方法

3. @staticmethod

将方法定义为静态方法,即方法既不与类关联也不与实例关联,只是放在类命名空间中的一个普通函数。

python复制代码

 

class MyClass:

 

@staticmethod

 

def static_method():

 

print("This is a static method")

 

 

 

MyClass.static_method() # 调用静态方法

 

 

目录
相关文章
|
3天前
|
Python
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
14 0
|
10天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
53 0
|
10天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
33 0
|
12天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
18天前
|
Python
python中threads.append的用法
将线程对象`t`添加到`threads`列表便于管理与控制线程,如等待所有线程完成。通过迭代列表并调用`join`方法,可依次等待每个线程执行完毕,实现同步。代码示例: ```python for t in threads: t.join() print(&quot;All threads are done!&quot;) ``` `join`方法使当前线程阻塞,直到线程执行结束。所有线程完成后,输出&quot;All threads are done!&quot;。
15 1
|
1月前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
19 0
|
1月前
|
Python
Python类定义:从小白到专家的旅程
Python类定义:从小白到专家的旅程
8 0
|
1月前
|
Python
Python类与对象:深入解析与应用
本文介绍了Python中的核心概念——类和对象,以及它们在面向对象编程中的应用。类是用户定义的类型,描述具有相同属性和行为的对象集合;对象是类的实例,具备类的属性和方法。文章通过示例讲解了如何定义类、创建及使用对象,包括`__init__`方法、属性访问和方法调用。此外,还阐述了类的继承,允许子类继承父类的属性和方法并进行扩展。掌握这些概念有助于提升Python编程的效率和灵活性。
|
1月前
|
Python
python函数用法(五)
python函数用法(五)
24 1
|
1月前
|
Python
python函数用法(四)
python函数用法(四)
22 0