π νμ΄μ¬μμμ λ€νμ±
Java μΈμ΄μ λ€νμ±μ λ€λ£¨λ©΄μ μ€λ²λΌμ΄λ©κ³Ό μ€λ²λ‘λ©μ λν΄ μμ보μλ€. κ·Έλ λ€λ©΄ Pythonμμλ μΈμ΄μ λ€νμ±μ΄ μλμ§ μ΄ν΄λ³΄λ €κ³ νλ€.
Java μ€λ²λΌμ΄λ©κ³Ό μ€λ²λ‘λ©
π± μ€λ²λ‘λ©(Overloading)
βοΈ νμ΄μ¬μμλ μ€λ²λ‘λ©μ μ μμΌλ‘ μ§μνμ§ μλλ€κ³ νλ€. νμ§λ§ ꡬνμ κ°λ₯νμ§λ§ μ€λ²λ‘λ©μ λ¨λ°νλ©΄ μ½λκ° λ³΅μ‘ν΄μ§λ€.
βοΈ νμ΄μ¬μμλ μ°μ°μ μ€λ²λ‘λ©μ μ¬μ©ν κ²½μ°κ° λ§μ΄ μλλ° μ΄λ μ°μ°μ(λνκΈ°, λΉΌκΈ°, λλκΈ°, κ³±νκΈ°)λ₯Ό κ°μ²΄λΌλ¦¬ μ¬μ©ν μ μκ² νλ κΈ°λ²μΌλ‘ μ°μ°μ μ€λ²λ‘λ©μ μ¬μ©νλ©΄ μλμ κ°μ΄ λμν μ μλ€.
class HouseCho:
lastname = "μ‘°"
def __inint__(self, name):
self.fullname = self.lastname + name
def __add__(self, other):
print(f"{self.fullname}, {other.fullname} κ²°νΌνλ€")
class HouseLee(HouseCho):
lastname = "μ΄"
def travel(self, where, day):
print(f"{self.fullname}, {where}μ¬ν {day}μΌ κ°λ€.")
pey = HouseCho("μμ©")
juliet = HouseLee("μ€λ¦¬μ£")
print(pey + juliet) <---- μ°μ°μ μ€λ²λ‘λ© νΈμΆ
μ‘°μμ©, μ΄μ€λ¦¬μ£ κ²°νΌνλ€.
π± μ€λ²λΌμ΄λ©(Overriding)
βοΈ μ€λ²λΌμ΄λ©μ λΆλͺ¨ ν΄λμ€μμ μ μν ν¨μλ₯Ό μμ ν΄λμ€μμ λ³κ²½νκ±°λ μμλ°μ μ¬μ©νλ κ²μ μλ―Ένλ€. λΆλͺ¨ ν΄λμ€μ ν¨μ μ΄λ¦, κΈ°λ³Έμ μΈ κΈ°λ₯μ κ·Έλλ‘ μ¬μ©νμ§λ§ μνλ ν¨μλ₯Ό 컀μ€ν
νμ¬ μ¬μ©ν λ μ¬μ©νλ€.
class HouseCho:
lastname = "μ‘°"
def __init__(self, name):
self.fullname = self.lastname + name
def travel(self,where):
print("{0}, {1} μ¬νμ κ°λ€." .format(self.fullname, where))
def love(self, other):
print("{0}, {1} μ¬λμ λΉ μ‘λ€." .format(self.fullname, other.fullname))
def __add__(self, other):
print("{0}, {1} κ²°νΌνλ€." .format(self.fullname, other.fullname))
class HouseLee(HouseCho):
lastname = "μ΄"
def travel(self, where, day):
print("{0}, {1}{2} μ¬ν κ°λ€." .format(self.fullname, where, day))
cho = HouseCho("μ΄μΌ")
cho.travel("1κ°μ")
lee = HouseLee("μ μ")
lee.travel("2κ°μ", "15μΌ")