[python学习]-- 类

在Python中,类是面向对象编程(OOP)的基础。类定义了一组相似的对象(通常称为实例)共有的属性和方法。以下是一个简单的Python类学习指南:

1. 定义类

使用class关键字来定义类。类名通常使用驼峰命名法(即每个单词的首字母大写)。

class MyClass: 
# 这里是类的定义 
pass

2. 添加属性

属性是类的变量,用于存储与类及其实例相关的数据。属性可以在类定义时直接定义,也可以在类的实例化后通过实例来设置。

class MyClass:

# 类属性(静态属性),所有实例共享

class_attribute = "I'm a class attribute"



def __init__(self, instance_attribute):

# 实例属性,每个实例都有自己的副本

self.instance_attribute = instance_attribute



# 实例化类

my_instance = MyClass("I'm an instance attribute")



# 访问类属性

print(MyClass.class_attribute) # 输出: I'm a class attribute



# 访问实例属性

print(my_instance.instance_attribute) # 输出: I'm an instance attribute

3. 添加方法

方法是类的函数,用于定义对象的行为。方法通常与实例(即对象)关联,但也可以定义为类方法或静态方法。

class MyClass:

def __init__(self, name):

self.name = name



def greet(self):

print(f"Hello, my name is {self.name}!")



# 实例化类并调用方法

my_instance = MyClass("Alice")

my_instance.greet() # 输出: Hello, my name is Alice!

4. 继承

继承是面向对象编程的一个重要特性,它允许一个类(子类)继承另一个类(父类)的属性和方法。

class ParentClass:

def __init__(self):

self.parent_attribute = "I'm from ParentClass"



def parent_method(self):

print("This is a method from ParentClass")



class ChildClass(ParentClass):

def __init__(self):

super().__init__() # 调用父类的初始化方法

self.child_attribute = "I'm from ChildClass"



def child_method(self):

print("This is a method from ChildClass")



# 实例化子类并访问属性和方法

child_instance = ChildClass()

print(child_instance.parent_attribute) # 输出: I'm from ParentClass

print(child_instance.child_attribute) # 输出: I'm from ChildClass

child_instance.parent_method() # 输出: This is a method from ParentClass

child_instance.child_method() # 输出: This is a method from ChildClass

5. 特殊方法(魔术方法)

Python类中有一些特殊的方法,它们以双下划线开头和结尾(如__init____str____eq__等),用于实现类的特殊行为。例如,__init__方法用于初始化实例,__str__方法用于定义对象的字符串表示形式。

6. 封装、多态和抽象

封装、多态和抽象是面向对象编程的三大核心概念,但在Python中,这些概念的实现方式与其他语言有所不同。Python通过类和方法来实现封装,通过继承和接口来实现多态,而抽象则通常通过抽象基类(使用abc模块)来实现。

相关推荐

  1. Python 学习

    2024-06-18 07:30:02       40 阅读
  2. [python学习]--

    2024-06-18 07:30:02       29 阅读
  3. [Python学习篇] Python面向对象——

    2024-06-18 07:30:02       23 阅读
  4. Python学习笔记——(基础知识)

    2024-06-18 07:30:02       39 阅读
  5. 层次【python,机器学习,算法】

    2024-06-18 07:30:02       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-18 07:30:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-18 07:30:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-18 07:30:02       87 阅读
  4. Python语言-面向对象

    2024-06-18 07:30:02       96 阅读

热门阅读

  1. C和C++

    2024-06-18 07:30:02       21 阅读
  2. 【 Python 自动化脚本:高效管理文件和文件夹】

    2024-06-18 07:30:02       27 阅读
  3. Starknet架构之Starknet state、State commitment

    2024-06-18 07:30:02       32 阅读
  4. SpringBoot3使用Swagger

    2024-06-18 07:30:02       33 阅读
  5. Mybatis 的缓存功能

    2024-06-18 07:30:02       26 阅读
  6. Flask-RESTPlus

    2024-06-18 07:30:02       28 阅读
  7. XML 编辑器:功能、选择与使用技巧

    2024-06-18 07:30:02       24 阅读
  8. 如何通俗理解逻辑回归(Logistic Regression)

    2024-06-18 07:30:02       30 阅读
  9. 【神经网络】深度神经网络如何应用于推荐系统

    2024-06-18 07:30:02       29 阅读
  10. TransformerConv

    2024-06-18 07:30:02       21 阅读
  11. 网络安全筑基篇——文件上传

    2024-06-18 07:30:02       33 阅读
  12. c++日期类的实现

    2024-06-18 07:30:02       30 阅读