Python류의 단일 계승 관련 지식에 대해 간단히 이야기하다

위의 글에서 Python 다중 상속에 관한 지식을 정리했습니다. 보지 못한 친구들도 가볼 수 있습니다. 오늘 파이썬류의 단일 계승 관련 지식을 소개해 드리겠습니다.

일류의 계승


대상을 향한 세 가지 요소 중 하나, 상속 Inheritance
인간과 고양이류는 모두 자동 물류를 계승한다.
개체는 부모로부터 계승되고 부모의 일부 특징을 계승하지만 자신의 개성을 가질 수 있다.
대상을 향한 세계에서 부류로부터 계승하면 부류의 속성과 방법을 직접 가지고 코드를 줄이고 많이 복용할 수 있다.하위 클래스는 자신의 속성과 방법을 정의할 수 있다

class Animal:
 
    def __init__(self,name):
        self._name = name
 
    def shout(self):
        print("{}  shouts".format(self.__class__.__name__))
    @property
    def name(self):
        return self._name
 
 
class Cat(Animal):
    pass
 
 
class Dog(Animal):
    pass
 
a = Animal("monster")
a.shout()           #   Animal  shouts
 
 
cat = Cat("garfield")
cat.shout()         #   Cat  shouts
print(cat.name)     #   garfield
 
 
dog = Dog("ahuang")
dog.shout()         #   Dog  shouts
print(dog.name)     #   ahuang
상례에서 우리는 계승, 고양이류, 개류를 통해 코드를 쓰지 않고 부류의 속성과 방법을 직접 계승하였음을 알 수 있다
상속:
  • class Cat(Animal) 이런 형식은 부류에서 계승하고 괄호에 계승된 클래스의 목록을 쓰는 것이다.
  • 상속을 통해 하위 클래스가 상위 클래스에서 피쳐(속성, 방법)를 얻을 수 있음
  • 상위 클래스:
  • Animal은 Cat의 부류로 기류, 초류라고도 부른다
  • 하위 클래스:
  • 캣은 애니멀의 자류이자 파생류가 된다
  • 2. 상속의 정의, 상속의 특수한 속성과 방법을 보기


    형식
    
    class   ( 1[, 2,……]):
         
    클래스를 정의할 때 기본 클래스 목록이 없으면 [object]에서 계승하는 것과 같습니다.Python3에서 [object] 클래스는 모든 대상의 기본 클래스입니다
    상속된 특수 속성 및 방법 보기
    특수 속성
    함의
    예제
    __base__ 
    클래스의 기본 클래스
    __bases__ 
    클래스의 기본 클래스 모듈
    __mro__ 
    표시 방법 찾기 순서, 기본 클래스의 메타그룹
    mro() 
    함께
     int.mro()
    __subclasses__() 
    클래스의 하위 클래스 목록
     int.__subclasses__()

    3. 상속 중인 액세스 제어

    
    class Animal:
        __COUNT = 100
        HEIGHT = 0
     
        def __init__(self,age,weight,height):
            self.__COUNT += 1
            self.age = age
            self.__weight = weight
            self.HEIGHT = height
     
        def eat(self):
            print("{}  eat".format(self.__class__.__name__))
     
        def __getweight(self):
            print(self.__weight)
     
        @classmethod
        def showcount1(cls):
            print(cls.__COUNT)
     
        @classmethod
        def __showcount2(cls):
            print(cls.__COUNT)
     
        def showcount3(self):
            print(self.__COUNT)
     
    class Cat(Animal):
        NAME = "CAT"
        __COUNT = 200
     
    #a = Cat()              #   TypeError: __init__() missing 3 required positional arguments: 'age', 'weight', and 'height'
    a = Cat(30,50,15)
    a.eat()                 #   Cat  eat
    print(a.HEIGHT)         #   15
    #print(a.__COUNT)        #   AttributeError: 'Cat' object has no attribute '__COUNT'
    #print(a.__showcount2)   #   AttributeError: 'Cat' object has no attribute '__showcount2'
    #print(a.__getweight)    #   AttributeError: 'Cat' object has no attribute '__getweight'
    a.showcount3()   #   101
    a.showcount1()   #  100
    print(a.NAME)    #    CAT
     
    print(Animal.__dict__)  #   {'__module__': '__main__', '_Animal__COUNT': 100, 'HEIGHT': 0, '__init__': <function Animal.__init__ at 0x020DC228>, 'eat': <function Animal.eat at 0x020DC468>, '_Animal__getweight': <function Animal.__getweight at 0x02126150>, 'showcount1': <classmethod object at 0x020E1BD0>, '_Animal__showcount2': <classmethod object at 0x020E1890>, 'showcount3': <function Animal.showcount3 at 0x021264F8>, '__dict__': <attribute '__dict__' of 'Animal' objects>, '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
    print(Cat.__dict__)     #   {'__module__': '__main__', 'NAME': 'CAT', '_Cat__COUNT': 200, '__doc__': None}
    print(a.__dict__)       #   {'_Animal__COUNT': 101, 'age': 30, '_Animal__weight': 50, 'HEIGHT': 15}
    
    부류의 계승, 자신이 없는 것을 부류에서 찾을 수 있다
    개인 정보는 접근할 수 없지만 본질적으로 이 속성이 있는 클래스에 이름을 바꿨습니다. [_dict__]중, 이 신민의 성과를 알면 이 숨겨진 변수를 직접 찾을 수 있습니다. 이것은 흑마법입니다.
    총결산
  • 계승할 때 공유되고 자류와 실례는 모두 마음대로 접근할 수 있다.개인 구성원이 숨겨져 있고 하위 클래스와 실례는 직접 접근할 수 없습니다. 개인 변수가 있는 클래스 내 방법에서 이 개인 변수에 접근할 수 있습니다
  • Python은 자신의 일련의 실현을 통해 다른 언어와 같은 대상을 향한 계승 메커니즘을 실현한다
  • 속성 찾기 순서: 인스턴스의 [_dict__]-----클래스의 [_dict__]------상위 클래스 [_dict__]
    만약 이곳을 수색한 후 이상을 찾지 못했다면, 먼저 찾으면 즉시 되돌아갈 것이다

    4. 방법의 다시 쓰기, 오버라이드 덮어쓰기

    
    class Animal:
     
        def shout(self):
            print("Animal shouts")
     
    class Cat(Animal):
     
        def shout(self):
            print("miao")
     
    a = Animal()
    a.shout()       #   Animal shouts
    b  = Cat()
    b.shout()       #   miao
     
    print(a.__dict__)       #   {}
    print(b.__dict__)       #   {}
    print(Animal.__dict__)  #   {'__module__': '__main__', 'shout': <function Animal.shout at 0x017BC228>, '__dict__': <attribute '__dict__' of 'Animal' objects>, '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
    
    
    Cat 클래스에서 shout은 왜 Animal에서 shout을 인쇄하는 방법이 없고 방법이 덮어씁니까?
  • 속성 찾기 순서: 인스턴스의 [_dict__] -----클래스의 [_dict__]------상위 클래스 [_dict__]
  • 그 부류는 부류의 동명을 어떻게 인쇄하는가
  • super() 상위 클래스에 액세스할 수 있는 속성
  • 
    class Animal:
     
        def shout(self):
            print("Animal shouts")
     
    class Cat(Animal):
     
        def shout(self):
            print("miao")
     
        def shout(self):
            print("super():   " , super())
            print(super(Cat, self))
            super().shout()
            super(Cat,self).shout()   #  super().shout()
            self.__class__.__base__.shout(self)  # 
     
    a = Animal()
    a.shout()       #   Animal shouts
    b  = Cat()
    b.shout()       #   super():    <super: <class 'Cat'>, <Cat object>>
                    #   <super: <class 'Cat'>, <Cat object>>
                    #   Animal shouts
                    #   Animal shouts
                    #   Animal shouts
    print(a.__dict__)       #   {}
    print(b.__dict__)       #   {}
    print(Animal.__dict__)  #   {'__module__': '__main__', 'shout': <function Animal.shout at 0x019AC228>, '__dict__': <attribute '__dict__' of 'Animal' objects>, '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
    print(Cat.__dict__)     #   {'__module__': '__main__', 'shout': <function Cat.shout at 0x019F6150>, '__doc__': None}
     
    
    
    super(Cat,self).shout()의 작용은
  • 호출, 현재 b의 실례에서cat류의 기본 클래스를 찾는 방법
  • 그것은 유형 방법과 정태 방법에도 똑같이 적용됩니까?
    
    class Animal:
        @classmethod
        def class_method(cls):
            print("class_method")
     
        @staticmethod
        def static_method():
            print("static_methond_animal")
     
    class Cat(Animal):
        @classmethod
        def class_method(cls):
            super().class_method()  #   class_method
            print("class_method_cat")
     
        @staticmethod
        def static_method(cls,self):
            super(Cat,self).static_method()
            print("static_method_cat")
     
    b = Cat()
    b.class_method()    #   class_method
                        #   class_method_cat
    b.static_method(Cat,b)
                        #   static_methond_animal
                        #   static_method_cat
    
    이 방법들은 모두 덮어쓸 수 있으며, 원리는 모두 같다. 속성 사전의 검색 순서

    5. 계승 중의 초기화


    다음 코드를 보십시오. 문제가 있습니까?
    
    class A:
        def __init__(self,a):
            self.a = a
     
    class B(A):
        def __init__(self,b,c):
            self.b = b
            self.c = c
     
        def printv(self):
            print(self.b)
            print(self.a)
     
    a = B(100,300)
    print(a.__dict__)       #   {'b': 100, 'c': 300}
    print(a.__class__.__bases__)    #   (<class '__main__.A'>,)
    a.printv()      #   100
                    #   AttributeError: 'B' object has no attribute 'a'
    
    이전 코드
  • B 클래스가 정의될 때 클래스 A에서 상속됨을 선언하면 B 클래스에서 __bases__클래스 A
  • 클래스 A 호출 여부와 구조 방법은 별개
  • 만약에 B에서 A의 구조 방법을 호출하면 부류의 속성을 가질 수 있다. 이 말을 이해한다면?
  • 
    class A:
        def __init__(self,a):
            self.a = a
     
    class B(A):
        def __init__(self,b,c):
            super().__init__(b+c)
            # A.__init__(self,b+c)
            self.b = b
            self.c = c
     
        def printv(self):
            print(self.b)
            print(self.a)
     
    a = B(100,300)
    print(a.__dict__)       #   {'a': 400, 'b': 100, 'c': 300}
    print(a.__class__.__bases__)    #   (<class '__main__.A'>,)
    a.printv()      #   100
                    #   400
    
    상위 클래스가 정의된 경우 좋은 습관으로 __init__방법, 하위 클래스로 변경__init__에서 호출합니다. [슈퍼 () 방법으로 호출하는 것을 권장합니다.]
    하위 클래스가 상위 클래스의 [_init__] 를 자동으로 호출하는 경우방법?
    예1: [B 인스턴스를 초기화하면 기본 클래스 A의 __init__ 메서드가 자동으로 호출됩니다.]
    
    class A:
        def __init__(self):
            self.a1 = "a1"
            self.__a2 = "a2"
            print("A init")
     
    class B(A):
        pass
     
    b = B()     #   A init
    print(b.__dict__)   #   {'a1': 'a1', '_A__a2': 'a2'}
    
    예2: [B 인스턴스의 초기화 __init__ 메서드는 상위 클래스의 초기화 __init_ 메서드를 자동으로 호출하지 않으며 수동으로 호출해야 함]
    
    class A:
        def __init__(self):
            self.a1 = "a1"
            self.__a2 = "a2"
            print("A init")
     
    class B(A):
        def __init__(self):
            self.b1 = "b1"
            self.__b2 = "b2"
            print("b init")
            #A.__init__(self)
     
    b = B()     #   b init
    print(b.__dict__)   #   {'b1': 'b1', '_B__b2': 'b2'}
    
    그러면 어떻게 정확하게 실례화합니까?
  • 상위 클래스를 호출하는 __init__방법, 서로 다른 위치에 나타나면 서로 다른 결과가 나타날 수 있다
  • 
    class Animal:
        def __init__(self,age):
            print("Animal init")
            self.age = age
     
        def show(self):
            print(self.age)
     
    class Cat(Animal):
        def __init__(self,age,weight):
            # __init__   show 
            super(Cat, self).__init__(age)
            print("Cat init")
            self.age = age + 1
            self.weight = weight
     
    a = Cat(10,5)
    a.show()        #   Animal init
                    #   Cat init
                    #   11
    
    어떻게 상례의 모든 실례 속성을 사유 속성으로 직접 바꿉니까?
  • 해결 방법, 하나의 원칙, 자신의 사유 속성, 이 자신의 방법에 대한 읽기와 수정, 다른 종류의 방법, 즉 부류나 파생류
  • 를 빌리지 마라
    
    class Animal:
        def __init__(self,age):
            print("Animal init")
            self.__age = age
     
        def show(self):
            print(self.__age)
     
    class Cat(Animal):
        def __init__(self,age,weight):
            # __init__   show 
            super(Cat, self).__init__(age)
            print("Cat init")
            self.__age = age + 1
            self.__weight = weight
     
        def show(self):
            print(self.__age)
     
    a = Cat(10,5)
    a.show()        #   Animal init
                    #   Cat init
                    #   11
    print(a.__dict__)   #   {'_Animal__age': 10, '_Cat__age': 11, '_Cat__weight': 5}
    
    파이썬 클래스의 단일 계승에 관한 지식을 간단히 말씀드리는 이 글을 소개합니다. 파이썬 클래스의 단일 계승에 관한 더 많은 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보시기 바랍니다. 앞으로 많은 응원 부탁드립니다!

    좋은 웹페이지 즐겨찾기