🍡 java

JAVA | λ‚˜λ„μ½”λ”© μžλ°” κ°•μ˜ (클래슀, λ©”μ†Œλ“œ) _ Quiz#7

c0zi 2023. 2. 10. 00:01
μΈν”„λŸ° 결제

μ €λ²ˆ ν”Όλ“œκΉŒμ§€ ν•΄μ„œ 무료 κ°•μ˜κ°€ λͺ¨λ‘ 끝이 났닀 !

λ„ˆλ¬΄ μœ μ΅ν•˜κ³  또 μ„€λͺ…도 μž˜ν•΄μ£Όμ…”μ„œ 이해가 쉬웠닀. κ·Έλž˜μ„œ λ’·λΆ€λΆ„κΉŒμ§€ λ§ˆμ € λ“£κ³  μ‹Άμ–΄μ„œ μΈν”„λŸ°μ—μ„œ ν’€κ°•μ˜λ₯Ό κ²°μ œν–ˆλ‹€.

λ‚˜λŠ” νšŒμ›κ°€μž… 할인 λ°›μ•„μ„œ 6λ§Œμ› ? 정도에 κ²°μ œν•œ 것 κ°™λ‹€.

 

λ©°μΉ λ™μ•ˆ λΈ”λ‘œκ·Έλ₯Ό λͺ» 올린 μ΄μœ λŠ” λ‚΄κ°€ ν† μ΅μ‹œν—˜(2/5)을 보느라 λ°”μ˜κΈ°λ„ ν–ˆκ³ , 또 이번 κ°•μ˜ νšŒμ°¨κ°€ μœ λ… κΈΈμ—ˆλ‹€.

보톡 ν•œ 챕터에 1μ‹œκ°„ μ •λ„λ‘œ 끊기길래 ν•˜λ£¨μ— ν•œκ°œμ”© λ³΄λŠ” 걸둜 λͺ©ν‘œλ₯Ό μ‚Όμ•˜λŠ”λ° μ΄λ²ˆμ—λŠ” 3μ‹œκ°„μ΄ λ„˜μ—ˆλ‹€...

그리고 또 λ‚΄κ°€ 일본 여행을 κ°€κ²Œ λ˜μ–΄μ„œ μ€€λΉ„κΉŒμ§€ ν•˜λ‹€λ³΄λ‹ˆ λ©°μΉ  λΈ”λ‘œκ·Έλ₯Ό μ‰¬μ—ˆλ‹€ γ…Žγ…Ž

 

클래슀

이번 μ±•ν„°λŠ” 정말 μ–΄λ €μ› λŠ”λ°, 처음 μ ‘ν•˜λŠ” κ°œλ…μ΄ 많이 λ‚˜μ˜¨ 것 κ°™λ‹€.

κ·Έλž˜λ„ μ²˜μŒμ—λŠ” μ΄ν•΄ν• λ§Œ ν–ˆμ§€λ§Œ λ’€λ‘œ 갈 수둝 λ©˜λΆ•μ˜ 연속,,,

 

μžλ°”μ—μ„œ 클래슀(class)λž€ 객체λ₯Ό μ •μ˜ν•˜λŠ” ν‹€ λ˜λŠ” 섀계도와 κ°™μ€ 의미둜 μ‚¬μš©λ©λ‹ˆλ‹€.

μžλ°”μ—μ„œλŠ” μ΄λŸ¬ν•œ 섀계도인 클래슀λ₯Ό κ°€μ§€κ³ , μ—¬λŸ¬ 객체λ₯Ό μƒμ„±ν•˜μ—¬ μ‚¬μš©ν•˜κ²Œ λ©λ‹ˆλ‹€.

 

ν΄λž˜μŠ€λŠ” 객체의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” ν•„λ“œ(field)와 객체의 행동을 λ‚˜νƒ€λ‚΄λŠ” λ©”μ†Œλ“œ(method)둜 κ΅¬μ„±λ©λ‹ˆλ‹€.

즉, ν•„λ“œ(field)λž€ ν΄λž˜μŠ€μ— ν¬ν•¨λœ λ³€μˆ˜(variable)λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

λ˜ν•œ, λ©”μ†Œλ“œ(method)λž€ μ–΄λ– ν•œ νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•œ λͺ…λ Ήλ¬Έμ˜ 집합이라 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

좜처 : http://www.tcpschool.com/java/java_class_intro (TCP School)

 

package chap_07;

public class _01_Class {
    public static void main(String[] args) {
        // 객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ° (OOP : Object-Oriented Programming)
        // μœ μ§€λ³΄μˆ˜ 용이
        // 높은 μž¬μ‚¬μš©μ„±

        // μ°¨λŸ‰μš© λΈ”λž™λ°•μŠ€

        // λͺ¨λΈλͺ…, 해상도, 가격, 색상
        // 첫 번째 μž‘ν’ˆ
        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();

    }
}
package chap_07;

public class BlackBox {
    String modelName; // λͺ¨λΈλͺ…
    String resolution; //
    int price;
    String color;
    int serialNumber;// μ‹œλ¦¬μ–Ό 번호

    static int counter = 0; // μ‹œλ¦¬μ–Ό 번호λ₯Ό μƒμ„±ν•΄μ£ΌλŠ” μ—­ν•  (μ²˜μŒμ—” 0, ++ 연산을 톡해 κ°’ 증가)

    static boolean canAutoReport = false; // μžλ™μ‹ κ³  κΈ°λŠ₯

    BlackBox() {
//        System.out.println("κΈ°λ³Έ μƒμ„±μž 호좜");
//        this.serialNumber = ++counter;
//        System.out.println("μƒˆλ‘œμš΄ μ‹œλ¦¬μ–Ό λ„˜λ²„λ₯Ό λ°œκΈ‰λ°›μ•˜μŠ΅λ‹ˆλ‹€ : " + this.serialNumber );
    }

    BlackBox(String modelName, String resolution, int price, String color) {
//        this(); // κΈ°λ³Έ μƒμ„±μž 호좜
//
//        System.out.println("μ‚¬μš©μž μ •μ˜ μƒμ„±μž 호좜");
//        this.modelName = modelName;
//        this.resolution = resolution;
//        this.price = price;
//        this.color = color;
    }

    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 0;
    }

    // showDataTime : λ‚ μ§œμ •λ³΄ ν‘œμ‹œμ—¬λΆ€
    // showSpeed : 속도정보 ν‘œμ‹œμ—¬λΆ€
    // min : μ˜μƒ 기둝 λ‹¨μœ„(λΆ„)
    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() { // 일반적 methodλŠ” 객체가 λ§Œλ“€μ–΄μ Έμ•Ό μ‚¬μš©κ°€λŠ₯
        record(true, true, 5); // κΈ°λ³Έκ°’μœΌλ‘œ μ§€μ • > μˆ˜μ •μ΄ ν•„μš”ν•  λ•Œ κ°„νŽΈ
    }

    static void callServiceCenter() { // static이 λΆ™μœΌλ©΄ calss methodμ΄λ―€λ‘œ 미리 μ‚¬μš©κ°€λŠ₯
        System.out.println("μ„œλΉ„μŠ€ μ„Όν„°(1588-0000)둜 μ—°κ²°ν•©λ‹ˆλ‹€.");
        canAutoReport = false; // instance variable은 static λ‚΄μ—μ„œ μ‚¬μš© λΆˆκ°€
    }

    void appendModelName(String modelName) {
        this.modelName += modelName; // this.을 뢙이면 instance λ³€μˆ˜
    }

    // Getter & Setter
    String getModelName() { // getter
        return modelName;
    }

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

    String getResolution() {
        if ( resolution == null || resolution.isEmpty()) {
            return "νŒλ§€μžμ—κ²Œ λ¬Έμ˜ν•˜μ„Έμš”";
        }
        return resolution;
    }

    void setResolution(String resolution) {
        this.resolution = resolution;
    }

    int getPrice() {
        return price;
    }

    void setPrice(int price) {
        if (price < 100000) {
            this.price = 100000;
        }
    }

    String getcolor() {
        return color;
    }

    void setColor(String color) {
        this.color = color;
    }

}

 

이런 μ €λŸ° μ—¬λŸ¬ 챕터λ₯Ό μˆ˜ν–‰ν•˜λ‹€ λ³΄λ‹ˆ BlackBox의 μ½”λ“œκ°€ κΈΈμ–΄μ‘Œμ§€λ§Œ, λ’€μ˜ μ½”λ“œλŠ” BlackBox 클래슀λ₯Ό ν˜•μ„±ν•œ 것이고 μœ„μ˜ μ½”λ“œλŠ” BlackBox ν΄λž˜μŠ€λ‘œλΆ€ν„° bBoxλΌλŠ” μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•œ 것이닀.

 

μ ‘κ·Ό μ œμ–΄μž

 

// μ ‘κ·Ό μ œμ–΄μž
// private : ν•΄λ‹Ή 클래슀 λ‚΄μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯
// public : λͺ¨λ“  ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯
// default : (아무것도 적지 μ•Šμ•˜μ„ λ•Œ) 같은 νŒ¨ν‚€μ§€ λ‚΄μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯
// protected : 같은 νŒ¨ν‚€μ§€ λ‚΄μ—μ„œ, λ‹€λ₯Έ νŒ¨ν‚€μ§€μΈ 경우 μžμ‹ ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯

 

enum

 

package chap_07;

public class _18_Enum {
    public static void main(String[] args) {
        // μ—΄κ±°ν˜• (Enum)
        // 달λ ₯ : JAN, FEB, MAR, ...
        // 옷 μ‚¬μ΄μ¦ˆ : S, M, L, XL, ...
        // OS μ’…λ₯˜ : WIN10, WIN11, MACOS, LINUX, ...

        Resolution resolution = Resolution.HD;
        System.out.println(resolution);

        resolution = Resolution.FHD;
        System.out.println(resolution);

        System.out.print("λ™μ˜μƒ λ…Ήν™” ν’ˆμ§ˆ :");
        switch (resolution) {
            case HD :
                System.out.println("μΌλ°˜ν™”μ§ˆ");
                break;
            case FHD :
                System.out.println("κ³ ν™”μ§ˆ");
                break;
            case UHD:
                System.out.println("μ΄ˆκ³ ν™”μ§ˆ");
                break;
        }

        resolution = Resolution.valueOf("UHD");
        System.out.println(resolution);

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

        for (Resolution myRes: Resolution.values()) {
            System.out.println(myRes.name() + " : " + myRes.ordinal());

        }

        System.out.println("--------------------");
        for (Resolution myRes : Resolution.values()) {
            System.out.println(myRes.name() + " : " + myRes.getWidth());
        }

    }
}

enum Resolution {
    HD(1280), FHD(1920), UHD(3840);

    private  final int width;

    Resolution(int width) {
        this.width = width;
    }

    public int getWidth() {
        return width;
    }
}

 

enum이라고 ν•˜λŠ” μ—΄κ±°ν˜•μ€ Cμ—μ„œ λ³Έ 것 κ°™μ•˜μ§€λ§Œ, 또 λ‚―μ„€μ—ˆλ‹€.

private μ ‘κ·Όμ œμ–΄μžλ₯Ό μ‚¬μš©ν•˜μ—¬ widthλ₯Ό μ„ μ–Έν•˜κ³  μ–΄μ©Œκ³ .. 사싀 아직 이해 λͺ»ν–ˆλ‹€ γ…Žγ…Ž

ν€΄μ¦ˆ ν’€λ©΄μ„œ μ—°μŠ΅ν•΄λ΄μ•Όμ§€

 

Quiz #7

 

클래슀λ₯Ό μ΄μš©ν•΄μ„œ 햄버거, μΉ˜μ¦ˆλ²„κ±°, μƒˆμš°λ²„κ±°λ₯Ό λ§Œλ“œλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λŠ” ν€΄μ¦ˆμ˜€λ‹€.

각 λ²„κ±°μ˜ 재료λ₯Ό μ£Όκ³ , main μ˜μ—­μ„ μ œμ‹œν•΄μ£Όλ©΄μ„œ μ›ν•˜λŠ” μ‹€ν–‰κ²°κ³Όκ°€ λ‚˜μ˜€λ„λ‘ ν•˜λ©΄ λ˜μ—ˆλ‹€.

 

package chap_07;

public class _Quiz_07 {
    public static void main(String[] args) {
        HamBurger[] hamBurgers = new HamBurger[3];
        hamBurgers[0] = new HamBurger();
        hamBurgers[1] = new CheeseBurger();
        hamBurgers[2] = new ShirimpBurger();

        System.out.println("μ£Όλ¬Έν•˜μ‹  메뉴λ₯Ό λ§Œλ“­λ‹ˆλ‹€.");
        System.out.println("--------------------");
        for (HamBurger hamBurger : hamBurgers) {
            hamBurger.cook();
            System.out.println("--------------------");
        }
        System.out.println("메뉴 μ€€λΉ„κ°€ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
    }

}

class HamBurger {
    public String name ;

    public HamBurger() {
        this("햄버거");
    }

    public HamBurger(String name) {
        this.name = name;
    }

    public void cook() {
        System.out.println(this.name + "λ₯Ό λ§Œλ“­λ‹ˆλ‹€.");
        System.out.println("λΉ΅ 사이에 λ“€μ–΄κ°€λŠ” μž¬λ£ŒλŠ” ?");
        System.out.println("> 양상좔");
        System.out.println("+ νŒ¨ν‹°");
        System.out.println("+ 피클");

        // 재료 : 양상좔, νŒ¨ν‹°, 피클
    }
}

class CheeseBurger extends HamBurger {

    public CheeseBurger() {
        super("μΉ˜μ¦ˆλ²„κ±°");
    }

    public String name;
    public void cook() {
        // 재료 : 양상좔, νŒ¨ν‹°, 피클, 치즈
        super.cook();
        System.out.println("+ 치즈");
    }
}

class ShirimpBurger extends HamBurger {
    public ShirimpBurger() {
        super("μƒˆμš°λ²„κ±°");
    }

    public String name;
    public void cook() {
        // 재료 : 양상좔, νŒ¨ν‹°, 피클, μƒˆμš°
        super.cook();
        System.out.println("+ μƒˆμš°");
    }
}

 

쑰금 ν—€λ§Έλ‹€.

사싀은 쑰금 많이.

μ—¬κΈ°μ„œ public Hamburger() μ—μ„œλΆ€ν„° this("햄버거")λ₯Ό ν•˜κ³  public Hamburger(String name)κΉŒμ§€μ˜ 뢀뢄이 이해가 μ•ˆλœλ‹€

λ‚˜λ¨Έμ§€λŠ” μ•Œκ² λŠ”λ°,,,

ν•˜μ—¬νŠΌ ν΄λž˜μŠ€λŠ” μ›λž˜ μ–΄λ €μšΈ 거라고 μ˜ˆμƒμ„ ν•΄μ„œ 쑰금 더 곡뢀해봐야 ν•  것 κ°™λ‹€.

 

였늘의 μžλ°” 끗 !

 

λ‚˜λ„μ½”λ”© μžλ°” κ°•μ˜

https://www.inflearn.com/course/%EB%82%98%EB%8F%84%EC%BD%94%EB%94%A9-%EC%9E%90%EB%B0%94-%EA%B8%B0%EB%B3%B8

 

λ‚˜λ„μ½”λ”©μ˜ μžλ°” 기본편 - ν’€μ½”μŠ€ (20μ‹œκ°„) - μΈν”„λŸ° | κ°•μ˜

λ°°μ›€μ˜ 즐거움을 μ•Œκ²Œ ν•΄μ£ΌλŠ” κ°•μ˜. λ‚˜λ„μ½”λ”©μ˜ μžλ°” κΈ°λ³ΈνŽΈμ„ μ†Œκ°œν•©λ‹ˆλ‹€., - κ°•μ˜ μ†Œκ°œ | μΈν”„λŸ°...

www.inflearn.com