- 객체 지향 프로그래밍
- 객체(Object): 자신만의 고유한 특성과 고유의 행동을 가지고 있다.
- 속성(Attribute): 객체가 가지는 자신만의 고유한 특성, 멤버 변수
- 메서드(method): 객체가 가지는 고유한 행동, 내부함수
- 캡슐화: 정보를 외부에서 접근할 수 없도록 보호하는 것(비공개), 매소드를 통해서만 접근 가능
- 상속: 부모 클래스의 속성과 메서드를 자식 클래스에게 물려줌
- 다형성: 같은 이름의 메서드가 클래스나 객체에 따라 다른 동작을 수행하도록 하는 것
- Class(클래스)
객체(object)를 생성하기 위한 상위개념으로 정의
- 인스턴스 = 클래스()
EX)
>>> class Robot:
... def greeting(self):
... print('Hello') #로봇클래스 정의
...
>>> robot1 = Robot() #로봇인스턴스 생성[인스턴스 = 클래스()]
>>> robot1.greeting() #매서드 호출
Hello #출력
EX)
인스턴스를 만들 때 값을 입력 받도록 코딩
class Robot:
def __init__(self, name, height):
self.hello = 'Hello!'
self.name = name
self.height = height
def greeting(self):
print(f'{self.hello} my name is {self.name}.')
robot1 = Robot('AI-1',2.1)
robot1.greeting()
print(robot1.name)
print(robot1.height)
비공개 속성(private attribute) 사용하기
class Robot:
def __init__(self, name, height, battery=100):
self.hello = 'Hello!'
self.name = name
self.height = height
self.__battery = battery
def work(self, amount):
self.__battery -= amount
if self.__battery < 50:
print(f'Battery has {self.__battery} remaining')
robot1 = Robot('AI-1', 2.1)
robot1.work(70)
print(robot1.name)
- 클래스 - 정적 메서드
인스턴스를 통하지 않고 클래스에서 바로 호출할 수 있는 메서드
EX)
class Calc:
@staticmethod
def add(a,b):
print(a+b)
@staticmethod
def mul(a,b):
print(a*b)
Calc.add(1,2)
Calc.mul(3,4)
- 상속(interitance)
클래스의 속성과 메서드를 물려받는다.
상속 받는 클래스는 괄호 안에 기반 클래스 이름을 넣는다.
EX)
메서드 상속
class Robot:
def greeting(self):
print("Hello!")
class Cookbot(Robot):
def cook(self):
print("Cooking!")
cookbot1 = Cookbot()
cookbot1.greeting()
cookbot1.cook()
속성(attribute) 상속
class Robot:
def __init__(self, name, battery_capacity=100):
self.name = name
self.battery_capacity = battery_capacity
print(f'Hello! my name is {self.name}.')
def work(self):
print(f'{self.name} works for you.')
class Cookbot(Robot):
def cook(self):
print("Cooking!")
robot1 = Cookbot('AI')
robot1.work()
robot1.cook()
기반클래스의 비공개 속성에 접근
=> '_ClassName__attributeName' 형식으로 접근
class Robot:
def __init__(self, name, battery_capacity=100):
self.name = name
self.__battery_capacity = battery_capacity
self.__battery_level = battery_capacity
print(f'Hello! my name is {self.name}.')
def work(self):
self.__battery_level -= 10
print(f'{self.name} is cleaning the room.')
class Cookbot(Robot):
def __init__(self, name, menu):
super().__init__(name)
self.menu = menu
def cook(self):
self._Robot__battery_level -= 20
print(f'{self.name} cooked {self.menu} for you!!')
robot1 = Cookbot('AI', 'Pizza')
robot1.work()
robot1.cook()
- 매서드 오버라이팅(Method Overriding)
기존 부모클래스의 메서드를 덮어쓰는 것
class Robot:
def greeting(self):
print("Hello")
class Cookbot(Robot):
def greeting(self):
print("Hello! I am a Cookbot")
cookbot1 = Cookbot()
cookbot1.greeting()
기존 부모클래스의 메서드를 호출
class Robot:
def greeting(self):
print("Hello")
class Cookbot(Robot):
def greeting(self):
super().greeting()
print("Hello! I am a Cookbot")
cookbot1 = Cookbot()
cookbot1.greeting()
'3-1 > Python' 카테고리의 다른 글
14주차-Exception (0) | 2024.06.11 |
---|---|
13주차-Module, Numpy (1) | 2024.06.04 |
11주차-file read/ write (0) | 2024.05.17 |
10주차-Dictionary and Set (0) | 2024.05.12 |
9주차-Function (0) | 2024.05.12 |