Live-Study

[5주차] 클래슀

youn12 2021. 1. 4. 17:43
✏️ Info.

 - Java Class ν•™μŠ΅

πŸ“‹ List.

1. 클래슀 μ •μ˜ν•˜λŠ” 방법
2. 객체 λ§Œλ“œλŠ” 방법 (new ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°)
3. λ©”μ„œλ“œ μ •μ˜ν•˜λŠ” 방법
4. μƒμ„±μž μ •μ˜ν•˜λŠ” 방법
5. this ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°


βœ”οΈ Content.

 

1. 클래슀 μ •μ˜ν•˜λŠ” 방법

 

  • Class
    • 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°(OOP, Object-Oriented Programming) μ—μ„œ 핡심인 Objectλ₯Ό λ§Œλ“€κΈ°μœ„ν•œ 섀계도와 같은 κ°œλ…
    • Class λŠ” 객체의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” Field 와 객체의 행동을 λ‚˜νƒ€λ‚΄λŠ” Method둜 κ΅¬μ„±λœλ‹€.

Class

  • Class μ„ μ–Έ 방법
public class StudyClass {

// Field
// Method
// Constructor

}
  • Class, Field, Method μ—μ„œ μ ‘κ·Όμ œμ–΄λ₯Ό μ‚¬μš©ν•¨μ„ 볼수 μžˆλŠ”λ° [μ ‘κ·Όμ œμ–΄] λž€ OOPμ—μ„œμ˜ μΊ‘μŠν™”λ‘œ λ‚΄λΆ€ 데이터 λ˜λŠ” λ©”μ†Œλ“œλ“€μ˜ 접근을 μ œμ–΄ν•˜λŠ” 것이닀.
  • 각각의 μ ‘κ·Ό μ œμ–΄μž μ’…λ₯˜μ™€ μ‚¬μš©λ²”μœ„λŠ” μ•„λž˜μ™€ κ°™λ‹€.
  • μ ‘κ·Ό ν—ˆμš© ν‘œ
μ ‘κ·Ό μ œμ–΄μž 같은 클래슀 같은 νŒ¨ν‚€μ§€ μžμ† 클래슀 전체
public O O O O
protected O O O X
default O O X X
private O X X X

 

  • μ ‘κ·Ό λŒ€μƒ ν‘œ
μ ‘κ·Ό μ œμ–΄μž 클래슀 ν•„λ“œ μƒμ„±μž λ©”μ†Œλ“œ
public O O O O
protected X O O O
default O O O O
private X O O O

 

2. 객체 λ§Œλ“œλŠ” 방법 (new ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°)

 

public static void main(String[] args) {
        StudyClass studyClass; // 객체 생성
        studyClass = new StudyClass(); // 객체의 μΈμŠ€ν„΄μŠ€ν™”
    }
  1. StudyClass 클래슀의 studyClassλΌλŠ” 객체λ₯Ό μƒμ„±ν•œλ‹€.
  2. new μ—°μ‚°μžλ₯Ό 톡해 studyClass 객체λ₯Ό λ©”λͺ¨λ¦¬μ— ν• λ‹Ήν•˜λ©΄μ„œ studyClass κ°μ²΄λŠ” StudyClass 클래슀의 μΈμŠ€ν„΄μŠ€κ°€ λ˜μ—ˆλ‹€.

 

3. λ©”μ„œλ“œ μ •μ˜ν•˜λŠ” 방법

 

  • [μ ‘κ·Όμ œμ–΄] [λ°˜ν™˜ν˜•μ‹] [Method 이름](λ§€κ°œλ³€μˆ˜){ μ½”λ“œκ΅¬ν˜„λΆ€(return [λ°˜ν™˜ν˜•μ‹ κ°’]) }
public class StudyClass {

// Field
// Constructor
    

    /**
     * Method
     */
    public void method() {

        int level = 0; // local variable

    }
    
    String method2(String name){
        return name + " μž…λ‹ˆλ‹€.";
    }

}

 

  • λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©(overloading)
    • λ©”μ†Œλ“œ μ‹œκ·Έλ‹ˆμ³ ( Method Signature)
      • λ©”μ†Œλ“œ 이름 + λ§€κ°œλ³€μˆ˜ 리슀트의 μ‘°ν•©
    • λ©”μ†Œλ“œμ˜ 이름은 κ°™μ§€λ§Œ λ§€κ°œλ³€μˆ˜ 리슀트의 쑰합이 λ‹€λ₯Έ 경우
    • 즉, λ©”μ†Œλ“œμ˜ μ‹œκ·Έλ‹ˆμ³κ°€ λ‹€λ₯Έ κ²½μš°μ΄λ‹€.
public class StudyClass{

    public void StudyMethod() {
        System.out.println("StudyClass μž…λ‹ˆλ‹€.");
    }

    public void StudyMethod(String name){
        System.out.println("StudyClass OverLoading : " + name);
    }

    public static void main(String[] args) {
        StudyClass studyClass = new StudyClass();
        studyClass.StudyMethod();
        studyClass.StudyMethod("Test");
    }
}

Method Overloading 좜λ ₯

  • λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©(overriding)
    • 상속관계에 μžˆλŠ” λΆ€λͺ¨ 클래슀둜 λΆ€ν„° μ •μ˜λœ λ©”μ†Œλ“œλ₯Ό μž¬μ •μ˜ ν•˜λŠ” 것
class Study{
    public void StudyMethod(){
        System.out.println("Study μž…λ‹ˆλ‹€.");
    }
}

public class StudyClass extends Study{
    
    @Override
    public void StudyMethod() {
        System.out.println("StudyClass μž…λ‹ˆλ‹€.");
    }

    public static void main(String[] args) {
        Study study = new Study();
        StudyClass studyClass = new StudyClass();
        study.StudyMethod();
        studyClass.StudyMethod();
    }
}

 

Method Overriding 좜λ ₯


 

4. μƒμ„±μž μ •μ˜ν•˜λŠ” 방법

 

  • ν΄λž˜μŠ€μ— μ‘΄μž¬ν•˜λ©° ν•˜λ‚˜μ΄μƒ 가지고 μžˆλ‹€.
  • μƒμ„±μžλ₯Ό μ •μ˜ ν•˜μ§€μ•Šμ„ 경우 μžλ°” μ»΄νŒŒμΌλŸ¬κ°€ μ•„λ¬΄λŸ° λ§€κ°œλ³€μˆ˜κ°€ μ—†μœΌλ©° κ΅¬ν˜„λΆ€ λ‚΄μš©μ΄ μ—†λŠ” κΈ°λ³Έ μƒμ„±μžλ₯Ό μ œκ³΅ν•œλ‹€.
  • λ°˜λ“œμ‹œ 클래슀의 이름과 κ°™μ•„μ•Ό ν•œλ‹€.
  • λ°˜ν™˜κ°’μ΄ μ—†μ§€λ§Œ, λ°˜ν™˜ νƒ€μž…μ„ voidν˜•μœΌλ‘œ μ„ μ–Έν•˜μ§€ μ•ŠλŠ”λ‹€.
  • overloding 이 κ°€λŠ₯ν•˜λ‹€.
public class StudyClass extends Study{

    
    /**
     * Method(Constructor)
     */
    public StudyClass() {
    }

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

    public StudyClass(String name, int age, int grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

}


 

5. this ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°

 

  • μΈμŠ€ν„΄μŠ€κ°€ λ°”λ‘œ μžκΈ°μžμ‹ μ„ μ°Έμ‘°ν•˜λŠ”λ° μ‚¬μš©ν•˜λŠ” λ³€μˆ˜
  • μΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œλ₯Ό 가리킀고 μžˆλ‹€.
  • μƒμ„±μž λ‚΄μ—μ„œ λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.
    public StudyClass(String name) {
        this.name = name;
    }

    public StudyClass(String name, int age, int grade) {
        this(name); //StudyClass(String name) μƒμ„±μž 호좜
        this.age = age;
        this.grade = grade;
    }

❗️ Refer.

클래슀 μ •μ˜ν•˜λŠ” 방법
- http://www.tcpschool.com/java/java_class_intro
- http://blog.naver.com/PostView.nhn?blogId=mals93&logNo=220631298165&parentCategoryNo=&categoryNo=15&viewDate=&isShowPopularPosts=false&from=postList

객체 λ§Œλ“œλŠ” 방법 (new ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°)
λ©”μ„œλ“œ μ •μ˜ν•˜λŠ” 방법
- http://www.tcpschool.com/java/java_usingMethod_overloading
- http://www.tcpschool.com/java/java_inheritance_overriding

μƒμ„±μž μ •μ˜ν•˜λŠ” 방법
- http://www.tcpschool.com/java/java_methodConstructor_constructor

this ν‚€μ›Œλ“œ μ΄ν•΄ν•˜κΈ°
- http://www.tcpschool.com/java/java_methodConstructor_this