이 기사에서는 Python 3.x에서 상속 및 확장 클래스를 학습합니다. 또는 그 이전.
상속은 실제 관계를 잘 나타내고 재사용성을 제공하며 전이성을 지원합니다. 더 빠른 개발 시간, 더 쉬운 유지 관리 및 쉬운 확장을 제공합니다.
상속은 크게 5가지 유형으로 분류됩니다 -
- 싱글
- 다중
- 계층적
- 다단계
- 하이브리드

위의 그림에서 보듯이 상속은 실제로 부모 클래스의 객체를 만들지 않고 다른 클래스의 기능에 접근을 시도하는 과정입니다.
여기서 우리는 단일 및 계층적 상속의 구현에 대해 배울 것입니다.
단일 상속
예시
# parent class
class Student():
# constructor of parent class
def __init__(self, name, enrollnumber):
self.name = name
self.enrollnumber = enrollnumber
def display(self):
print(self.name)
print(self.enrollnumber)
# child class
class College( Student ):
def __init__(self, name, enrollnumber, admnyear, branch):
self.admnyear = admnyear
self.branch = branch
# invoking the __init__ of the parent class
Student.__init__(self, name, enrollnumber)
# creation of an object for base/derived class
obj = College('Rohit',42414802718,2018,"CSE")
obj.display() 출력
Rohit 42414802718
다중 상속
예시
# parent class
class Student():
# constructor of parent class
def __init__(self, name, enrollnumber):
self.name = name
self.enrollnumber = enrollnumber
def display(self):
print(self.name)
print(self.enrollnumber)
# child class
class College( Student ):
def __init__(self, name, enrollnumber, admnyear, branch):
self.admnyear = admnyear
self.branch = branch
# invoking the __init__ of the parent class
Student.__init__(self, name, enrollnumber)
# child class
class University( Student ):
def __init__(self, name, enrollnumber, refno, branch):
self.refno = refno
self.branch = branch
# invoking the __init__ of the parent class
Student.__init__(self, name, enrollnumber)
# creation of an object for base/derived class
obj_1 = College('Rohit',42414802718,2018,"CSE")
obj_1.display()
obj_2 = University ('Rohit',42414802718,"st2018","CSE")
obj_2.display() 출력
Rohit 42414802718 Rohit 42414802718
결론
이 기사에서 우리는 Python의 상속에 대해 광범위하게 단일 및 계층적 상속에 대해 배웠습니다.