Classes & objects · 클래스 & 객체
A class is a blueprint
- A class defines a new kind of thing — a user-defined type.
- An object is one item made from that class.
- One
Dogclass can make many dog objects, each with its own data.
클래스는 설계도(blueprint)입니다
- **클래스(class)**는 새로운 종류의 사물을 정의합니다 — 사용자 정의 유형입니다.
- **オブジェクト(object)**는 해당 클래스로부터 만들어진 하나의实例입니다.
- 하나의
Dogclass에서 여러 개의 dog objects를 만들 수 있으며, 각각 고유한 데이터를 가집니다.
init and attributes
__init__is a special method that runs when you make an object.selfrefers to the object being made.- Values stored on
selfare the object's attributes (its data).
__init__와 속성
__init__는 object를 생성할 때 실행되는 special method입니다.self는 현재 생성되고 있는 object를 가리킵니다.self에 저장된 값들은 object의 속성(attributes) (데이터)입니다.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(2, 5)
print(p.x)
print(p.y)
Methods
- A method is a function defined inside a class.
- Its first parameter is always
self, the object it works on. - Call it with a dot:
object.method().
메소드(methods)
- **메소드(method)**는 클래스 내부에 정의된 함수입니다.
- 첫 번째 파라미터는 항상
self이며, 이는 메소드가 작동하는 object입니다. - 점(.)을 사용하여 호출합니다:
object.method().
class Greeter:
def __init__(self, name):
self.name = name
def hello(self):
return "Hi, " + self.name
g = Greeter("Sam")
print(g.hello())
Enumerated types
- An enumerated type is a fixed set of named values.
from enum import Enumlets you define one as a class.- It is clearer than using loose numbers like
1,2,3.
열거형 types
- 열거형 enum type은 고정된 집합의 명명된 값들입니다.
from enum import Enum는 이를 class로 정의하도록 허용합니다.1,2,3와 같은 임의의 숫자를 사용하는 것보다 명확합니다.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.GREEN)
print(Color.GREEN.value)
In Cambridge pseudocode
- A
CLASSblock lists its data and aNEWconstructor.
캐미지아 가위코드에서
CLASS블록은 데이터를 나열하고NEWconstructor를 포함합니다.
CLASS Point
PRIVATE X : INTEGER
PRIVATE Y : INTEGER
PUBLIC PROCEDURE NEW(startX : INTEGER, startY : INTEGER)
X ← startX
Y ← startY
ENDPROCEDURE
ENDCLASS
DECLARE p : Point
p ← NEW Point(2, 5)
Common mistakes
- Every method takes
selfas its first parameter. __init__runs automatically when you create an object.
흔한 실수
- 모든 메소드는 첫 번째 파라미터로
self를 받습니다. __init__는 object를 생성할 때 자동으로 실행됩니다.
Now you try
- Define each class with
__init__and any methods it needs. - Press Check answer to test your code.
이제 직접 해보기
- 각 class를
__init__와 필요한 메소드들로 정의하십시오. - Answer 확인 버튼을 눌러 코드를 테스트하세요.
Define a class Dog whose __init__ takes name and age and stores them as attributes self.name and self.age. · Dog 클래스를 정의하세요. 이 클래스의 __init__은 name과 age을接受하여 속성 self.name과 self.age으로 저장합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Define a class Rectangle with __init__(self, width, height) and a method area(self) that returns width * height. · **Rectangle** 클래스를 __init__(self, width, height) 속성으로 정의하고, area(self) 메서드를 작성하여 **width * height**을 반환하도록 하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Define a class BankAccount. __init__ sets self.balance to 0. Add deposit(self, amount) to add money and withdraw(self, amount) to take it away. · BankAccount 클래스를 정의하세요. __init__은 self.balance을 0로 설정합니다. 돈을 추가하는 deposit(self, amount)과 빼는 withdraw(self, amount)을 추가하세요.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.