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(),则:
    1. 在别的模块使用from somemodule import _method导入方法成功。
    2. 在别的模块使用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)