- 객체지향 프로그래밍
- 객체와 클래스
- 클래스 상속
객체를 이용한 프로그램으로 객체는 속성과 기능으로 구성된다.

[출처이미지]제로베이스 취업스쿨
객체는 클래스에서 생성된다.

[출처이미지]제로베이스 취업스쿨
객체는 원하는 만큼 생성이 가능하다.
클래스는 class 키워드와 속성(변수) 그리고 기능(함수)를 이용해서 만든다.

[출처이미지]제로베이스 취업스쿨
클래스는 시작을 대문자로 시작한다.
객체는 클래스의 생성자를 호출한다.
class Air(): def __init__(self, color, len): self.color = color self.length = len def airPortInfo(self): print(f"self.color : {self.color}") print(f"self.length : {self.length}") def doFly(self): print("비행기 이륙 합니다.") def doStop(self): print("비행기 착륙 합니다.") air1 = Air("blue", 1500) air2 = Air("green" , 2000) air1.airPortInfo() air2.airPortInfo()
class NewGenerationPC: def __init__(self , name, cpu, memory, ssd): self.name = name self.cpu = cpu self.memory = memory self.ssd = ssd def doExcel(self): print("EXCEL Run!!") def doPotoshop(self): print("PHOTOSHOP RUN") def printPCInfo(self): print(f"self.name: {self.name}") print(f"self.cpu: {self.cpu}") print(f"self.memory: {self.memory}") print(f"self.sdd: {self.ssd}") myPC = NewGenerationPC("myPC", "i5","16GB","256GB") myPC.printPCInfo() print("-" * 30) friendPC = NewGenerationPC("friendPC", "i7","32GB","512GB") friendPC.printPCInfo() print("-" * 30) myPC.cpu = 'i9' myPC.memory = '32GB' myPC.ssd = '1T' myPC.printPCInfo()
class Address: def __init__(self, color, weight): self.color = color self.weight = weight def printInfo(self): print(f"address1 : {self.color}") print(f"address2 : {self.weight}") addr1 = Address("blue",300) addr2 = Address("green",100) addr3 = addr1 addr1.printInfo() addr2.printInfo() addr3.printInfo() print("속성 변경 후 확인") addr1.weight = 100 addr1.color = "black" addr1.printInfo() addr2.printInfo() addr3.printInfo()
class TemCls: def __init__(self,n,s): self.number = n self.str = s def printClsInfo(self): print(f"self.number : {self.number}") print(f"self.str : {self.str}") import copy tc1 = TemCls(10,"Hello") tc2 = copy.copy(tc1) tc1.printClsInfo() tc2.printClsInfo() print("깊은복사 ------") tc1.number = 20 tc1.str = "bye" tc1.printClsInfo() tc2.printClsInfo()

[출처이미지]제로베이스 취업스쿨
class NormalCar: def drive(self): print("[NormalCar] drivee() called!!") def back(self): print("[NormalCar] back() called!!") class TurboCar(NormalCar): def turbo(self): print("[TurboCar] turbo() called!!") myTurboCar = TurboCar() myTurboCar.turbo() myTurboCar.drive() myTurboCar.back()