[참고] Oracle > Java Documentation > What is a Class?
운영체제(OS) : Windows 10 64bit
JDK8 기준 내용임.
Oracle 원문을 네이버 파파고 번역기를 통해 해석한 거라 내용이 이상할 수 있음.
클래스(Class)란
-
클래스(Class)는 객체 지향 프로그래밍(OOP, Object-Oriented Programming)를 정의하는 개념 중 하나로, 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀(Blueprint, 청사진)이다.
-
객체를 정의하기 위한 상태(멤버 변수=필드)와 메서드(함수)로 구성된다.
-
템플릿을 사용하면 객체를 클래스로 정의할 때 멤버의 자료형을 미리 정하지 않고 객체를 사용할 때 결정할 수 있다. 이를 통해 클래스나 멤버의 중복 정의를 하지 않아도 되므로 효율적으로 코딩이 가능하다.
-
객체는 클래스로 규정된 인스턴스로서, 변수 대신 실제값을 가진다.
클래스에 대한 중요한 몇 가지 개념
-
클래스는 전부 혹은 일부를 그 클래스 특성으로부터 상속받는 서브클래스를 가질 수 있으며, 클래스는 각 서브클래스에 대해 수퍼클래스가 된다.
-
서브클래스는 자신만의 메소드와 변수를 정의할 수도 있다. 이러한 클래스와 그 서브클래스 간의 구조를 "클래스 계층(hierarchy)"이라 한다.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
|
cs |
-
필드 cadence, speed, gear는 객체의 상태를 나타내며, 메소드(changeCadence, changeGear, speedUp 등)는 외부의 상호 작용을 정의한다.
-
Bicycle class에 main method이 포함되어 있지 않은 것을 발견했을 수 있다. 이는 완전한 애플리케이션이 아니기 때문에 애플리케이션에 사용될 수 있는 Bicycle를 위한 틀(Blueprint)일 뿐이다.
-
새 Bicycle 객체를 만들고 사용하는 책임은 응용 프로그램의 다른 클래스에 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class BicycleDemo {
public static void main(String[] args) {
// 서로 다른 두 가지 Bicycle 객체 생성.
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// 해당 객체의 메소드 호출.
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
// cadence:50 speed:10 gear:2 // cadence:40 speed:20 gear:3 |
cs |
-
여기 BicycleDemo class가 있습니다. 두 개의 Bicycle 객체를 만들고 메소드를 실행한다.
-
The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
-
이 테스트의 출력은 두 자전거의 cadence, speed, gear를 출력한다.
'IT 개발 > JAVA' 카테고리의 다른 글
변수(Variables) - 기본 자료형(Primitive Data Type) (0) | 2021.01.12 |
---|---|
변수(Variables) (0) | 2021.01.11 |
객체 지향 프로그래밍 개념 - 객체(Object) (0) | 2021.01.10 |