Object-Oriented Programming, OOP : 객체지향 프로그래밍
구조적 프로그래밍에 비해 관리가 쉬워짐
Instance : 메모리 상에 존재
Object : 메모리 상 + 비 메모리 상에 존재
function = method : 기능
class = method + variable
Code
public class Mouse {
// 클래스 상수
final static int VALUE =100;
// 클래스 변수-모든 인스턴스가 공유하는 변수
// static - 정적 메모리 (프로그램 실행 시 생성)
static int count = 0;
// 인스턴스 변수 (멤버 변수)
// 인스턴스화가 되어야 사용가능 (메모리에 생성되어야 사용가능)
boolean wireless;
boolean wheel;
boolean leftBtn;
boolean rightBtn;
int sensor; // 0. 광 / 1. 레이저 / 2. 트랙패드 / 3. 트랙볼
int shape; // 0. 일반 / 1. 인체공학 / 2. 게이밍 / 3. 패드
boolean connect;
int posX;
int posY;
int wheelDelta;
void deviceConnectToggle() {
connect = !connect;
}
void changeMousePos(int x, int y) {
posX = x;
posY = y;
}
void rollingMouseWheel(int delta) {
wheelDelta = delta;
}
void showStatus () {
System.out.println("장치 연결 상태 : " + (connect ? "연결됨" : "연결해제"));
if(connect) {
System.out.println("마우스 위치 : (" + posX + "," + posY + ")");
System.out.println("휠 변화량 : " + wheelDelta);
}
}
void printInfo() {
System.out.println("마우스 연결 방식 : " + (wireless ? "무선" : "유선" ));
System.out.println("마우스 휠 유무 : " + (wheel ? "유" : "무" ));
System.out.println("마우스 버튼 (왼쪽) : " + (leftBtn ? "유" : "무" ));
System.out.println("마우스 버튼 (오른쪽) : " + (rightBtn ? "유" : "무" ));
if(sensor == 0) System.out.println("감응방식 : 광");
else if(sensor == 1) System.out.println("감응방식 : 레이저");
else if(sensor == 2) System.out.println("감응방식 : 트랙패드");
else if(sensor == 3) System.out.println("감응방식 : 트랙볼");
if(shape == 0) System.out.println("형태 : 일반");
else if(shape == 1) System.out.println("형태 : 인체공학");
else if(shape == 2) System.out.println("형태 : 게이밍");
else if(shape == 3) System.out.println("형태 : 패드");
}
void mouseClick(int btn) {
// 인스턴스는 static 표시 안함.
// class 변수로 사용할 경우 static 키워드를 사용함.
if (btn == 0) {
System.out.println("왼쪽 클릭");
}
if (btn == 1) {
System.out.println("오른쪽 클릭");
}
if (btn == 2) {
System.out.println("휠버튼 클릭");
}
}
}
import java.sql.Date;
public class MovieBasicInfo {
final String[] status_list = {"개봉예정" , "상영중" , "상영종료" , "재개봉" , "VOD"};
String title_kor;// 한글제목
String title_eng; // 영어제목
int status; // 0. 개봉예정 / 1. 상영중 / 2. 상영종료 / 3. 재개봉 / 4. VOD
double audience_score; // 관람객평점
double netizen_score; // 네티즌 평점
double critic_score; // 평론가 평점
String genre; // 장르
String country; // 국가
int running_time; // 상영시간
Date opening_dt; // 개봉일
String director; // 감독
String actor_main; // 주연배우
String actor_sub; // 조연배우
int viewing_age; // 관람등급 0, 12, 15, 19
int acc_audience; // 누적관객수
String poster_img; // 포스터 이미지 파일
void showInfo(){
System.out.println(title_kor + "(" + title_eng + ")" + "[" + status_list[status] + "]");
System.out.printf("관람객 평점 : %.2f / 네티즌 평점 : %.2f / 기자, 평론가 평점 : %.2f \n", audience_score, netizen_score, critic_score);
System.out.println("장르 : " + genre + " / 국가 : " + country + " / 상영시간 : " + running_time + "분");
System.out.println("개봉일 : " + opening_dt + " / 감독 : " + director + " / 주연 : " + actor_main + " / 조연 : " + actor_sub);
String str_viewing_age = "";
if (viewing_age == 0) {str_viewing_age = "전체 관람가";}
else {str_viewing_age = viewing_age + "세 이상 관람가";}
System.out.println("관람등급 : " + str_viewing_age + " / 누적관객 수 : " + acc_audience + "명");
System.out.println("포스터 : " + poster_img);
}
void changMovieStatus(int status) {
if (status < 0 || status >= status_list.length) {
System.out.println("상태 변경 실패 / 0-4 중 입력) ");
return; // 메서드 종료
}
System.out.println("상태 변경 : " + status_list[this.status] + " -> " + status_list[status]);
this.status = status; // this가 붙으면 멤버변수, 그냥 쓰면 int
}
void changAudienceScore (double score) {
System.out.println("관람 평점 변경 : " +audience_score+ " -> " + score);
audience_score = score;
}
void changNetizenScore (double score) {
System.out.println("네티즌 평점 변경 : " +netizen_score+ " -> " + score);
netizen_score = score;
}
void changCriticScore (double score) {
System.out.println("평론가 평점 변경 : " +critic_score+ " -> " + score);
critic_score = score;
}
void addAudienceCount (int count) {
System.out.println("누적 관객 수 : " + acc_audience + " -> " + (acc_audience+count));
acc_audience += count;
}
}
import java.sql.Date;
public class MovieTest {
public static void main(String[] args) {
MovieBasicInfo movie = new MovieBasicInfo();
movie.title_kor = "블랙 아담";
movie.title_eng = "Black Adam";
movie.status = 1;
movie.audience_score = 7.60;
movie.netizen_score = 7.70;
movie.critic_score = 5.67;
movie.genre = "액션, 모험, SF";
movie.country = "미국";
movie.running_time = 125;
// 연도는 현재연도에서 1990 뺀다
// 월은 현재 월에서 1 뺀다
movie.opening_dt = new Date(122, 9, 19);
// 줄이 그어진 표시는 권장 하지 않음의 의미
movie.director = "자움 콜렛 세라";
movie.actor_main = "드웨인 존슨";
movie.actor_sub = "노아센티네오, 피어스 프로스넌";
movie.viewing_age = 12;
movie.acc_audience = 531013;
movie.poster_img = "poster.jpg";
movie.showInfo();
movie.changMovieStatus(0);
movie.changMovieStatus(1);
movie.changMovieStatus(2);
movie.changMovieStatus(3);
movie.changMovieStatus(4);
movie.changMovieStatus(5);
movie.showInfo();
movie.changAudienceScore (23.4);
movie.changCriticScore(1.23);
movie.changNetizenScore(6.35);
movie.addAudienceCount(300);
movie.showInfo();
}
}