일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 프로그래밍
- C#
- UE5
- 파이썬
- 문제풀이
- parameter
- Basic
- 재귀
- dynamic
- Unity
- String
- Algorithm
- Unreal Engine 5
- Material
- github
- c++
- 기초
- dfs
- 오류
- guide
- python
- loop
- Class
- Tutorial
- 시작해요 언리얼 2022
- Programming
- DP
- w3school
- 백준
- W3Schools
- Today
- Total
행복한 개구리
Python 공부내용 21.10.08. Python - Inheritance(상속) 본문
W3Schools의 Python Tutorial을 보며 해석하고 정리한 글입니다.
오역이 존재할 수 있습니다
저도 파이썬은 처음이니 가볍게 봐주세요 :D
파이썬 상속
파이썬 상속
상속은 우리가 다른 클래스의 모든 메서드와 프로퍼티들을 상속받아 클래스를 정의하도록 해줍니다.
Parent class (부모 클래스) 는 상속을 하는 클래스입니다. base class (기초 클래스) 라고도 합니다.
Child class (자식 클래스) 는 다른 클래스를 상속받는 클래스입니다. 또한 deprived class (파생 클래스) 라고도 합니다.
부모 클래스 만들기
어느 클래스던지 부모 클래스가 될 수 있습니다. 따라서 다른 아무 클래스를 만들 때와 같은 구문을 사용합니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
자식 클래스 만들기
다른 클래스의 기능들을 상속받으며 클래스를 생성하려면, 자식 클래스를 생성할 때 부모 클래스를 매개변수로 전달하면 됩니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
class Student(Person):
pass
주의 : pass 키워드는 클래스에 메서드나 프로퍼티가 없는데 오류를 일으키고 싶지 않을 때 사용합니다.
이제 Student클래스는 Person 클래스와 같은 프로퍼티와 메서드를 가집니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
class Student(Person):
pass
x = Student("Mike", "Olsen")
x.printname()
__init__() 함수 추가하기
지금까지 우리가 부모 클래스의 프로퍼티와 메서드를 상속받아 자식 클래스를 생성했습니다.
우리는 __init__() 함수를 자식 클래스에 추가하기를 원합니다. ( pass 키워드 대신에 말이죠.)
주의 : __init__() 함수는 클래스가 새로운 객체를 생성할 때마다 자동적으로 호출됩니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
class Student(Person):
def __init__(self, fname, lname):
당신이 __init__() 함수를 추가했을 때, 자식 클래스는 더 이상 부모 클래스의 __init__() 함수를 상속받지 않습니다.
주의 : 자식 클래스의 __init__() 함수는 부모 클래스의 __init__() 함수를 오버라이드(override, 덮어쓰기)합니다.
부모 클래스의 __init__() 함수를 유지하고 싶다면, 내용을 부모 클래스의 __init__() 함수에 추가합니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
이제 우리는 성공적으로 __init__() 함수를 추가했습니다. 그리고 부모 클래스와의 상속을 유지했으며 __init__() 함수에 기능을 추가할 준비가 되었습니다.
super() 함수 사용하기
파이썬은 super() 함수를 가지고 있습니다. 이것은 자식 클래스가 부모 클래스의 모든 프로퍼티들과 메서드들을 상속하게 해 줄 것입니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
super() 함수를 사용함으로써 당신은 부모 요소의 이름을 따르지 않아도 되게 됩니다. 그것은 부모로부터 자동적으로 메서드와 프로퍼티를 상속합니다.
프로퍼티 추가하기
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
아래의 예시에서는 2019라는 년도가 변경 가능하며 student객체를 생성할 때 Student클래스에 전달됩니다. 그렇게 하려면 __init__() 함수에 또 다른 매개변수를 추가해야 합니다.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Mike", "Olsen", 2019)
메서드 추가하기
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student("Mike", "Olsen", 2019)
x.welcome()
만약 당신이 자식 클래스에 부모 클래스에 있는 함수와 같은 이름으로 메서드를 추가한다면, 상속받은 부모 메서드는 오버라이드될(덮어씌워질) 것이다.
'Python > 공부내용' 카테고리의 다른 글
Python 공부내용 21.10.09. Python - Scope(범위) (0) | 2021.10.09 |
---|---|
Python 공부내용 21.10.09. Python - Iterators(반복자) (0) | 2021.10.09 |
Python 공부내용 21.10.08. Python - Classes / Objects (0) | 2021.10.08 |
Python 공부내용 21.10.07. Python- Arrays (0) | 2021.10.07 |
Python 공부내용 21.10.07. Python - Lambda (0) | 2021.10.07 |