프로그래밍 언어/Python
클래스와 객체 생성
ruby-jieun
2023. 1. 9. 19:42
클래스와 객체 생성
클래스는 class 키워드와 속성(변수) 그리고 기능(함수)를 이용해서 만든다.
객체는 클래스의 생성자를 호출한다.
class Car:
def __init__(self, color, length):
self.color = color
self.length = length
def doStop(self):
print('STOP!!')
def doStart(self):
print('START!!')
객체 2개 생성
car1 = Car('red', 200)
car2 = Car('blue', 300)
class Car:
def __init__(self, color, length):
self.color = color
self.length = length
def doStop(self):
print('STOP!!')
def doStart(self):
print('START!!')
def printCarInfo(self):
print(f'self.color: {self.color}')
print(f'self.length: {self.length}')
car1 = Car('red', 200)
car2 = Car('blue', 300)
car1.printCarInfo()
car2.printCarInfo()