# 01 클래스(Class)의 기초
class Car(object):
pass
# 02 클래스(class)의 인스턴스(instance) 생성
class Car(object):
pass
my_car = Car()
# 03 클래스 맴버 변수(Class member variables)
class Car(object):
condition = "new"
my_car = Car()
# 04 클래스 맴버 변수(Class member variables) 호출하기
class Car(object):
condition = "new"
my_car = Car()
print (my_car.condition)
new
# 05 클래스(Class) 초기화하기
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
my_car = Car("DeLorean", "silver", 88)
print (my_car.condition)
new
# 06 맴버 변수(member variables) 참조하기
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
my_car = Car("DeLorean", "silver", 88)
print (my_car.condition)
print (my_car.model)
print (my_car.color)
print (my_car.mpg)
new DeLorean silver 88
# 07 클래스 메소드(method) 생성하기
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
my_car = Car("DeLorean", "silver", 88)
print (my_car.display_car())
This is a silver DeLorean with 88 MPG.
# 08 맴버 변수(member variables) 수정하기
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
def drive_car(self):
self.condition = "used"
my_car = Car("DeLorean", "silver", 88)
print (my_car.condition)
my_car.drive_car()
print (my_car.condition)
new used
# 09 상속(Inheritance)
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
def drive_car(self):
self.condition = "used"
class ElectricCar(Car):
def __init__(self, battery_type, model, color, mpg):
self.battery_type = battery_type
self.model = model
self.color = color
self.mpg = mpg
my_car = ElectricCar("molten salt", "DeLorean", "silver", 88)
# 10 메소드 오버라이드 (Overriding Methods)
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
def drive_car(self):
self.condition = "used"
class ElectricCar(Car):
def __init__(self, battery_type, model, color, mpg):
self.battery_type = battery_type
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self):
self.condition = "like new"
my_car = ElectricCar("molten salt", "DeLorean", "silver", 88)
print (my_car.condition)
my_car.drive_car()
print (my_car.condition)
new like new
# 11 유용한 클래스(Classes) 만들기
class Point3D(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return "(%s, %s, %s)" % (self.x, self.y, self.z)
my_point = Point3D(1, 2, 3)
print (my_point)
(1, 2, 3)