class Person:
def __init__(self, name, phone=None):
self.name = name
self.phone = phone
def __str__(self): # __는 예약어인데 __str__은 toString이다 즉. 객체 내에 있는것을 보여준다.
return '<Person %s %s>' % (self.name, self.phone)
class Employee(Person):
def __init__(self, name, phone, position, salary):
Person.__init__(self, name, phone)
self.position = position
self.salary = salary
p1 = Person('이광현', 1498)
print("p1.name-->",p1.name)
m1 = Employee('이규현', 5564, '대리', 200)
m2 = Employee('이금비', 8546, '과장', 300)
print("m1.name, m1.position-> ", m1.name, m1.position)
print("m1->", m1)
print("m2.name, m2.position-> ", m2.name, m2.position)
print("m2->", m2)
p1.name--> 이광현
m1.name, m1.position-> 이규현 대리
m1-> <Person 이규현 5564>
m2.name, m2.position-> 이금비 과장
m2-> <Person 이금비 8546>
# 클래스의 다중상속
# : 2개 이상의 부모 클래스로 부터 상속 받는것을 의미함.
# 자바에서는 안됬다 다중상속이 근데 파이썬은 가능함
class Add: # 부모 클래스
def add(self, n1,n2):
return n1 + n2
class Multiply: #부모 클래스
def multiply(self, n1, n2):
return n1 * n2
# 클래스의 다중상속 (Add, Multiply 클래스를 상속 받는다)
class Calculator(Add, Multiply): # 자식 클래스
def sub(self, n1, n2):
return n1 - n2
cal = Calculator()
print(cal.add(1,2))
print(cal.multiply(3,2))
print(cal.sub(3,2))
3
6
1