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
상례에서 우리는 계승, 고양이류, 개류를 통해 코드를 쓰지 않고 부류의 속성과 방법을 직접 계승하였음을 알 수 있다상속:
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__]중, 이 신민의 성과를 알면 이 숨겨진 변수를 직접 찾을 수 있습니다. 이것은 흑마법입니다.
총결산
만약 이곳을 수색한 후 이상을 찾지 못했다면, 먼저 찾으면 즉시 되돌아갈 것이다
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을 인쇄하는 방법이 없고 방법이 덮어씁니까?
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()의 작용은
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'
이전 코드
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'}
그러면 어떻게 정확하게 실례화합니까?
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}
파이썬 클래스의 단일 계승에 관한 지식을 간단히 말씀드리는 이 글을 소개합니다. 파이썬 클래스의 단일 계승에 관한 더 많은 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보시기 바랍니다. 앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.