Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- python알고리즘
- 주피터노트북
- 판다스데이터분석
- 주피터노트북데이터분석
- sql따라하기
- python수업
- SQLSCOTT
- python데이터분석
- Python
- 주피터노트북그래프
- sql연습하기
- 수업기록
- sql연습
- 데이터분석시각화
- 파이썬데이터분석
- matplotlib
- 맷플롯립
- 주피터노트북판다스
- 주피터노트북맷플롯립
- 파이썬시각화
- 파이썬차트
- 파이썬크롤링
- SQL수업
- 파이썬수업
- SQL
- 판다스그래프
- 파이썬데이터분석주피터노트북
- 파이썬알고리즘
- 파이썬
- 팀플기록
Archives
- Today
- Total
IT_developers
Python 개념 및 실습 - 클래스(3) 본문
오버라이딩 : 부모클래스가 가지고 있는 메소드 재정의
class Parent:
def func1(self):
print("Parent func1()")
def func2(self):
print("Parent func2()")
class Child(Parent):
def func1(self):
print("Child func1()")
def func3(self):
print("Child func2()")
parent, child = Parent(), Child()
parent.func1()
child.func1()
parent.func2()
child.func2()
child.func3()
# Car ==> upspeed 오버라이딩
class Car:
speed = 0
def up_speed(self, value):
self.speed = self.speed + value
print("현재 속도(부모 클래스) : {}".format(self.speed))
def down_speed(self, value):
self.speed = self.speed - value
class Sedan(Car):
seatNum = 0
def up_speed(self, value):
self.speed = self.speed + value
# 재정의
# 현재 속도가 150보다 크면 현재 속도를 150으로 변경
if self.speed > 150:
self.speed = 150
print("현재 속도(자식 클래스) : {}".format(self.speed))
def getSeatNum(self):
return self.seatNum
class Truck(Car):
capacity = 0
def getCapacity(self):
return self.capacity
sedan1, truck1 = Sedan(), Truck()
sedan1.up_speed(200)
truck1.up_speed(80)
sedan1.seatNum = 5
truck1.capacity = 50
print("승용차의 속도는 {}km, 좌석수는 {}개".format(sedan1.speed, sedan1.getSeatNum()))
print("트럭의 속도는 {}km, 총 중량은 {}톤".format(truck1.speed, truck1.getCapacity()))

private 개념 : 키워드를 사용하진 않음.
# private : "__변수명" 기능
import math
class Circle:
def __init__(self, radius) -> None:
self.__radius = radius
def get_circumference(self):
return 2 * math.pi * self.__radius
def get_circle_area(self):
return math.pi * (self.__radius**2)
def get_radius(self):
return self.__radius
def set_radius(self, radius):
self.__radius = radius
circle = Circle(10)
print("둘레 ", circle.get_circumference())
print("면적 ", circle.get_circle_area())
# print("__radius", circle.__radius) #AttributeError: 'Circle' object has no attribute '__radius'
print("__radous", circle.get_radius())
circle.set_radius(5)
print("둘레 ", circle.get_circumference())
print("면적 ", circle.get_circle_area())

'Python' 카테고리의 다른 글
Python 개념 및 실습 - 모듈(2) (0) | 2022.09.12 |
---|---|
Python 개념 및 실습 - 모듈(1) (0) | 2022.09.12 |
Python 개념 및 실습 - 클래스(2) (0) | 2022.09.11 |
Python 개념 및 실습 - 클래스(1) (1) | 2022.09.11 |
Python 개념 및 실습 - 파일 읽고 쓰기(4) (0) | 2022.09.10 |
Comments