클래스 (프로그래밍)
객체를 생성하기 위한 코드의 청사진 또는 템플릿으로, 상태(변수)와 행동(메서드)을 정의합니다.
클래스는 사용자 정의 데이터 유형 역할을 합니다. 클래스는 OOP의 세 가지 주요 기둥을 지원합니다. 캡슐화(private/public 접근 제어자를 통해 내부 상태를 숨김), 상속(부모 클래스로부터 속성과 메서드를 물려받음), 그리고 다형성(서로 다른 객체가 동일한 인터페이스를 통해 상호작용할 수 있는 능력)입니다.
graph LR
Center["클래스 (프로그래밍)"]:::main
Pre_object_oriented_programming["object-oriented-programming"]:::pre --> Center
click Pre_object_oriented_programming "/terms/object-oriented-programming"
Rel_object["object"]:::related -.-> Center
click Rel_object "/terms/object"
classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
linkStyle default stroke:#4b5563,stroke-width:2px;
🧒 5살도 이해할 수 있게 설명
클래스를 쿠키 커터(틀)로, 객체를 실제 쿠키로 생각해 보세요. 쿠키 커터는 모양을 결정합니다. 같은 쿠키 커터를 사용하여 수백 개의 쿠키를 만들 수 있습니다. 모양은 모두 같지만 어떤 쿠키에는 초콜릿 칩이 있고 다른 쿠키에는 설탕 가루가 뿌려져 있을 수 있습니다(이것이 각 쿠키의 고유한 상태입니다).
🤓 Expert Deep Dive
클래스가 인스턴스화될 때(생성자 사용), 런타임은 힙(Heap)에 인스턴스 변수를 위한 메모리를 할당합니다. 메서드는 객체마다 중복으로 생성되지 않으며, 컴파일러가 암시적으로 현재 객체에 대한 참조(this 또는 self)를 메서드에 전달합니다. C++이나 Java에서 다형성(동적 디스패치)은 가상 메서드 테이블(vtables)을 통해 구현됩니다.
❓ 자주 묻는 질문
What is the difference between a class and an object?
A class is the blueprint or definition (the idea). An object is the actual instance created from that blueprint (the physical thing in memory). For example, 'House' is a class, but the specific house you live in is the object.
What is a constructor in a class?
A constructor is a special method inside a class that is automatically called when a new object is created. It is used to set the initial values (state) of the object.
Do all programming languages use classes?
No. While languages like Java, C++, and Python rely heavily on classes (Object-Oriented Programming), other languages use different paradigms. For example, C uses procedural programming, Haskell uses functional programming, and JavaScript originally used prototype-based inheritance.