python- Object Oriented (1)

  • Object-Oriented Programming (OOP)
    • basis
    • Public Private
    • inherit
    • Combination, Mixin
  • Magic function
    • Magic Functions Overview
    • Magic class constructor function
    • Arithmetic function like magic

1. Overview of Object-Oriented (ObjectOriented, OO)

  • Classes and Objects and concepts
    • Categories: abstract nouns, representing a collection of things in common
    • Object: figurative things, a single individual
    • Relations with the object class
      • A figurative, on behalf of a class of things a certain individual
      • One is the abstract, represent a large class of things
  • Content class, content should have two
    • Show the characteristics of things, called attributes (variables)
    • Show that things function or action, called member methods (functions)

2. The basic implementation class

  • Class naming
    • Follow variable naming norms
    • Large hump (composed of one or more words, each word capitalized, the word is directly connected with the word)
    • Try to avoid naming system with a similar name
  • How to declare a class
    • You must use the keyword class
    • The method consists of classes and properties, other allowed
    • Members of the attribute definition can be used as variable assignment, if there is no value, then None
  • Instantiate the class

      变量 = 类名() #实例化了一个对象
  • Access object members
    • Using the dot operator

         obj.成员属性名称
         obj.成员方法 
  • By default all members can be special function (magic function) to check classes and objects
    • All members of the inspection target

        # dict前后各有两个下划线
        obj.__dict__ 
    • All members of the class

        # dict前后各有两个下划线
       class_name.__dict__
    • Case

        # 创建类
        class Person():
            # 类变量
            name = 'chenpingan'
            age = 18
            # 方法
            def say(self):
                print('hello world')
        # 实例化 
        p = Person()
        p.say()
        # 查看对象成员
        print(p.__dict__)
        print(Person.__dict__)
        >>>
        hello world
        {}
        {'__module__': '__main__', 'name': 'chenpingan', 'age': 18, '__init__': <function Person.__init__ at 0x7f87ec138510>, 'say': <function Person.say at 0x7f87ec138400>,                         
        '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}

3. The members of the class and object analysis

  • Classes and objects can be stored members, members can categorize all, you can go all objects
  • Used for the storage class members are associated with the class of an object: the class object
  • Exclusive storage members is to store in the current object
  • When object access a member, if the member does not object, try to access the same name as a member of the class, if the object has this member, be sure to use members of the object
  • When creating an object, class members will not put an object which, but get an empty object, no members
  • Re-assignment or add members through an object through an object of class members, members will be saved in the corresponding object, and does not modify the class members

4. With regard to self

  • self represents a parameter of the current object itself, if you call a method through an object, then the object is automatically passed to the current method in the object's methods
  • self is not a keyword, just an ordinary parameter accepts an object, in theory, may be replaced by any ordinary variable name
  • The method has become the method of self parameter unbound class method, can be accessed through the object, is a method not self binding class, the class can only be accessed by
  • When using the method to access class binding class, if the class method requires access to members of the current class, by class access. Member Name
  • Case

      # 创建类
      class A():
          name = "dana"
          age = 18
          # 注意say的写法,参数由一个self
          def say(self):
              self.name = "aaaa"
              self.age = 200
      # 此案例说明
      # 类实例的属性和其对象的实例的属性在不对对象的实例属性赋值的前提下,
      # 指向同一个变量
      # 此时,A称为类实例
      print(A.name)
      print(A.age)
      print("*" * 20)
      # id可以鉴别一个变量是否和另一个变量是同一变量
      print(id(A.name))
      print(id(A.age))
      print("*" * 20)
      a = A()
      # 查看A内所有的属性
      print(A.__dict__)
      print(a.__dict__)
      a.name = "yaona"
      a.age = 16
      print(a.__dict__)
      print(a.name)
      print(a.age)
      print(id(a.name))
      print(id(a.age))
      >>>
      dana
      18
      ********************
      140666260313176
      93936169025280
      ********************
      {'__module__': '__main__', 'name': 'dana', 'age': 18, 'say': <function A.say at 0x7fef6a799730>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute     
      '__weakref__' of 'A' objects>, '__doc__': None}
      {}
      {'name': 'yaona', 'age': 16}
      yaona
      16
      140666251554240
      93936169025216
  • Case

      # __class__.属性
      class Teacher():
          name = "dana"
          age = 19    
          def say(self):
              self.name = "yaona"
              self.age = 17
              print("My name is {0}".format(self.name))
              # 调用类的成员变量需要用 __class__
              print("My age is {0}".format(__class__.age))
          def sayAgain():
              print(__class__.name)
              print(__class__.age )
              print("Hello, nice to see you again")
      t = Teacher()
      t.say()
      # 调用绑定类函数使用类名
      Teacher.sayAgain()
      >>>
      My name is yaona
      My age is 19
      dana
      19
      Hello, nice to see you again
  • Case

      # 关于self的案例
      class A():
          name = " liuying"
          age = 18    
          def __init__(self):
              self.name = "aaaa"
              self.age = 200        
          def say(self):
              print(self.name)
              print(self.age)        
      class B():
          name = "bbbb"
          age = 90    
      a = A()
      # 此时,系统会默认把a作为第一个参数传入函数
      a.say()   
      # 此时,self被a替换
      A.say(a)
      # 同样可以把A作为参数传入
      A.say(A)
      # 此时,传入的是类实例B,因为B具有name和age属性,所以不会报错
      A.say(B)
      # 以上代码,利用了鸭子模型
      >>>
      aaaa
      200
      aaaa
      200
       liuying
      18
      bbbb
      90

Guess you like

Origin www.cnblogs.com/chenpingan-cc/p/12324354.html