zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python类和对象概念_Python自学第七节

2023-06-13 09:16:14 时间

了解类和对象概念首先要先了解函数的概念,在了解函数的概念基本就可以理解类和对象的概念。

函数的概念:

  • 函数的作用就是为了完成某个功能。
  • 类似linux命令包含内/外部函数,可以导入模块使用里边的函数或自定义函数。
  • 有些函数是通用的,有些函数只能特定的对象来使用。

面向过程和面向对象:

面向过程:

分析出解决问题所需的步骤,使用函数把解决问题的步骤逐步实现。例如C语言就是典型面向过程的。

面向对象:

把构成问题的事物分解成各个对象,建立对象的目的是为了实现某个功能,而不是为了完成一个步骤。

类的理解:

  • 提供一个模板,批量添加实例。
  • 类是实例的工厂,类提供模板,实例是具体的产品,对象是类的实例。

定义类:

例:定义名为Hero()的类,类中包括health和power两个变量(通常将类中的变量称为属性)、一个函数add(类中的函数称为方法

>>> class Hero(object):   #类的名字跟上object,表明该类是继承object类
...     health=100
...     power=80
...     def add(self,x,y):   #函数必须要有一个默认的self参数作为第一个参数
...             return x+y   #返回 x+y的值

>>> zhangxu = Hero()   #实例化成一个对象

>>> type(zhangxu)   #可以看到下方信息显示来自与Hero类


>>> isinstance(zhangxu,Hero)   #判断某个对象是否来自某个类
True

>>> zhangxu.health = 200   #变量可以直接更改
>>> Hero.health = 500   #可以灵活的进行更改类的变量

>>> zhangxu.add(4,5)   #调用类的add方法
9

dir()函数:

dir()可以查看类或对象提供可以调用的属性和方法。

>>> dir(Hero)   #查看Hero类的属性和方法 如下前边是默认的,后边是定义的
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'health', 'power']

help 可以查看指定方法的帮助信息。

help(Hero.add)   #可以查看指定方法的帮助信息


>>> a = '123' 
>>> dir(a)   #查看a定义的字节能调用的方法
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> dir(str)   #查看str字节类可以调用的方法
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(a.split)   #查看a.split方法的帮助信息
>>> help(str.split)   #查看str类下split方法的帮助信息

概念总结:

  • 面向对象是一种编程方式,此编程方式是基于类和对象的使用。
  • 类是一种模板,模板中封装了多个函数供使用。
  • 对象是根据模板创建的实例,调用被封装在类中的函数。
  • 模块中则集成了大量相关的类、函数、变量。