Java

[Java] Class

98kg 2024. 2. 19. 16:30
java 는 객체지향 언어로서 유지보수에 용이하고 높은 재사용성을 보여준다

 

클래스란 하나의 설계도로 보면된다.

클래스의 메서드는 기능, 필드는 구성용품으로 예를 들 수 있다

 

우선 블랙박스라는 클래스를 만들어주어 작성해 보자

각 설명은 주석을 해놓았다.

package Chapter_07;

public class BlackBox {

    //기본 인스턴스변수 -> 객체 필요
    String modelName;
    String resolution;
    int price;
    String color;

    int serialNumber; //시리얼 넘버

    static int counter = 0; //시리얼 번호를 생성해주는 역할 (처음엔 0이었다가 ++ 연산을 통해서 값을 증가)
    // static 붙이면 클래스 변수이다. -> 객체 없이 메소드 이름으로 바로 접근 가능
    static boolean canAutoReport = false; // 자동 신고 기능


    BlackBox() {
//        System.out.println("기본 생성자 호출");
//        this.serialNumber = ++counter;
//        System.out.println("새로운 시리얼 넘버를 발급 받았습니다 : "+ this.serialNumber);
    }//생성자

    BlackBox(String modelName, String resolution, String color, int price){
//        this();//기본 생성자의 바로 접근 후 아래 생성자 호출
//
//        System.out.println("사용자 정의 생성자 호출");
//        this.modelName = modelName;
//        this.resolution =resolution;
//        this.color=color;
//        this.price=price;

    }

    void autoReport() {
        if (canAutoReport) {
            System.out.println("충돌이 감지되어 자동 신고 합니다.");
        } else {
            System.out.println("신고 기능이 지원되지 않습니다.");
        }
    }
    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;
    }
    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); // 기본 정보
    }
    static void callServiceCenter() {
        System.out.println("서비스 센터(1588-0000) 로 연결합니다.");
    }
    void appendModelName(String modelName) {
        this.modelName += modelName; //인스턴스 변수인 modelName 에 직접 접근 : this.변수
    }

    // getter : 값을 가져온다,  setter : 값을 설정한다
    String getModelName() {
        return modelName;

    }
    void setModelName(String modelName) {
        this.modelName = modelName;
    }

    String getResolution() {
        if(resolution == null || resolution.isEmpty()) {
            return "판매자 문의 요함.";
        }
        return resolution;
    }

    void setResolution() {
        this.resolution = resolution;
    }
    int getPrice(){
        return price;
    }
    void setPrice(int price){
        if (price < 100000){
            this.price = 100000;
        }
        else{
        this.price = price;
        }
    }
    String getColor() {
        return color;
    }
    void setColor(String color) {
        this.color = color;
    }


}

이렇게 생성 후 다른 클래스에서 인스턴스를 생성하여 블랙박스를 호출 해보겠다.

package Chapter_07;

public class _01_Class {
    public static void main(String[] args) {
        

       
        BlackBox BBox = new BlackBox();
        // BlackBox 클래스로부터 BBox 객체 생성
        //BBox 객체는  Blackbox 클래스이 인스턴스
        //BBOX. 으로 BlackBox 에 있는 메서드를 호출 할 수 있다.
        // 여기서 필드를 호출하기 위해선 get으로 호출하고 필드 값을 재정의 하기위해선 set 으로 재정의한다.




    }

}
인스턴스 생성 후 필드호출
package Chapter_07;

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

        // 처음 만든 블랙박스
        System.out.println(b1.modelName);
        System.out.println(b1.resolution);
        System.out.println(b1.color);
        System.out.println(b1.price);

        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.color);
        System.out.println(b2.price);

    }
}
package Chapter_07;

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); // 권장

    }
}
인스턴스 생성 후 메서드 호출
package Chapter_07;

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
        // 이벤트 영상 ( 충돌 감지 ) : 2

        int fileCount = b1.getVideoFileCount(1); // 일반영상
        System.out.println(" 일반 영상 파일 수  : "+ fileCount + "개");

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


    }
}