본문 바로가기

인강 보습 (인프런,유데미,패스트캠퍼스)/나도코딩의 자바 기본편

클래스 (1) - 클래스, 인스턴스 변수, 클래스 변수, 메소드, 메소드 오버로딩, 클래스 메소드

1. 클래스 

객체 지향 프로그래밍 (OOP)란 유지보수가 용이. 높은 재사용성. 

 

예)

 

camera._01_Class

// 차량용 블랙박스
// 모델명, 해상도, 가격, 색상

// 우리가 만든 첫 번째 제품
String modelName = "까망이";
String resolution = "FHD";
int price = 200000;
String color = "블랙";

// 새로운 제품을 개발
String modelName2 = "하양이";
String resolution2 = "UHD";
int price2 = 300000;
String color2 = "화이트";

// 또다른 제품을 개발?
BlackBox bbox = new BlackBox();
// BlackBox 클래스로부터 bbox 객체 생성
// bbox 객체는 BlackBox 클래스의 인스턴스

BlackBox bbox2 = new BlackBox();

camera.BlackBox

public class BlackBox {
    String modelName; // 모델명
    String resolution; // 해상도
    int price; // 가격
    String color; // 색상
    int serialNumber; // 시리얼 번호

 

 

2. 인스턴스 변수  

 

camera._02_InstanceVariables

public class _02_InstanceVariables {
    public static void main(String[] args) {
        // 처음 만든 블랙박스
        BlackBox b1 = new BlackBox();
        b1.modelName = "까망이";
        b1.resolution = "FHD";
        b1.price = 200000;
        b1.color = "블랙";

        System.out.println(b1.modelName);
        System.out.println(b1.resolution);
        System.out.println(b1.price);
        System.out.println(b1.color);

        System.out.println("-------------------");

        // 새로운 블랙박스 제품
        BlackBox b2 = new BlackBox();
        b2.modelName = "하양이";
        b2.resolution = "UHD";
        b2.price = 300000;
        b2.color = "화이트";

        System.out.println(b2.modelName);
        System.out.println(b2.resolution);
        System.out.println(b2.price);
        System.out.println(b2.color);

camera.BlackBox

public class BlackBox {
    String modelName; // 모델명
    String resolution; // 해상도
    int price; // 가격
    String color; // 색상
    int serialNumber; // 시리얼 번호

 

3. 클래스 변수  

camera._03_ClassVariables

public class _03_ClassVariables {
    public static void main(String[] args) {
        BlackBox b1 = new BlackBox();
        b1.modelName = "까망이";
        System.out.println(b1.modelName);

        BlackBox b2 = new BlackBox();
        b2.modelName = "하양이";
        System.out.println(b2.modelName);

        // 특정 범위를 초과하는 충돌 감지 시 자동 신고 기능 개발 여부
        System.out.println(" - 개발 전 -");
        System.out.println(b1.modelName + " 자동 신고 기능 : " + b1.canAutoReport);
        System.out.println(b2.modelName + " 자동 신고 기능 : " + b2.canAutoReport);
        System.out.println("모든 블랙박스 제품 자동 신고 기능 : " + BlackBox.canAutoReport);

        // 기능 개발
        BlackBox.canAutoReport = true;

        System.out.println(" - 개발 후 -");
        System.out.println(b1.modelName + " 자동 신고 기능 : " + b1.canAutoReport);
        System.out.println(b2.modelName + " 자동 신고 기능 : " + b2.canAutoReport);
        System.out.println("모든 블랙박스 제품 자동 신고 기능 : " + BlackBox.canAutoReport); // 권장
    }
}

camera.BlackBox

public class BlackBox {
    String modelName; // 모델명
    String resolution; // 해상도
    int price; // 가격
    String color; // 색상
    int serialNumber; // 시리얼 번호

    static int counter = 0; // 시리얼 번호를 생성해주는 역할 (처음엔 0이었다가 ++ 연산을 통해서 값을 증가)
    static boolean canAutoReport = false; // 자동 신고 기능
void autoReport() {
    if (canAutoReport) {
        System.out.println("충돌이 감지되어 자동으로 신고합니다.");
    }
    else {
        System.out.println("자동 신고 기능이 지원되지 않습니다.");
    }
}

 

4. 메소드

camera._04_Method

public class _04_Method {
    public static void main(String[] args) {
        BlackBox b1 = new BlackBox();
        b1.modelName = "까망이";

        b1.autoReport(); // 지원 안됨
        BlackBox.canAutoReport = true;
        b1.autoReport(); // 지원 됨

        b1.insertMemoryCard(256);

        // 일반 영상 : 1 (type)
        // 이벤트 영상 (충돌 감지) : 2
        int fileCount = b1.getVideoFileCount(1); // 일반 영상
        System.out.println("일반 영상 파일 수 : " + fileCount + "개");

        fileCount = b1.getVideoFileCount(2); // 이벤트 영상
        System.out.println("이벤트 영상 파일 수 : " + fileCount + "개");
    }
}

camera.BlackBox

void insertMemoryCard(int capacity) {
    System.out.println("메모리 카드가 삽입되었습니다.");
    System.out.println("용량은 " + capacity + "GB 입니다.");
}

int getVideoFileCount(int type) {
    if (type == 1) { // 일반 영상
        return 9;
    }
    else if (type == 2) { // 이벤트 영상
        return 1;
    }
    return 10;
}

 

 

5. 메소드  오버로딩 

 

camera._05_MethodOverloading

public class _05_MethodOverloading {
    public static void main(String[] args) {
        BlackBox b1 = new BlackBox();
        b1.modelName = "까망이";

        b1.record(false, false, 10);
        System.out.println("------------------------");
        b1.record(true, false, 3);
        System.out.println("------------------------");
        b1.record();

        // String
        String s = "BlackBox";
        System.out.println(s.indexOf("a"));
    }

camera.BlackBox

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 + "분 단위로 기록됩니다.");
}

void record() {
    record(true, true, 5);
}

 

6. 클래스 메소드

camera._06_ClassMethod

public class _06_ClassMethod {
    public static void main(String[] args) {
//        BlackBox b1 = new BlackBox();
//        b1.callServiceCenter();
//
//        BlackBox b2 = new BlackBox();
//        b2.callServiceCenter();

        BlackBox.callServiceCenter();

        String s = String.valueOf(3);
    }
}

camera.BlackBox

static void callServiceCenter() {
    System.out.println("서비스 센터(1588-oooo) 로 연결합니다.");
}

         *static을 붙이면 모든 개체에 공통적으로 적용
         *클래스 메소드를 만들면 객체 없이도 바로 접근 가능  (eg. BlackBox.callServiceCenter(); )