Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- php
- 자바스크립트 객체
- 보안뉴스요약
- 자바스크립트 기본 문법
- 랜섬웨어
- 카카오프로젝트100
- 파이썬
- 자바스크립트 API
- 보안뉴스 요약
- Oracle SQL
- 자바스크립트 element api
- 보안뉴스 한줄요약
- 보안뉴스
- 깃허브
- 다크웹
- 보안뉴스한줄요약
- oracle
- numpy
- ES6
- 오라클
- python
- 카카오프로젝트
- 카카오프로젝트 100
- 자바스크립트
- 자바스크립트 jQuery
- oracle db
- javascript
- GIT
- 자바스크립트 prototype
- 자바스크립트 node
Archives
- Today
- Total
FU11M00N
[Python] 상속, 메소드 오버라이딩 본문
- 클래스 상속
• 기존 클래스는 그대로 유지한 채로 기존 클래스에 다른 기능을
추가할 때 사용하는 기능입니다.
• 기반(base) 클래스(super class) : 물려주는 클래스
• 파생(derived) 클래스(sub class): 상속을 받아 새롭게 만드는 클래스
- 형식
class 기반 클래스명 :
코드
class 파생 클래스명(기반 클래스명) :
코드
- 예제 1
class Person:
def __init__(self):
print('Person __init__')
self.hello = "안녕하세요"
class Student(Person):
def __init__(self):
print('Person __init__')
super().__init__()
self.school = "파이썬 학교"
james = Student()
print(james.school)
print(james.hello)

- 예제 2
class Father:
def handsome(self):
print("잘생겼다")
class Brother(Father):
'''아들'''
class Sister(Father):
def pretty(self):
print("예쁘다")
def handsome(self):
super().handsome()
brother = Brother()
brother.handsome()
girl = Sister()
girl.handsome()
girl.pretty()

- 예제 3
class Father(): #부모 클래스
def __init__(self,who):
self.who = who
def handsome(self):
print("{}를 닮아 잘생겼다.".format(self.who))
class Sister(Father):
def __init__(self,who,where):
super().__init__(who)
self.where = where
def choice(self):
print("{} 말이야 ". format(self.where))
def handsome(self):
super().handsome()
self.choice()
girl = Sister("아빠",'얼굴')
girl.handsome()

현재 클래스가 특정 클래스의 파생 클래스인지 확인할 때는
issubclass () 함수를 사용합니다.
• 파생 클래스이면 True, 아니면 False를 반환
issubclass(Student, Person)
True
상위 클래스에 존재하는 메소드나 속성을 사용할 때는 super()을
이용해서 호출이 가능합니다.
-메소드 오버라이딩
• 상위 클래스에 존재하는 메소드를 하위 클래스에서 재정의해서 사용하
는 것
- 예제 4
#메소드 오버라이딩
class Person:
def greeting(self):
print("안녕하세요")
class Student(Person):
def greeting(self):
print("안녕하세요. 반갑습니다.")
james = Student()
james.greeting()

- 예제 5
class Country:
"""super Class"""
name = "국가명"
population = "인구"
capital = "수도"
def show(self):
print("국가 클래의 메소드 입니다.")
class Korea(Country):
"""sub class"""
def __init__(self,name,population,capital):
self.name = name
self.population = population
self.capital = capital
def show_name(self):
print("국가 이름은 : ",self.name)
def show(self):
super().show()
print("""
국가의 이름은 {} 입니다.
국가의 인구는 {} 입니다.
국가의 수도는 {} 입니다.
""".format(self.name, self.population, self.capital))
a=Korea("대한민국",50000000,"서울")
a.show()

- 예제 6
class Calculate():
type = 'low'
def __init__(self,n1,n2):
self.n1 = n1
self.n2 = n2
def sum(self):
print(self.n1+ self.n2)
class Calculate_1(Calculate):
type = 'high'
def sub(self):
print(self.n1 - self.n2)
def mul(self):
print(self.n1 * self.n2)
c1 = Calculate_1(4,2)
print(c1.type)
c1.sum()
c1.sub()
c1.mul()
class Calculate_2(Calculate_1):
type = "Very high"
def div(self):
print(self.n1 / self.n2)
def mul(self):
result = self.n1 * self.n2
print("곱셈 결과는 %s 입니다" %result)
c2 = Calculate_2(4,2)
print(c2.type)
c2.sum()
c2.sub()
c2.div()
c2.mul()

'Programming > Python' 카테고리의 다른 글
[Python] 추상 클래스 (0) | 2020.11.24 |
---|---|
[Python]다중 상속 (0) | 2020.11.24 |
[Python] 파이썬으로 구현한 연봉 인상율 프로그램 (0) | 2020.11.17 |
[Python] 정적 메소드 (0) | 2020.11.17 |
[Python] 파이썬 생성자와 소멸자 (0) | 2020.11.10 |