α„‚ πŸ”Ά [5 일차] : Flipped Units 34,35

백건·2022λ…„ 1μ›” 21일
0

Class μ‚¬μš©ν•˜κΈ°

  • νŠΉμ •ν•œ κ°œλ…μ΄λ‚˜ λͺ¨μ–‘μœΌλ‘œ μ‘΄μž¬ν•˜λŠ” 것
  • ν”„λ‘œκ·Έλž˜λ°μœΌλ‘œ 객체λ₯Ό λ§Œλ“€ λ•Œ μ‚¬μš©ν•˜λŠ” 것이 클래슀
  • 클래슀둜 객체λ₯Ό λ§Œλ“¦
  • 데이터λ₯Ό 클래슀의 속성(attribute)
  • κΈ°λŠ₯을 method

ν΄λž˜μŠ€μ™€ λ©”μ„œλ“œ λ§Œλ“€κΈ°

class ν΄λž˜μŠ€μ΄λ¦„:
    def λ©”μ„œλ“œ(self):
        μ½”λ“œ
class Person:
    def greeting(self):
        print('Hello')
# μΈμŠ€ν„΄μŠ€ = 클래슀()
james = Person() 

λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°.

# μΈμŠ€ν„΄μŠ€.λ©”μ„œλ“œ()
james.greeting()
Hello

νŒŒμ΄μ¬μ—μ„œ ν”νžˆ λ³Ό 수 μžˆλŠ” 클래슀

a = int(10)
a
10
b = list(range(10))
b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
c = dict(x=10, y=20)
c
{'x': 10, 'y': 20}
b = list(range(10))
b.append(20)
b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
# type(객체)
a = 10
b = [0, 1, 2]
type(b)
list
c = {'x':10, 'y':20}
type(c)
dict
maria = Person()
type(maria)
__main__.Person

μΈμŠ€ν„΄μŠ€μ™€ 객체의 차이점

  • μΈμŠ€ν„΄μŠ€μ™€ κ°μ²΄λŠ” 같은 것.
  • 객체만 지칭할 λ•Œ 객체
  • 객체가 ν΄λž˜μŠ€μ™€ 연관지을 λ•Œ μΈμŠ€ν„΄μŠ€λΌ 함.
# 리슀트 λ³€μˆ˜ a, b λŠ” 객체
# a, bλŠ” list 클래슀의 μΈμŠ€ν„΄μŠ€
# μ‚¬λžŒμ΄λƒ ν•œκ΅­ μ‚¬λžŒμ΄λƒ 인거지....

a = list(range(10))
b = list(range(20))

빈클래슀 λ§Œλ“€κΈ°

class Person:
    pass

λ©”μ„œλ“œ μ•ˆμ—μ„œ λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°

class Person:
    def greeting(self):
        print('Hello')
 
    def hello(self):
        self.greeting()    # self.λ©”μ„œλ“œ() ν˜•μ‹μœΌλ‘œ 클래슀 μ•ˆμ˜ λ©”μ„œλ“œλ₯Ό 호좜
 
james = Person()
james.hello()    # Hello
Hello

νŠΉμ • 클래슀의 μΈμŠ€ν„΄μŠ€μΈμ§€ ν™•μΈν•˜κΈ°

class Person:
    pass

james = Person()
isinstance(james, Person)
True
# isinstance λŠ” 객체의 μžλ£Œν˜• νŒλ‹¨ μ‹œ μ‚¬μš© (νŠΉμ • 쑰건으둜 μ‚¬μš©)
def factorial(n):
    if not isinstance(n, int) or n < 0:    # n이 μ •μˆ˜κ°€ μ•„λ‹ˆκ±°λ‚˜ 음수이면 ν•¨μˆ˜λ₯Ό 끝냄
        return None
    if n == 1:
        return 1
    return n * factorial(n - 1)

λ©”μ„œλ“œμ—μ„œ μΈμŠ€ν„΄μŠ€ 속성에 μ ‘κ·Όν•  λ•Œ

self.λ³€μˆ˜
class Person:
    def __init__(self, name):
        self.name = name
self.name
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

/tmp/ipykernel_1427/2604046875.py in <module>
      2     def __init__(self, name):
      3         self.name = name
----> 4 self.name()


NameError: name 'self' is not defined

속성 μ‚¬μš©ν•˜κΈ°

ν΄λž˜μŠ€μ—μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€ λ•Œ ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œinit

# 속성을 λ§Œλ“€λ•ŒλŠ”  __int__ λ©”μ„œλ“œ μ•ˆμ—μ„œ self.속성에 값을 ν• λ‹Ή

class ν΄λž˜μŠ€μ΄λ¦„:
    def __init__(self):
        self.속성 = κ°’
# Person 클래슀의 __init__ λ©”μ„œλ“œμ—μ„œ self.hello에 'μ•ˆλ…•ν•˜μ„Έμš”.' 인사말
class Person:
    def __init__(self):
        self.hello = 'μ•ˆλ…•ν•˜μ„Έμš”.'

# greeting λ©”μ„œλ“œλ₯Ό μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€. greeting λ©”μ„œλ“œμ—μ„œλŠ” print둜 self.helloλ₯Ό 좜λ ₯
    def greeting(self):
        print(self.hello)

# Person 클래슀둜 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“¦
james = Person()

# greeting λ©”μ„œλ“œλ₯Ό 호좜
james.greeting()    # self.hello에 μ €μž₯된 'μ•ˆλ…•ν•˜μ„Έμš”.' κ°€ 좜λ ₯
μ•ˆλ…•ν•˜μ„Έμš”.

self의 의미

123

μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€ λ•Œ κ°’ λ°›κΈ°

class ν΄λž˜μŠ€μ΄λ¦„:
    def __init__(self, λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2):
        self.속성1 = λ§€κ°œλ³€μˆ˜1
        self.속성2 = λ§€κ°œλ³€μˆ˜2
class Person:
    def __init__(self, name, age, address): # self λ‹€μŒμ— name, age, addressλ₯Ό 지정
        self.hello = 'μ•ˆλ…•ν•˜μ„Έμš”.'
        self.name = name
        self.age = age
        self.address = address
# λ©”μ„œλ“œ μ•ˆμ—μ„œλŠ” self.name = name처럼 λ§€κ°œλ³€μˆ˜λ₯Ό κ·ΈλŒ€λ‘œ self에 λ„£μ–΄μ„œ μ†μ„±μœΌλ‘œ
 
    
    def greeting(self): # greeting λ©”μ„œλ“œλŠ” 인사λ₯Ό ν•˜κ³  이름을 좜λ ₯
        print('{0} μ €λŠ” {1}μž…λ‹ˆλ‹€.'.format(self.hello, self.name)) 
        # name 속성에 μ ‘κ·Όν•  λ•ŒλŠ” self.name처럼 μ‚¬μš©
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™') # μΈμŠ€ν„΄μŠ€ = 클래슀()
maria.greeting()    # μ•ˆλ…•ν•˜μ„Έμš”. μ €λŠ” λ§ˆλ¦¬μ•„μž…λ‹ˆλ‹€. //maria μΈμŠ€ν„΄μŠ€μ˜ greeting λ©”μ„œλ“œλ₯Ό 호좜

#  클래슀 λ°”κΉ₯μ—μ„œ 속성에 μ ‘κ·Όν•  λ•ŒλŠ” μΈμŠ€ν„΄μŠ€.속성 ν˜•μ‹μœΌλ‘œ μ ‘κ·Ό
print('이름:', maria.name)       # λ§ˆλ¦¬μ•„
print('λ‚˜μ΄:', maria.age)        # 20
print('μ£Όμ†Œ:', maria.address)    # μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™
μ•ˆλ…•ν•˜μ„Έμš”. μ €λŠ” λ§ˆλ¦¬μ•„μž…λ‹ˆλ‹€.
이름: λ§ˆλ¦¬μ•„
λ‚˜μ΄: 20
μ£Όμ†Œ: μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™

0

# 클래슀의 μœ„μΉ˜ 인수
# **args -> args[0]
class Person:
    def __init__(self, *args):
        self.name = args[0]
        self.age = args[1]
        self.address = args[2]
 
maria = Person(*['λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™']) 
# maria μΈμŠ€ν„΄μŠ€ Person 클래슀의 name, age, address 속성
# 클래슀의 ν‚€μ›Œλ“œ μΈμˆ˜μ™€ λ”•μ…”λ„ˆλ¦¬ μ–ΈνŒ¨ν‚Ή 
# **kwargs kwargs['name']
class Person:
    def __init__(self, **kwargs):    # ν‚€μ›Œλ“œ 인수
        self.name = kwargs['name']
        self.age = kwargs['age']
        self.address = kwargs['address']
 
maria1 = Person(name='λ§ˆλ¦¬μ•„', age=20, address='μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™')
maria2 = Person(**{'name': 'λ§ˆλ¦¬μ•„', 'age': 20, 'address': 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™'})
# μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•œ 뒀에 속성 μΆ”κ°€
# 
# Person ν΄λž˜μŠ€λŠ” 빈 ν΄λž˜μŠ€μ΄μ§€λ§Œ μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“  λ’€ name 속성을 μΆ”κ°€
class Person:
    pass
maria = Person()         # μΈμŠ€ν„΄μŠ€ 생성
maria.name = 'λ§ˆλ¦¬μ•„'    # μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“  λ’€ 속성 μΆ”κ°€
maria.name
'λ§ˆλ¦¬μ•„'
# ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€λ§Œ 생성 λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€μ—ˆμ„ λ•Œ μΆ”κ°€ν•œ 속성은 생성 μ•ˆλ¨
james = Person()    # james μΈμŠ€ν„΄μŠ€ 생성
james.name    # maria μΈμŠ€ν„΄μŠ€μ—λ§Œ name 속성을 μΆ”κ°€ν–ˆμœΌλ―€λ‘œ james μΈμŠ€ν„΄μŠ€μ—λŠ” name 속성이 μ—†μŒ
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1427/1051155537.py in <module>
      1 # ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€λ§Œ 생성
      2 james = Person()    # james μΈμŠ€ν„΄μŠ€ 생성
----> 3 james.name    # maria μΈμŠ€ν„΄μŠ€μ—λ§Œ name 속성을 μΆ”κ°€ν–ˆμœΌλ―€λ‘œ james μΈμŠ€ν„΄μŠ€μ—λŠ” name 속성이 μ—†μŒ


AttributeError: 'Person' object has no attribute 'name'
#
#
class Person:
    def greeting(self):
        self.hello = 'μ•ˆλ…•ν•˜μ„Έμš”'    # greeting λ©”μ„œλ“œμ—μ„œ hello 속성 μΆ”κ°€

maria = Person()
maria.hello    # 아직 hello 속성이 μ—†μŒ
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1427/2501446103.py in <module>
      6 
      7 maria = Person()
----> 8 maria.hello    # 아직 hello 속성이 μ—†μŒ


AttributeError: 'Person' object has no attribute 'hello'
maria.greeting()    # greeting λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ•Ό
maria.hello         # hello 속성이 생성됨
'μ•ˆλ…•ν•˜μ„Έμš”'
# νŠΉμ • μ†μ„±λ§Œ ν—ˆμš©ν•˜κ³  λ‹€λ₯Έ 속성은 μ œν•œ
# ν΄λž˜μŠ€μ—μ„œ __slots__에 ν—ˆμš©ν•  속성 이름을 리슀트둜
# __slots__ = ['속성이름1, '속성이름2']
class Person:
    __slots__ = ['name', 'age']    # name, age만 ν—ˆμš©(λ‹€λ₯Έ 속성은 생성 μ œν•œ)

maria = Person()
maria.name = 'λ§ˆλ¦¬μ•„'                     # ν—ˆμš©λœ 속성
maria.age = 20                            # ν—ˆμš©λœ 속성
maria.address = 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™'    # ν—ˆμš©λ˜μ§€ μ•Šμ€ 속성은 μΆ”κ°€ν•  λ•Œ μ—λŸ¬κ°€ λ°œμƒν•¨
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1427/1073807025.py in <module>
      5 maria.name = 'λ§ˆλ¦¬μ•„'                     # ν—ˆμš©λœ 속성
      6 maria.age = 20                            # ν—ˆμš©λœ 속성
----> 7 maria.address = 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™'    # ν—ˆμš©λ˜μ§€ μ•Šμ€ 속성은 μΆ”κ°€ν•  λ•Œ μ—λŸ¬κ°€ λ°œμƒν•¨


AttributeError: 'Person' object has no attribute 'address'

λΉ„κ³΅κ°œ 속성 μ‚¬μš©ν•˜κΈ°

Person ν΄λž˜μŠ€μ—λŠ” hello, name, age, address 속성

class Person:
    def __init__(self, name, age, address):
        self.hello = 'μ•ˆλ…•ν•˜μ„Έμš”.'
        self.name = name
        self.age = age
        self.address = address
# 이 속성듀은 λ©”μ„œλ“œμ—μ„œ self둜 μ ‘κ·Όν•  수 있고, μΈμŠ€ν„΄μŠ€.속성 ν˜•μ‹μœΌλ‘œ 클래슀 λ°”κΉ₯μ—μ„œλ„ μ ‘κ·Ό
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™')
maria.name
'λ§ˆλ¦¬μ•„'
# λΉ„κ³΅κ°œ 속성은 __속성과 같이 이름이 __(밑쀄 두 개)둜 μ‹œμž‘
class ν΄λž˜μŠ€μ΄λ¦„:
    def __init__(self, λ§€κ°œλ³€μˆ˜)
        self.__속성 = κ°’
  File "/tmp/ipykernel_1427/1631572235.py", line 3
    def __init__(self, λ§€κ°œλ³€μˆ˜)
                            ^
SyntaxError: invalid syntax
class Person:
    def __init__(self, name, age, address, wallet):
        self.name = name
        self.age = age
        self.address = address
        self.__wallet = wallet    # λ³€μˆ˜ μ•žμ— __λ₯Ό λΆ™μ—¬μ„œ λΉ„κ³΅κ°œ μ†μ„±μœΌλ‘œ λ§Œλ“¦
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™', 10000)
maria.__wallet -= 10000    # 클래슀 λ°”κΉ₯μ—μ„œ λΉ„κ³΅κ°œ 속성에 μ ‘κ·Όν•˜λ©΄ μ—λŸ¬κ°€ λ°œμƒν•¨
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1427/3017173123.py in <module>
      7 
      8 maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™', 10000)
----> 9 maria.__wallet -= 10000    # 클래슀 λ°”κΉ₯μ—μ„œ λΉ„κ³΅κ°œ 속성에 μ ‘κ·Όν•˜λ©΄ μ—λŸ¬κ°€ λ°œμƒν•¨


AttributeError: 'Person' object has no attribute '__wallet'
# λΉ„κ³΅κ°œ 속성은 클래슀 μ•ˆμ˜ λ©”μ„œλ“œμ—μ„œλ§Œ μ ‘κ·Ό
class Person:
    def __init__(self, name, age, address, wallet):
        self.name = name
        self.age = age
        self.address = address
        self.__wallet = wallet    # λ³€μˆ˜ μ•žμ— __λ₯Ό λΆ™μ—¬μ„œ λΉ„κ³΅κ°œ μ†μ„±μœΌλ‘œ λ§Œλ“¦
 
    def pay(self, amount):
        self.__wallet -= amount   # λΉ„κ³΅κ°œ 속성은 클래슀 μ•ˆμ˜ λ©”μ„œλ“œμ—μ„œλ§Œ μ ‘κ·Όν•  수 있음
        print('이제 {0}원 λ‚¨μ•˜λ„€μš”.'.format(self.__wallet))
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™', 10000)γ…‡γ…‡
maria.pay(3000)
이제 7000원 λ‚¨μ•˜λ„€μš”.
# μ•„λž˜ λΆ€λΆ„μ²˜λŸΌ 클래슀 속성을 λ°”κΉ₯으둜 λ“œλŸ¬λ‚΄μ§€ μ•Šμ„ λ•Œ μ‚¬μš© κ°€λŠ₯.
    def pay(self, amount):
        if amount > self.__wallet:    # μ‚¬μš©ν•˜λ €κ³  ν•˜λŠ” κΈˆμ•‘λ³΄λ‹€ 지갑에 λ“  돈이 적을 λ•Œ
            print('돈이 λͺ¨μžλΌλ„€...')
            return
        self.__wallet -= amount
        
  File "/tmp/ipykernel_1427/2524260839.py", line 2
    def pay(self, amount):
    ^
IndentationError: unexpected indent
class Person:
    def __init__(self, name, age, address, wallet):
        self.name = name
        self.age = age
        self.address = address
        self.__wallet = wallet    # λ³€μˆ˜ μ•žμ— __λ₯Ό λΆ™μ—¬μ„œ λΉ„κ³΅κ°œ μ†μ„±μœΌλ‘œ λ§Œλ“¦
 
    def pay(self, amount):
        if amount > self.__wallet:    # μ‚¬μš©ν•˜λ €κ³  ν•˜λŠ” κΈˆμ•‘λ³΄λ‹€ 지갑에 λ“  돈이 적을 λ•Œ
            print('돈이 λͺ¨μžλΌλ„€...')
            return
        self.__wallet -= amount
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™', 10000)
maria.pay(100000) # μ§€λΆˆν•  가격
돈이 λͺ¨μžλΌλ„€...

클래슀 λ°”κΉ₯μ—μ„œ μ ‘κ·Όν•  수 μžˆλŠ” 속성을 곡개 속성(public attribute)이라 λΆ€λ₯΄κ³ ,
클래슀 μ•ˆμ—μ„œλ§Œ μ ‘κ·Όν•  수 μžˆλŠ” 속성을 λΉ„κ³΅κ°œ 속성(private attribute)이라 λΆ€λ¦…λ‹ˆλ‹€.

λΉ„κ³΅κ°œ λ©”μ„œλ“œ μ‚¬μš©ν•˜κΈ°

# μ†μ„±λΏλ§Œ μ•„λ‹ˆλΌ λ©”μ„œλ“œλ„ 이름이 __(밑쀄 두 개)둜 μ‹œμž‘ν•˜λ©΄ 클래슀 μ•ˆμ—μ„œλ§Œ ν˜ΈμΆœν•  수 μžˆλŠ” λΉ„κ³΅κ°œ λ©”μ„œλ“œκ°€ 됩
class Person:
    def __greeting(self):
        print('Hello')
 
    def hello(self):
        self.__greeting()    # 클래슀 μ•ˆμ—μ„œλŠ” λΉ„κ³΅κ°œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 있음
 
james = Person()
james.__greeting()    # μ—λŸ¬: 클래슀 λ°”κΉ₯μ—μ„œλŠ” λΉ„κ³΅κ°œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 μ—†μŒ
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1427/199009468.py in <module>
      7 
      8 james = Person()
----> 9 james.__greeting()    # μ—λŸ¬: 클래슀 λ°”κΉ₯μ—μ„œλŠ” λΉ„κ³΅κ°œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 μ—†μŒ


AttributeError: 'Person' object has no attribute '__greeting'

ν€΄μ¦ˆ

μ—°μŠ΅λ¬Έμ œ : κ²Œμž„ 캐릭터 클래슀 λ§Œλ“€κΈ°

κ²Œμž„ μΊλ¦­ν„°μ˜ λŠ₯λ ₯μΉ˜μ™€ 'λ² κΈ°'κ°€ 좜λ ₯되게

                                            
...
                                            
 
x = Knight(health=542.4, mana=210.3, armor=38) # μš” λ„˜μ΄ ν΄λž˜μŠ€λ„€
print(x.health, x.mana, x.armor) 
x.slash()
class Knight:
    def __init__(self, health, mana, armor):
        self.health = health
        self.mana = mana
        self.armor = armor
 
    def slash(self):
        print('λ² κΈ°')
        
x = Knight(health=542.4, mana=210.3, armor=38)
print(x.health, x.mana, x.armor)
x.slash()
542.4 210.3 38
λ² κΈ°
class ν΄λž˜μŠ€μ΄λ¦„:                                         # Classκ°€ ν¬ν•¨λ˜μ–΄ μžˆλŠ” Global frame
    def λ©”μ„œλ“œ(self(λ§€κ°œλ³€μˆ˜λ₯Ό 끌고 κ°€λŠ” κΈ°μ°¨), λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2, λ§€κ°œλ³€μˆ˜3): # λ©”μ„œλ“œμ™€ λ§€κ°œλ³€μˆ˜λ₯Ό ν•©μ³μ„œ function
        self.속성1 = λ§€κ°œλ³€μˆ˜1
        self.속성2 = λ§€κ°œλ³€μˆ˜2
        self.속성3 = λ§€κ°œλ³€μˆ˜3

#μž…λ ₯받을떄?        
λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2, λ§€κ°œλ³€μˆ˜3 = (λ§€κ°œλ³€μˆ˜ κ°’)

μΈμŠ€ν„΄μŠ€ = 클래슀(λ§€κ°œλ³€μˆ˜1=κ°’, λ§€κ°œλ³€μˆ˜2=κ°’, λ§€κ°œλ³€μˆ˜3=κ°’)
print(μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜1, μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜2, μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜3)
μΈμŠ€ν„΄μŠ€.λ©”μ„œλ“œ()

μ‹¬μ‚¬λ¬Έμ œ

ν‘œμ€€ μž…λ ₯으둜 κ²Œμž„ 캐릭터 λŠ₯λ ₯치(체λ ₯, λ§ˆλ‚˜, AP)κ°€ μž…λ ₯λ©λ‹ˆλ‹€.
λ‹€μŒ μ†ŒμŠ€ μ½”λ“œμ—μ„œ μ• λ‹ˆ(Annie) 클래슀λ₯Ό μž‘μ„±ν•˜μ—¬
티버(tibbers) μŠ€ν‚¬μ˜ ν”Όν•΄λŸ‰μ΄ 좜λ ₯되게 λ§Œλ“œμ„Έμš”.
ν‹°λ²„μ˜ ν”Όν•΄λŸ‰μ€ AP * 0.65 + 400이며 AP(Ability Power, μ£Όλ¬Έλ ₯)λŠ” λ§ˆλ²• λŠ₯λ ₯치λ₯Ό λœ»ν•©λ‹ˆλ‹€.

class Annie:
    def __init__(self, health, mana, ability_power):
        self.health = health
        self.mana = mana
        self.ability_power = ability_power
    
    def tibbers(self):
        print('티버: ν”Όν•΄λŸ‰ ' ,ability_power*0.65+400)
health, mana, ability_power = map(float, input().split())
 
x = Annie(health=health, mana=mana, ability_power=ability_power)
x.tibbers()
511.68 334 298
티버: ν”Όν•΄λŸ‰  593.7
class Annie:
    def __init__(self, health, mana, ability_power):
        self.health = health
        self.mana = mana
        self.ability_power = ability_power
    
    def tibbers(ability_powers):
        print('tibber:', 'damage', ability_power*0.65+400)
        
health, mana, ability_power = map(float, input().split())

x = Annie(health=health, mana=mana, ability_power=ability_power)
x.tibbers()
511.68 334 298
tibber: damage 593.7

d

총정리

class ν΄λž˜μŠ€μ΄λ¦„:                                         # Classκ°€ ν¬ν•¨λ˜μ–΄ μžˆλŠ” Global frame
    def λ©”μ„œλ“œ(self(λ§€κ°œλ³€μˆ˜λ₯Ό 끌고 κ°€λŠ” κΈ°μ°¨), λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2, λ§€κ°œλ³€μˆ˜3): # λ©”μ„œλ“œμ™€ λ§€κ°œλ³€μˆ˜λ₯Ό ν•©μ³μ„œ function
        self.속성1 = λ§€κ°œλ³€μˆ˜1
        self.속성2 = λ§€κ°œλ³€μˆ˜2
        self.속성3 = λ§€κ°œλ³€μˆ˜3

#μž…λ ₯받을떄?        
λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2, λ§€κ°œλ³€μˆ˜3 = (λ§€κ°œλ³€μˆ˜ κ°’)

μΈμŠ€ν„΄μŠ€ = 클래슀(λ§€κ°œλ³€μˆ˜1=κ°’, λ§€κ°œλ³€μˆ˜2=κ°’, λ§€κ°œλ³€μˆ˜3=κ°’)
print(μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜1, μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜2, μΈμŠ€ν„΄μŠ€.λ§€κ°œλ³€μˆ˜3)
μΈμŠ€ν„΄μŠ€.λ©”μ„œλ“œ()

클래슀 속성과 정적, 클래슀 λ©”μ„œλ“œ μ‚¬μš©ν•˜κΈ°

클래슀 속성과 μΈμŠ€ν„΄μŠ€ 속성 μ•Œμ•„λ³΄κΈ°

클래슀 속성 μ‚¬μš©ν•˜κΈ°

μΈμŠ€ν„΄μŠ€ 속성 μ‚¬μš©ν•˜κΈ°

λΉ„κ³΅κ°œ 클래슀 속성 μ‚¬μš©ν•˜κΈ°

정적 λ©”μ„œλ“œ μ‚¬μš©ν•˜κΈ°

클래슀 λ©”μ„œλ“œ μ‚¬μš©ν•˜κΈ°

ν€΄μ¦ˆ

μ—°μŠ΅λ¬Έμ œ: λ‚ μ§œ 클래슀 λ§Œλ“€κΈ°

μ‹¬μ‚¬λ¬Έμ œ: μ‹œκ°„ 클래슀 λ§Œλ“€κΈ°

총정리

profile
λ§ˆμΌ€νŒ…μ„ μœ„ν•œ 인곡지λŠ₯ 섀계와 μŠ€νƒ€νŠΈμ—… Log

0개의 λŒ“κΈ€