Python(5) 面向对象相关
来源:互联网 发布:歼十模型淘宝 编辑:程序博客网 时间:2024/06/07 18:00
1. 类基本定义
- 默认构造器为只有一个self参数,内容只有一行pass。
- 方法:
- 实例方法:以实例本身
self
作为第一个参数。 - 类方法:以类对象本身
cls
作为第一个参数,以@classmethod
修饰。 - 静态方法:参数无要求,以
@staticmethod
修饰。
- 实例方法:以实例本身
- 变量:
- 实例变量:以
self.
开头的变量。 - 类变量:在类定义后、方法外定义的变量。
- 变量的引用:
- 类变量:
self.__class__.class_field_name
:这种方法肯定成功。self.class_field_name
:如果成员变量中没有与类变量同名的变量,则该方法也可以调用类变量。class_name.class_field_name
:通过类名直接引用。
- 成员变量:
self.instant_field_name
。
- 类变量:
- 实例变量:以
class Foo: # 定义类变量 class_field = "class field" field = "class normal field" def __init__(self, x, y=1): # 构造器定义,默认构造器为无参数,内容只有一行pass # 可以设置参数,如上,则x必填,y选填 # 只能有一个构造器 print("Foo constructor", x, y) # 定义成员变量 self.x = x self.field = "instant normal field" def instant_function(self, field="method field"): print("实例方法") print(self.class_field) print(self.__class__.class_field) # 对于同名变量的处理 print(field) print(self.field) print(self.__class__.field) print(Foo.field) @classmethod def class_function(cls): print("类方法") @staticmethod def static_function(): print("静态方法")foo = Foo(1)foo.instant_function()# 输出# Foo constructor 1 1# 实例方法# class field# class field# method field# instant normal field# class normal field# class normal field
2. 超类相关
- 若子类不显示定义构造器,则默认调用父类构造器。
- 子类显示定义了父类方法/构造器后,不管参数列表是否相同,都会覆盖父类方法/父类构造器。
- 此处的方法包括实例方法、类方法、静态方法。
__mro__
class Father: @classmethod def class_method(cls, message): print('Father class method', message) @staticmethod def static_method(): print('Father static method') def __init__(self): print('father constructor') def method1(self, message): print('Father method1', message) def method2(self, message): print('Father method2', message)class Son(Father): @classmethod def class_method(cls): print('Son class method') @staticmethod def static_method(message): print('Son static method', message) def __init__(self, x, y=2): # 就算不调用父类也没关系 # super方式调用父类构造器 super(Son, self).__init__() print('son constructor') def method1(self): # 与父类方法名相同,参数不同 # 直接覆盖父类方法 print('Son method1') def method2(self, message): # 与父类方法名相同,参数相同 # 覆盖父类方法 # 调用父类方法 # super方式调用父类方法 super(Son, self).method2(message) print('Son method2')son = Son(1)son.class_method()son.static_method("son")# 输出# father constructor# son constructor# Son class method# Son static method son
3. 访问控制
- 以双下划线
__
开头的变量/方法,就是私有变量/方法。 - 私有方法不能直接以
__field_name
或__method_name
进行访问,但可以通过_class_name__field_name
或_class_name__method_name
访问。 - Python中没有绝对的私有。
- 单下划线:虽然不是私有的,但应被看作私有。如果有单下划线函数
_method()
,则:- 在别的模块使用
from somemodule import _method
导入方法成功。 - 在别的模块使用
from somemodule import *
则不会导入_method
方法。
- 在别的模块使用
class Foo: __class_field_name = 'private class field name' def __init__(self): __instant_field_name = 'private instant field name' def __private_method(self): print('private method')foo = Foo()print(foo._Foo__class_field_name) #foo._Foo__private_method()
4. 鸭子类型(Duck Typing)
- 定义:动态类型的一种风格。在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由当前方法和属性的集合决定。这个概念的名字来源于由James Whitcomb Riley提出的鸭子测试,“鸭子测试”可以这样表述:“当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。”
5. 对象信息
- type方法:获取对象类型。
- isinstance方法:判断的对象类型(包括继承)
- dir方法:获取对象所有属性和方法。(结果为list)
阅读全文
0 0
- Python(5) 面向对象相关
- Python学习笔记5-面向对象相关
- python——面向对象相关
- Python 5、面向对象
- 【5】Python面向对象编程
- python面向对象编程(5)
- 适配器、面向对象原则、python 基础语法相关技巧
- python(二):函数以及面向对象相关
- Python之路【第五篇】:面向对象及相关
- 面向对象相关
- 面向对象相关
- 面向对象相关知识
- javascript 面向对象相关
- 面向对象相关
- 面向对象相关问题
- python学习(5)面向对象编程
- python学习笔记5 面向对象编程
- Python面向对象5:特殊方法
- matplotlib的简单应用
- glide加载gif动态图
- Tomcat简介
- Python(4) 序列
- c0201时遇到的问题
- Python(5) 面向对象相关
- impdp或expdp报错ORA-39002: invalid operation,ORA-39070: Unable to open the log file
- Python(6) 异常处理
- two_sum
- 快速掌握Glide图片加载的强大功能你如果
- 设计模式13-策略模式
- c++设计模式三,工厂模式
- 洛谷 P1220 关路灯
- csdn 的友链设置