2Python全栈之路系列之面向对象进阶及类成员

简介:

再次了解多继承

先来一段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
 
class  A:
     def  bar( self ):
         print ( "BAR" )
         self .f1()
         
class  B(A):
     def  f1( self ):
         print ( "B" )
         
class  C:
     def  f1( self ):
         print ( "C" )
         
class  D(C, B):
     pass
     
obj  =  D()
obj.bar()

执行结果

1
2
3
4
5
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day03 / s1.py
BAR
C
 
Process finished with exit code  0

流程释意:

  1. 创建了类A、B、C、D;

  2. D继承了CBB继承了AD内什么都不做,pass

  3. 创建一个对象obj,类是D,当执行Dbar方法的时候会先从C里面寻找有没有bar方法;

  4. C内没有bar方法,然后继续从B里面查找,B里面也没有,B的父类是AA里面有bar方法,所以就执行了Abar方法;

  5. Abar方法首先输出了BAR

  6. 然后又执行了self.f1()self=obj,相当于执行了obj.f1()

  7. 执行obj.f1()的时候先从C里面查找有没有f1这个方法,C里面又f1这个方法;

  8. 最后就执行C里面的f1方法了,输出了C

执行父类的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
class  Annimal:
     def  __init__( self ):
         print ( "Annimal的构造方法" )
         self .ty  =  "动物"
         
class  Cat(Annimal):
     def  __init__( self ):
         print ( "Cat的构造方法" )
         self .n  =  "猫"
         # 寻找Cat类的父类,然后执行父类的构造方法
         super (Cat,  self ).__init__()
mao  =  Cat()
print (mao.__dict__)

执行结果

1
2
3
4
5
6
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day03 / s1.py
Cat的构造方法
Annimal的构造方法
{ 'ty' '动物' 'n' '猫' }
 
Process finished with exit code  0

先执行了Cat的构造方法,然后又执行了Annimal的构造方法。
第二种执行父类的方法如下:

1
Annimal.__init__( self )

不推荐使用

利用反射查看面向对象成员归属

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
 
class  Foo:
     def  __init__( self , name):
         self .name  =  name
     def  show( self ):
         print ( 'show' )
         
obj  =  Foo( "as" )
 
# 如果是类,就只能找到类里的成员
print ( hasattr (Foo,  "show" ))
 
# 如果是对象,既可以找到对象,也可以找类里的成员
print ( hasattr (obj,  "name" ))
print ( hasattr (obj,  "show" ))

执行结果

1
2
3
4
5
6
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day03 / s2.py
True
True
True
 
Process finished with exit code  0

利用反射导入模块、查找类、创建对象、查找对象中的字段

s1脚本文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
# _*_coding:utf-8 _*_
 
# 导入模块
=  __import__ ( 's2' , fromlist = True )
 
# 去模块中找类
class_name  =  getattr (m,  "Foo" )
 
# 根据类创建对象
obj  =  class_name( "ansheng" )
 
# 去对象中找name对应的值
print ( getattr (obj,  'name' ))

s2脚本文件内容

1
2
3
4
5
6
7
#!/usr/bin/env python
# _*_coding:utf-8 _*_
 
class  Foo:
     def  __init__( self , name):
         # 普通字段,保存在对象中
         self .name  =  name

执行结果:

1
2
3
4
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day04 / s1.py
ansheng
 
Process finished with exit code  0

面向对象类成员之静态字段

静态字段存在类中,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# _*_coding:utf-8 _*_
 
# 静态字段存在的意义就是将每个对象中重复的东西在类里面保存一份即可,这就是静态字段
 
class  Provice:
 
     # 静态字段
     contry  =  "China"
     
     def  __init__( self , name):
         self .name  =  name
         
     def  show( self ):
         print (Provice.contry,  self .name)
         
hebei  =  Provice( "河北" )
hebei.show()
 
hubei  =  Provice( "湖北" )
hubei.show()

执行结果

1
2
3
4
5
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day04 / s2.py
China 河北
China 湖北
 
Process finished with exit code  0

类里面的成员类去访问,对象内的成员用对象去访问。

面向对象类成员之静态方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
# _*_coding:utf-8 _*_
 
class  Foo:
 
     # 静态方法括号内没有self,切方法前一行要加上@staticmethod
     @ staticmethod
     def  static():
     # def static(arg1, arg2): # 也可以设置参数
         print ( "static" )
         
# 静态方法通过类名+方法名既可执行
Foo.static()
# Foo.static("arg1", "arg2") 通过类调用的时候传入对于的参数即可
 
# 静态方法也可以通过对象去访问,对于静态方法用类去访问
obj  =  Foo()
obj.static(

执行结果

/usr/bin/python3.5 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
static
static

Process finished with exit code 0

面向对象类成员之类方法

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
# _*_coding:utf-8 _*_
 
class  Foo:
 
     # 创建类方法的时候需要在方法前面加上@classmethod
     @ classmethod
     def  ClassMethod ( cls ):  # 并且方法的括号内必须带有cls关键字,类方法的参数是当前类的类名
         print ( "类方法" )
         
# 调用类方法
Foo. ClassMethod ()

执行结果:

1
2
3
4
/ usr / bin / python3. 5  / home / ansheng / 文档 / Python_code / sublime / Week06 / Day04 / s2.py
类方法
 
Process finished with exit code  0

面向对象类成员内容梳理

字段

1.静态字段(每个对象都有一份)
2.普通字段(每个对象都不同的数据)

方法

1.静态方法(无需使用对象封装的内容)
2.类方法
3.普通方法(适用对象中的数据)

特性

1.普通特性(将方法未造成字段)

快速判断,类执行、对象执行:

1.self —> 对象调用
2.无self —> 类调用










本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1917743,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Python
你真的会面向对象吗!解密Python“魔术方法”
你真的会面向对象吗!解密Python“魔术方法”
22 0
|
1月前
|
供应链 数据挖掘 Serverless
【python】美妆类商品跨境电商数据分析(源码+课程论文+数据集)【独一无二】
【python】美妆类商品跨境电商数据分析(源码+课程论文+数据集)【独一无二】
【python】美妆类商品跨境电商数据分析(源码+课程论文+数据集)【独一无二】
|
14天前
|
Python
12类常用的Python函数
12类常用的Python函数
|
12天前
|
存储 程序员 Python
Python类的定义_类和对象的关系_对象的内存模型
通过类的定义来创建对象,我们可以应用面向对象编程(OOP)的原则,例如封装、继承和多态,这些原则帮助程序员构建可复用的代码和模块化的系统。Python语言支持这样的OOP特性,使其成为强大而灵活的编程语言,适用于各种软件开发项目。
14 1
|
15天前
|
存储 程序员 C++
python类及其方法
python类及其方法
|
14天前
|
C++ Python
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
11 1
|
27天前
|
对象存储 Python
Python代码解读-理解-定义一个User类的基本写法
以上描述清晰地阐述了如何在Python中定义 `User`类的基本方法以及如何创建和使用该类的实例。这是面向对象编程中的核心概念,是紧密结合抽象和实现,封装数据并提供操作数据的接口。由于用简单通用的语言易于理解,这样的解释对于初学者而言应该是友好且有帮助的。
22 4
|
1月前
|
测试技术 数据处理 数据格式
Python中动态类和动态方法的创建与调用
【8月更文挑战第5天】在Python中,`type`函数可用于创建动态类,结合`types.MethodType`可创建动态方法。例如,定义`dynamic_method`后,可通过`type`创建包含该方法的`DynamicClass`。同样,对于已存在的类实例,可通过`types.MethodType`绑定新方法。这种动态特性适用于自动化测试框架或数据处理应用等场景,实现根据需求动态生成类及方法以执行特定逻辑。
|
11天前
|
JSON 数据库 开发者
FastAPI入门指南:Python开发者必看——从零基础到精通,掌握FastAPI的全栈式Web开发流程,解锁高效编码的秘密!
【8月更文挑战第31天】在当今的Web开发领域,FastAPI迅速成为开发者的热门选择。本指南带领Python开发者快速入门FastAPI,涵盖环境搭建、基础代码、路径参数、请求体处理、数据库操作及异常处理等内容,帮助你轻松掌握这一高效Web框架。通过实践操作,你将学会构建高性能的Web应用,并为后续复杂项目打下坚实基础。
24 0
|
1月前
|
程序员 Ruby Python
Python里的类和对象是什么?
本文介绍了Python中面向对象编程的核心概念——类与对象。类作为一种“蓝图”,定义了一组属性和方法,用于描述一类对象的共同特征与行为。通过类可以创建具体的对象实例,每个对象拥有类所定义的属性和方法。文章通过`Human`类的例子详细展示了如何定义类、初始化对象及其属性、定义方法,以及如何给对象添加自定义属性。此外,还介绍了如何通过类创建多个具有不同特性的对象实例,并探讨了属性覆盖和使用`@property`装饰器实现只读属性的方法。