자바 프로그래밍을 OOP(객체 지향 프로그래밍)이라고도 한다. 현실 세계에서도 특정 제품을 만들 때 여러 가지 부품들을 각각 만들어 하나로 조립해서 제품을 생산하듯이 프로그래밍적으로도 여러 가지 객체(Object)를 각각 만들어서 하나로 조립하는 프로그래밍 기법을 객체 지향 프로그래밍이라고 합니다. 자바에서 객체는 클래스를 통해 생성하실 수 있다.
객체 지향 프로그래밍 (Object Oriented Programming)
객체
인스턴스
필드
메소드
생성자
class 클래스명{ }
public class BlackBox {
// 인스턴스 변수들
String name; // 모델명
String resolution; // 해상도
int price; // 가걱
String color; //색
int serialNumber; // 시리얼 번호
static int counter = 0; // 시리얼 번호를 생성해주는 역활(초기값이 0에서 ++을 통해 증가)
// static을 붙이면 클래스 변수라고 한다
// 만들어지는 모든 객체에 자동이로 적용
// 클래스 변수는 클래스이름.변수로 접근
// 자동신고 기능
static boolean conAutoReport = false;
// 생성자는 인스턴스 변수 다음에 적는다
/*BlackBox() {
System.out.println("기본 생성자 호출");
this.serialNumber = ++counter;
System.out.println("새로운 시리얼 넘버를 발급받았습니다 " + this.serialNumber);
}
BlackBox(String name, String resolution, int price, String color) {
this(); // 기본 생성자를 가르킨다, 기본생성자를 수행하고 아래 코드 수행
System.out.println("사용자 정의 생성자 호출");
this.name = name;
this.resolution = resolution;
this.price = price;
this.color = color;
}*/
// 간단한 메소드
void autoReport() {
if (conAutoReport) {
System.out.println("충돌이 감지되어 자동으로 신고");
} else {
System.out.println("신고 기능이 지원되지 않습니다");
}
}
}
BlackBox라는 클래스를 만들었다
내부에 필드, 생성자, 메소드를 정의하면된다
추가로 생성자와 메소드는 오버 로딩이 가능하다
생성자를 인스턴스가 있는경우와 없는 경우를 만들었다
public class Main {
public static void main(String[] args) {
//생성자가 없는 경우
BlackBox b1 = new BlackBox();
// 인스턴스 변수에 값 저장
b1.name = "까망";
b1.resolution = "HD";
b1.price = 200000;
b1.color = "블랙";
//생성자가 있는 경우
BlackBox b2 = new BlackBox("까망", "HD", 200000, "블랙");
}
}
new 연산자로 객체생성
BlackBox b1 = new BlackBox();
객체 맴버에 접근
b1.name = "까망";
BlackBox클래스 안에 아래 메소드를 추가해보자
// 간단한 메소드
void autoReport() {
if (conAutoReport) {
System.out.println("충돌이 감지되어 자동으로 신고");
} else {
System.out.println("신고 기능이 지원되지 않습니다");
}
}
void insertMemoryCard(int capacity) {
System.out.println(capacity + "GB 메모리 카드 삽입");
}
int getVideoFileCount(int type) {
if (type == 1) {
return 9;
} else if (type == 2) {
return 1;
}
return 10;
}
public class Main {
public static void main(String[] args) {
// 메소드
BlackBox b1 = new BlackBox();
// 클래스내의 메소드 호출
b1.autoReport();
BlackBox.conAutoReport = true;
b1.autoReport();
b1.insertMemoryCard(256);
// 일반 영상 : 1
// 이벤트 영상 : 2
int fileCount = b1.getVideoFileCount(1);
System.out.println("일반 영상 파일 수 " + fileCount);
fileCount = b1.getVideoFileCount(2);
System.out.println("이벤트 영상 파일 수 " + fileCount);
}
}
객체를 생성하여 클래스의 메소드를 불러올 수 있다
// 날짜, 속도, 분을 나타낸다
void record(boolean showDatetime, boolean showspeed, int min) {
System.out.println("녹화 시작");
if (showDatetime) {
System.out.println("영상에 날짜 정보 표시");
}
if (showspeed) {
System.out.println("영상에 속도가 표시");
}
System.out.println("영상 기록 단위 " + min + "분");
}
// record메소드가 호출될 때 기본적으로 정의된 값을 준다
// 메소드 오버로딩
void record() {
record(true, true, 5);
}
record() 메소드를 살향 했을 때 record(boolean showDatetime, boolean showspeed, int min) 메소드를 불러온다
오버로딩이란?
클래스에 의해서 만들어진 객체를 인스턴스라고도 한다. BlackBox b1 = new BlackBox(); 이렇게 만들어진 b1은 객체이다 b1은 BlackBox의 인스턴스이다
인스턴스라는 말은 특정 객체(여기서는 b1)가 어떤 클래스(여기서는 BlackBox)의 객체인지를 관계 위주로 설명할 때 사용
국비학원에서 배운것과 따로 공부한 것을 정리한 것입니다
참고
나도코딩 유튜브