๐ OOP With Python
Animal ํด๋์ค
class Animal:
def __init__(self):
self.age = 1
def happyBirth(self):
self.age += 1
Human ํด๋์ค
class Human(Animal):
def __init__(self):
super().__init__()
self.money = 10000
def albamon(self):
self.money += 1
๊ฐ์ฒด ์์ฑ
a = Animal()
h = Human()
์ถ๋ ฅ
h = Human()
print(f"๋์ด : {h.age}")
print(f"์์ฐ : {h.money}")
print("happyBirth, albamon ์คํ")
h.albamon()
h.happyBirth()
print(f"๋์ด : {h.age}")
print(f"์์ฐ : {h.money}")
๊ฒฐ๊ณผ
๐ ๋ค์ค์์์ ๋ฐ์ ๋
๋ถ๋ชจ ํด๋์ค
class Xi:
def __init__(self):
self.money = 1000
def steal(self, smoney):
self.money += smoney
class Putin:
def __init__(self):
self.nuclear = 5000
def alzheimer(self):
self.nuclear -= 1
class JungEun:
def __init__(self):
self.missile = 10000
def ssorau(self):
self.missile -= 100
์์ํด๋์ค
class Child(Xi, Putin, JungEun):
def __init__(self):
Xi.__init__(self)
Putin.__init__(self)
JungEun.__init__(self)
def steal(self, smoney):
super().steal(smoney)
def alzheimer(self):
super().alzheimer()
def ssorau(self):
super().ssorau()
- ์ฌ๋ฌ ๋ถ๋ชจํด๋์ค๋ฅผ ์์๋ฐ์ ๋ ๋ถ๋ชจ์์ฑ์์ ํํ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ธํด์ค๋ค.
๋ถ๋ชจ์์ฑ์.__init__(self)
- ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ์์ํด๋์ค์์ ์ ์ธํด์ผํ๋ค.
๊ฐ์ฒด ์์ฑ
ch = Child()
์ถ๋ ฅ
print(f"money : {ch.money}, nuclear : {ch.nuclear}, missile : {ch.missile}")
ch.steal(100)
ch.alzheimer()
ch.ssorau()
print(f"money : {ch.money}, nuclear : {ch.nuclear}, missile : {ch.missile}")
๊ฒฐ๊ณผ
๐ ๊ฐ์ฒด ์๋ฉธ
class Cat:
def __init__(self):
print("constructor")
def crying(self):
print("crying")
def __del__(self):
print("destroyer")
def __str__(self):
return "mew"
- open์ด ๋์ด์๋ ๊ฒ์ ์ ๋ถ closeํด์ค์ผํ๋ค.
- ์์ฑ์ : open, ์๋ฉธ์ : close
์คํ์ฝ๋
c = Cat()
c.crying()
time.sleep(4)
print(c)
๊ฒฐ๊ณผ