μ°Έμ‘°λ³€μˆ˜ this πŸ†š μƒμ„±μž this()

κΈˆμ€μ²΄λ¦¬Β·2023λ…„ 10μ›” 25일
0

Java

λͺ©λ‘ 보기
11/14

μƒμ„±μž this()

클래슀λͺ…() {
...
}
클래슀λͺ…(인수1, 인수2, ...) {
...
}
  • μƒμ„±μž: 클래슀의 이름과 λ˜‘κ°™μ€ μ΄λ¦„μ˜ λ©”μ†Œλ“œ
    - λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”© κ°€λŠ₯
  • μΈμŠ€ν„΄μŠ€λ₯Ό 생성될 λ•Œ ν˜ΈμΆœλ˜λ„λ‘ μ•½μ†λ˜μ–΄ 있음
    - λ°˜λ“œμ‹œ μ‹€ν–‰λ˜μ–΄μ•Ό ν•  초기 μž‘μ—…μ„ μƒμ„±μž μ•ˆμ— λ„£μœΌλ©΄ 됨
  • 리턴이 μ—†μŒ
  • μƒμ„±μžλ₯Ό μ •μ˜ν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ μ œκ³΅ν•˜λŠ” μƒμ„±μžκ°€ 호좜됨
  • μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ™€ μ§€μ—­λ³€μˆ˜λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©
  • 같은 클래슀의 λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©λ¨
    - λ‹€λ₯Έ μƒμ„±μžμ˜ ν˜ΈμΆœμ€ μƒμ„±μžμ˜ 첫 λ¬Έμž₯μ—μ„œλ§Œ κ°€λŠ₯

μ°Έμ‘°λ³€μˆ˜ this

 public MyDate(int new_year, int new_month) 
       {
          year = new_year;
          month = new_month;
        }
public MyDate(int year, int month) 
        {
           this.year = year;
           this.month = month;
         }
  • μΈμŠ€ν„΄μŠ€ μžμ‹ μ„ κ°€λ¦¬ν‚€λŠ” μ°Έμ‘°λ³€μˆ˜
  • 클래슀 λ©”μ†Œλ“œλ‚΄μ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯
  • this.year은 MyDate클래슀의 속성인 Pribate year 뢀뢄을 가리킴
  • static λ©”μ„œλ“œ(클래슀 λ©”μ„œλ“œ) μ—μ„œλŠ” μ‚¬μš© λΆˆκ°€λŠ₯

같은 클래슀 μ•ˆμ—μ„œλŠ” this μƒλž΅ κ°€λŠ₯

 Car(String c, String g, int d) {
      color = c;
      gearType = g;
      door = d;
  }

μ§€μ—­λ³€μˆ˜μ™€ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜κ°€ 동일할 λ•Œ

  Car(String color, String gearType, int door) {
      this.color = color;
      this.gearType = gearType;
      this.door = door;
  }

μΈμŠ€ν„΄μŠ€ λ³€μˆ˜, 클래슀 λ³€μˆ˜, μ§€μ—­λ³€μˆ˜

public class test {

	int iv; // μΈμŠ€ν„΄μŠ€ λ³€μˆ˜
	static int cv; // 클래슀 λ³€μˆ˜
	
	void method() {
		int lv; // 지역 λ³€μˆ˜
	}
}
profile
μ „ 체리 μ•ŒλŸ¬μ§€κ°€ μžˆμ–΄μš”!

0개의 λŒ“κΈ€