python class์™€ method

Jahoยท2021๋…„ 11์›” 30์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
2/18

ํด๋ž˜์Šค์˜ ์›ํ˜•

#[Step 01:  ํด๋ž˜์Šค ์„ ์–ธ ]
class Test: # object์˜  ํ›„์† ํด๋ž˜์Šค๊ฐ€ ๋˜์–ด ์„ ์กฐ์˜ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค.    object <- Test
    # class UserName :
    # 1.class์•ˆ์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ์ธ์ž๋Š” self๊ฐ€ ์ง€์ •๋˜์–ด์žˆ๋‹ค.
    # 2.๋ชจ๋“  ๋ฉ”์†Œ๋“œ์— self๋กœ ๋ณ€์ˆ˜ ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    
    def empty(self):
       self.data = []

    def add(self, x):
       self.data.append(x)

if __name__ == '__main__':
#[Step 02:  ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ  ]
    my01 = Test()    # ๊ฐ์ฒด ์ƒ์„ฑ
    my02 = Test()
    print(my01) # ์ฃผ์†Œ ์ถœ๋ ฅ
    print(my02)

    my01.empty() #๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
    my01.add(10)
    print(my01.data) # ๊ฐ’ ๋Œ€์ž… ๋ฐ›์€ ๋ณ€์ˆ˜ ํ™•์ธ
    my03 = my01 #my01 ์ฃผ์†Œ ๋ณต์‚ฌ
    print(my03.data) # my01๊ณผ ๊ฐ™์€ data ์ถœ๋ ฅ
    print(my03) # my01์„ ๋ณต์‚ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์†Œ๊ฐ€ ๋™์ผ

# [Step03 :  ๋ฉค๋ฒ„ ํ˜ธ์ถœ]
     my01.empty()
     my02.empty()
     for  i in range(1,6):
         my01.add(i)
     print(my01.data)
     print(my02.data) # my02์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋น„์–ด์žˆ๋‹ค. = []
     print(my01, my02) # ์ฃผ์†Œ ์ถœ๋ ฅ

๐Ÿ’ก
class ์•ˆ์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ์ธ์ž๋Š” self ์ง€์ •
๊ฐ์ฒด์ƒ์„ฑ์€ java์˜ new ์ƒ์„ฑ์ž์™€ ๊ฐ™๋‹ค.

#์ด๋ฆ„ , ์ฃผ์†Œ , ์ „ํ™” ๋ฒˆํ˜ธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Address ๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด์„œ ๋ณ€์ˆ˜๋กœ ๊ฐ’์„ ์ €์žฅํ•ด ๋ณด์ž
# ์ •์  ๋ณ€์ˆ˜  static ๋ณ€์ˆ˜   ํด๋ž˜์Šค.๋ฉค๋ฒ„๋ณ€์ˆ˜
class Address:
   name="Dominica"
   addr = "seoul"
   tel ="02-0000-000"
   def prn(self):  # ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ
       print(Address.name, Address.addr, Address.tel) #์ •์ ํ˜ธ์ถœ
       
if __name__ == '__main__':
   print(Address.name, Address.addr, Address.tel)
   #Dominica seoul 02-0000-000
   Address.name ="1111111111111" #Addressํด๋ž˜์Šค์˜ name์„ ๋ณ€๊ฒฝ
   print(Address.name, Address.addr, Address.tel) # 111๋กœ ๋ณ€๊ฒฝ๋œ ํ›„ ์ถœ๋ ฅ
   #1111111111111 seoul 02-0000-000

   a1 = Address() #์ฃผ์†Œ๋ณต์‚ฌ
   a1.prn()  # ๋ฉ”์†Œ๋“œ์ถœ๋ ฅ
   #1111111111111 seoul 02-0000-000

Address.name == ์ •์ ํ˜ธ์ถœ (ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•œ๋‹ค.)
a1= Address() == ๊ฐ์ฒด์ƒ์„ฑ(์ฃผ์†Œ๋ณต์‚ฌ)

#__class__ : ์ธ์Šคํ„ด์Šค๊ฐ€ ์ž์‹ ์„ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ํŒŒ์ด์„ ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค์˜์—ญ(heap)์—
# ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์—  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
'''
์‚ฌ์› ๋ฒˆํ˜ธ    7์›” ์˜์—…์‹ค์ 
a111          850
b111          750
c111          650
Emp ๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ  ๋ณ€์ˆ˜๋ฅผ 2๊ฐœ ์„ ์–ธํ•œ ํ›„  a1,b1,c1์˜ ์ด๋ฆ„์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ๊ฐ’์„ ์ „๋‹ฌ ํ›„ ์ถœ๋ ฅ ํ•ด ๋ณด์ž.
'''

class Emp:
    empno =0
    result=0
    
if __name__ == '__main__':

    a1=Emp()
    a1.empno ='a111'
    a1.result =850
    print(a1.empno, a1.result)
	# a111 500

    b1=Emp()
    b1.empno ='b111'
    b1.result = 750
    print(b1.empno , b1.result)
	# b111 700

    c1=Emp()
    c1.empno='c111'
    c1.result= 650
    print(c1.empno, c1.result)
	# c111 600
    #์ •์  ํ˜ธ์ถœ
    print(Emp.empno, Emp.result)
    # 0 0 ์ถœ๋ ฅ
    # Empํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ’์€ 0 ์ด๊ธฐ ๋•Œ๋ฌธ
	
    #ํด๋ž˜์Šค ๋ช…๋ช… , ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ์™ธ๋ถ€์—์„œ ์ฐธ์กฐ
    print(Emp.__name__, type(a1.__class__) , b1.__class__ , c1.__class__)
  # Emp <class 'type'> <class '__main__.Emp'> <class '__main__.Emp'>

์บก์Šํ™”

# ์บก์Šํ™” : ์€๋‹‰๋œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์˜คํ”ˆ๋œ ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ’์„ ์ „๋‹ฌ(setXX)  ๋ฐ  ๋ณ€๊ฒฝํ•˜๋Š” ๊ตฌ์กฐ(getXX return)
# getter & setter
class Test:
    __b=100 #๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๊ณ , Test์˜ ๋ฉค๋ฒ„๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. #__ = ๊ฐ•ํ•œ(strong) private
    def __m(self): #ํ•œ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” private ๋ฉ”์†Œ๋“œ
        return 'a'
    def k(self):
        print(self.__m(), self.__b)


if __name__ == '__main__':
    my = Test()
    my.k()
    #print(my.__b)์˜ค๋ฅ˜ ,,!!Test.__b X

__x= ๊ฐ•ํ•œ private ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.

# ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜ a,b ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž. ๋‹จ ์บก์Šํ™”๋กœ ๊ตฌํ˜„ ํ•ด๋ณด์ž.
# ์€๋‹‰๋œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์—๊ฒŒ  setxx์œผ๋กœ ๊ฐ’ ์ „๋‹ฌ ๋ฐ ๋ณ€๊ฒฝํ•˜๊ณ    getxx return ๋ฉ”์†Œ๋“œ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๊ตฌ์กฐ
class Test:
    __a=0  #์ฃผ์†Œ ํžˆ๋“  private ์ดˆ๊ธฐ๊ฐ’์€ ์ƒ์„ฑ์ž์—์„œ ๋Œ€์ž…
    __b=0
    #name ="abc"   def setName():~~
    def setA(self, a):
        self.__a =a  # ๊ฐ์ฒด ์ƒ์„ฑํ›„ ๊ฐ’์„ a ๋กœ ์ „๋‹ฌ๋ฐ›์•„  ๋ฉค๋ฒ„__a ์—๊ฒŒ ๊ฐ’์ „๋‹ฌ ๋ฐ ๋ณ€๊ฒฝ
    def getA(self):
       return self.__a

    def setB(self,b):
        self.__b=b
    def getB(self):
        return self.__b

    def gethap(self):
        return (self.__a+self.__b)

    def gethsub(self):
        return (self.__a - self.__b)


if __name__ == '__main__':
    t1 = Test()
    t1.setA(100) 
    print(t1.getA())
    t1.setB(200)
    print(t1.getB())
    print('hap=',t1.gethap())
    t1.setA(300) # a๊ฐ’ ๋ณ€๊ฒฝ
    print('sub=',t1.gethsub())

์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž

#์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž๋ฅผ ์‚ดํŽด๋ณด์ž.
# ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ๋‚ด๋ถ€ํ˜ธ์ถœ๋˜์–ด ์ƒ์„ฑ๋˜๊ณ  ๋ช…์‹œํ•˜๊ฒŒ ๋˜๋ฉด ๋ช…์‹œ๋œ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
# ์ƒ์„ฑ์ž๋Š” ๋‹จํ•œ๋ฒˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž๋™ ํ˜ธ์ถœ๋˜๋ฉฐ ํ•ด๋‹นํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„๋ฅผ ๋™์  ํ• ๋‹น ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋กœ๋“œํ•˜๊ฒŒ ๋œ๋‹ค.
# ์ƒ์„ฑ์ž์˜ ๋ชฉ์ ์€ ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”์— ์žˆ๋‹ค.

class MyDel:

    #์ƒ์„ฑ์ž๊ฐ€ 2๊ฐœ์ผ ๊ฒฝ์šฐ 2๋ฒˆ์งธ ์ƒ์„ฑ์ž๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. (๋ฎ์–ด์“ฐ๊ธฐ)
    def __init__(self, a=100):  #์ƒ์„ฑ์ž
        self.a=a
        print("__ init__",a)

    # def __init__(self):
    #     print("__ init__")

    def __del__(self):  #์†Œ๋ฉธ์ž ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋Š” ์‹œ์ ์—์„œ ํ˜ธ์ถœ ๋˜๋ฉด์„œ ๋ฆฌ์†Œ์Šค ํ•ด์ œ
        print("๋‚˜ ์†Œ๊ฑฐ๋œ๋‹ค. ํ˜„์žฌ ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ ์ฐธ์กฐํ•œ ๋‹ค๋ฅธํด๋ž˜์Šค๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ€์งˆ๋•Œ  ์†Œ๊ฑฐํ•œ๋‹ค.  ")

if __name__ == '__main__':
    # 1. ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ž
    m1 = MyDel()


    # a1 = MyDel(200)  #MyDel์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๊ฐ์ฒด ์ƒ์„ฑํ•œ๋‹ค.
    # print(a1.a)
    # MyDel.a=10000   #
    # print(MyDel.a, a1.a)

๊ฐ™์€ ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์ถœ๋ ฅ
์†Œ๋ฉธ์ž๋Š” ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋Š” ์‹œ์ ์— ํ˜ธ์ถœ๋œ๋‹ค.

์ƒ์†

#๊ฐ„๋‹จํ•œ  ๊ตฌ์กฐ์˜ ์ƒ์†์„  ๊ตฌํ˜„ํ•ด ๋ณด์ž.
# ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”  Person ํด๋ž˜์Šค์žˆ๋‹ค.
# Studentํด๋ž˜์Šค Person์„ ์ƒ์†์„ ๋ฐ›์•„ ํ•™๋…„๋งŒ ์ถ”๊ฐ€ํ•ด์„œ  ์ด๋ฆ„, ๋‚˜์ด, ํ•™๋…„์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค.
# this, super  = self, super | java => super(name,age,b)  | python => super().__init__(name,age,b)

'''
1. ์ƒ์† ๊ตฌ์กฐ : ํ›„์†ํด๋ž˜์Šค (์„ ์กฐํด๋ž˜์Šค):
2. ํ›„์† ํด๋ž˜์Šค์—์„œ ์„ ์กฐ์˜ ๋ฉค๋ฒ„๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ฐ ์ƒ์† ๋ฐ›๋Š” ๋ฉค๋ฒ„
3. ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
#1. return  self.personInfo() + ",grade :"+ str(self.grade) #self.์„ ์กฐ์˜ ๋ฉค๋ฒ„
#2. return super().personInfo()+ ",grade :"+ str(self.grade) # super().์„ ์กฐ์˜ ๋ฉค๋ฒ„
#3. return Person.personInfo(self)+ ",grade :"+ str(self.grade) #์„ ์กฐ์˜ class๋ช….์„ ์กฐ์˜ ๋ฉค๋ฒ„(self)
'''

class Person:
    __b = 10 #๊ฐ•ํ•œ private ํ˜•์‹์€ ๋ฉค๋ฒ„๋งŒ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค ํ›„์†์€ ํ˜ธ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    def __init__(self, name, age,b): #5. ์„ ์กฐ์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด์„œ ๋ฉค๋ฒ„๋ณ€์ˆ˜์—๊ฒŒ ๊ฐ’์ „๋‹ฌ
        self.name = name
        self.age = age
        self.__b=b
    def personInfo(self):  #๋ฉค๋ฒ„๋ณ€์ˆ˜ ์ถœ๋ ฅ์šฉ ๋ฉ”์†Œ๋“œ  + ์—ฐ๊ฒฐ์—ฐ์‚ฐ์ž๋Š” ์‹œํ€€์Šค์˜ ๊ฐ™์€ ๊ฐ์ฒด๋ผ๋ฆฌ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. str+str
        return  self.name  +": (age :" +  str(self.age) +")"

class Student(Person): #2
    def __init__(self,name, age,b, grade):#3
        # Person.__init__(self,name,age) #4 ์„ ์กฐ์˜ ์ƒ์„ฑ์ž
        super().__init__(name,age,b) # self๊ฐ€ ํ•„์š”์—†์Œ?
        self.grade = grade #6. ๊ฐ์ฒด ์ƒ์„ฑ ํ•˜๋ฉด์„œ grade๋ณ€์ˆ˜ ๊ฐ’์ „๋‹ฌ

    def GetStudent(self):
        # print("b= " ,  self.__b) #์„ ์กฐ์˜ ๋ฉค๋ฒ„์˜ private์€ ์ƒ์†๋ฐ›์ง€ ๋ชปํ•œ๋‹ค.
        # 1. return  self.personInfo() + ",grade :"+ str(self.grade)
        # 2. return super().personInfo()+ ",grade :"+ str(self.grade) #self์™€๋™์ผ
        return Person.personInfo(self)+ ",grade :"+ str(self.grade) #์„ ์กฐ์˜ class๋ช… + self
if __name__ == '__main__':
    x=Student("Ruri",7,3,5) #1.Student("Ruri",7,3) 7.์ƒ์„ฑ๋œ ์„ ์กฐ์˜ ์ฃผ์†Œ๋ฅผ ๋ฆฌํ„ด
    print(x.GetStudent())

์ƒ์„ฑ์ž ํ˜ธ์ถœ ๋ฐฉ๋ฒ•๊ณผ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•

  • return self.๋ฉ”์†Œ๋“œ์ด๋ฆ„
  • return super().๋ฉ”์†Œ๋“œ์ด๋ฆ„
  • return ํด๋ž˜์Šค.๋ฉ”์†Œ๋“œ์ด๋ฆ„(self)

์„ ์กฐ์™€ ํ›„์†

#super() : ์„ ์กฐ ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋ช…์‹œ์ ์œผ๋กœ ํ›„์† ํด๋ž˜์Šค์—์„œ ์„ ์กฐ์˜ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
#ํ›„์†ํด๋ž˜์Šค์—์„œ ํ›„์†์ด ๊ฐ€์ง„ ๊ฐ’์„ ์„ ์กฐ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋Œ€์ž…ํ•˜๋ ค๋ฉด super()ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
class AA:
    def __init__(self):
        print("๋‚˜ AA ์ƒ์„ฑ์ž ")

class BB(AA):
    def __init__(self):
        super().__init__() #์„ ์กฐ์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœ
        #AA.__init__(self)
        print("๋‚˜ BB ์ƒ์„ฑ์ž ")

if __name__ == '__main__':
    #a1 = AA()  #์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ์ž์œ ์˜์—ญ๊ณต๊ฐ„ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๋œ๋‹ค.
    b1=BB()  # AA()  <- BB() ์„ ์กฐ๊ฐ€ ์ƒ์„ฑ ๋œํ›„  ํ›„์†์ด ์ƒ์„ฑ

ํ›„์† ํด๋ž˜์Šค์—์„œ ์„ ์กฐ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ(๋Œ€์ž…) ํ•˜๋ ค๋ฉด super()ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

class MyScore:
    def __init__(self,kor,eng):
        self.kor =kor
        self.eng= eng
    def getTot(self):
        print("์„ ์กฐ")
        return(self.kor + self.eng)

class MyScore_Sub(MyScore):
    def __init__(self, kor,eng,mat):
        # super().__init__(kor,eng)
        MyScore.__init__(self,kor,eng) #__init__๋ฉ”์†Œ๋“œ ๋™์ผํ•˜๊ฒŒ ๋„ฃ์–ด์ค€๋‹ค
        self.mat=mat
    def getTot(self):
        print("ํ›„์† ๋ฉ”์†Œ๋“œ")
        # return (super().getTot()+ self.mat) #ํ›„์†์ด ์„ ์กฐ์˜ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒƒ
        return MyScore.getTot(self)+ self.mat

if __name__ == '__main__':
    #print(MyScore(20,30).getTot())
    print(MyScore_Sub(100,100,100).getTot()) #์ƒ์†์‹œ์— ์žฌ์ •์˜๋œ ํ›„์†์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

a1 = MyScore(10,20) #๊ฐ์ฒด ์ƒ์„ฑ
    print(a1.getTot())
    

์ถ”์ƒ๋ฉ”์†Œ๋“œ (abstractmethod)

from abc import abstractmethod, ABCMeta
class Base(metaclass=ABCMeta):
    @abstractmethod
    def start(self):
        print("base start")
    @abstractmethod
    def Stop(self):
        print("base stop")

class Cat(Base):
    def start(self):
        print("cat start")
    def Stop(self):
        print("cat stop")
class Duck(Base):
    def start(self):
        print("duck start")
    def Stop(self):
        print("duck stop")
class Puppy(Base):
    def start(self):
        print("puppy start")

    def Stop(self):
        print("puppy stop")
def MY_Call(m):
    print(m.__class__)
    m.start()
    m.Stop()
if __name__ == '__main__':
    MY_Call(Puppy())
    MY_Call(Cat())
    MY_Call(Duck())

@abstractmethod๋ฅผ ๊ฑธ๊ณ  java์˜ ์ถ”์ƒ๋ฉ”์†Œ๋“œ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์ค‘์ƒ์†

#ํ•˜๋‚˜ ์ด์ƒ์˜ ์„ ์กฐ๋ฅผ ๊ฐ€์ง„ ๋‹ค์ค‘์ƒ์†์„ ๋ฐ›์€ ํด๋ž˜์Šค๊ฐ€ ์„ ์กฐ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
#์„ ์กฐ์˜ ์ƒ์„ฑ์ž์—๊ฒŒ ๊ฐ’ ์ „๋‹ฌ ํ•˜๋Š” ๊ฒƒ


class Dog:
    def sound(self):
     print ("bark" )
    def AA(self):
     print("Dog'AA ")

class Cat:
    def sound(self):
        print ("meow")
    def BB(self):
       print("Cat's BB ")

#๋‹ค์ค‘ ์ƒ์†์— ๊ฒฝ์šฐ class๋ช…์œผ๋กœ ๋ช…์‹œํ•˜์—ฌ ํ˜ธ์ถœํ•˜์ž. 1. O ,2.X
class Chimera(Dog,Cat):
    def sound(self):
        #1 ์–ด๋–ค ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์ธ์ง€ ์•Œ์ˆ˜์žˆ๋‹ค
        Cat.sound(self)
        Dog.sound(self)
        #2 ์–ด๋–ค ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์ธ์ง€ ๋ชจ๋ฆ„
        super().sound()
        super().AA()
        print("Chimera")

if __name__ == '__main__':
    Chimera().sound()   #    a1 = Chimera() ;  a1.sound()

๋‹ค์ค‘์ƒ์†์— ๊ฒฝ์šฐ class๋ช…์œผ๋กœ ๋ช…์‹œํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค.
es) Cat.sound(self)

@property (getter & setter)

# @property : ๋ฉ”์†Œ๋“œ๋ฅผ ๋ณ€์ˆ˜๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์ •
class My:
    def __init__(self):
        self.x=0

    @property      #@mFun.getter์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ฐ’์„ ๋ฐ›๋Š” ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜
    def mFun(self):   # ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ x๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” UserMethod    /   print(a.mFun)
        return  self.x

    @mFun.setter
    def mFun(self, x): #๋ฉค๋ฒ„ ๋ณ€์ˆ˜ x์—๊ฒŒ ์ง€์—ญ๋ณ€์ˆ˜ x๊ฐ€ ๊ฐ’์„ ๋ฐ›์•„ ์ „๋‹ฌ ๋ฐ ๋ณ€๊ฒฝํ•˜๋Š” UserMethod
        self.x = x

if __name__ == '__main__':
    a = My()
    a.mFun = 100
    print(a.mFun)

@property == (getter)
@๋ฉ”์†Œ๋“œ์ด๋ฆ„.setter

profile
๊ฐœ๋ฐœ ์˜น์•Œ์ด ๋ถ€ํ„ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€