CODING PRACTICE/Python

Object Oriented Programming (객체지향프로그램)

HELLICAT 2024. 7. 18. 12:26
반응형

oop는 Python에만 국한 된것이 아니라 많은 다른 언어들도 oop를 지원하고 있다.

oop를 사용하여 object 중심으로 데이터를 다루는 함수는 매우 직관적이고 생각하기 쉽다. javascript의 class를 생각하면 이해하기 쉽다.

class Charactor:
    def __init__(self): 
        self.name="Anduin"
        self.job = "cleric"
        self.mp=10
        

anduin = Charactor()

print(anduin.name)
print(anduin.job)
print(anduin.mp)

 

Method & Inheritence

class Charactor:
    def __init__(self ,name,job,mp,tribe): 
        self.name = name
        self.job = job
        self.mp = mp
        self.tribe = tribe

    def say_hello(self):
        return print(f'my name is {self.name}. And Iam {self.job}')
    def tell_magicpoint (self):
        return print(f'my Magic power if {self.mp}')
    def tell_my_tribe(self):
        return print(f'I am {self.tribe}')
    
class Hord(Charactor):
    def __init__(self,name,job,mp):
        super().__init__(
            name,
            job,
            mp,
            "Hord"
            )

    def for_hord(self):
        print("for the hord")

class Alience(Charactor):
    def __init__(self,name,job,mp):
        super().__init__(
            name,
            job,
            mp,
            "Alience"
            )

    def for_alience(self):
        print("for the alience")

anduin = Alience(name="anduin",job="cleric", mp=10)
print(anduin.name, anduin.job, anduin.mp )
anduin.say_hello()
anduin.tell_magicpoint()
anduin.for_alience()
anduin.tell_my_tribe()


silvanas = Hord(name="silvanas",job="Hunter", mp=20)
print(silvanas.name, silvanas.job, silvanas.mp )
silvanas.say_hello()
silvanas.tell_magicpoint()
silvanas.for_hord()
silvanas.tell_my_tribe()
728x90