めけゃくけゃ ι–‹η™Ίθ€…πŸ¦Ύ
μ½”λ”© 짐 πŸ’ͺ
めけゃくけゃ ι–‹η™Ίθ€…πŸ¦Ύ
  • μΉ΄ν…Œκ³ λ¦¬ (135)
    • 개발 ν™˜κ²½ ꡬ좕 (12)
      • 개발 ν™˜κ²½ (5)
      • DB (0)
      • Node.js (4)
      • ν˜•μƒ 관리 (2)
      • Spring (1)
    • μ›Ή (11)
      • React (5)
      • 슀파λ₯΄νƒ€μ½”λ”©ν΄λŸ½__μ›Ή (6)
    • λͺ¨λ°”일 (2)
      • μ•ˆλ“œλ‘œμ΄λ“œ (2)
    • ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄ (55)
      • C (13)
      • Python (15)
      • SQL (5)
      • Java (22)
    • 사물인터넷 (11)
      • 아두이노 (11)
    • 일상 (31)
      • λ§›μ§‘ (13)
      • λž­ν‚Ήλ‹­μ»΄ (4)
      • ν›„κΈ° (11)
      • μš”λ¦¬ (3)
      • μž‘λ‹΄ (0)
    • ꡐ양 (4)
      • 심리학 (3)
      • ν…Œλ‹ˆμŠ€ (1)
    • 자격증 (9)
      • μ •λ³΄μ²˜λ¦¬κΈ°μ‚¬ (9)

λΈ”λ‘œκ·Έ 메뉴

  • πŸ’» github

인기 κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

hELLO Β· Designed By μ •μƒμš°.
めけゃくけゃ ι–‹η™Ίθ€…πŸ¦Ύ

μ½”λ”© 짐 πŸ’ͺ

ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Java

[Java] μžλ°” this, this()와 super, super()

2023. 1. 25. 16:42

[ this ν‚€μ›Œλ“œ ]

더보기

[ this ν‚€μ›Œλ“œ ]

- μ–Έμ  κ°€ 생성될 μžμ‹  객체(μΈμŠ€ν„΄μŠ€)의 λ©”λͺ¨λ¦¬ μ£Όμ†Œ

- μΈμŠ€ν„΄μŠ€κ°€ 자기 μžμ‹ μ„ μ°Έμ‘°ν•  λ•Œ μ‚¬μš©

- this.λ©€λ²„ν•„λ“œ

   · ν΄λž˜μŠ€λ‚΄ μžμ‹ μ˜ 멀버λ₯Ό 직접 μ°Έμ‘°

   · λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜μ™€ 클래슀의 λ©€λ²„ν•„λ“œκ°€ 동일할 λ•Œ 이λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

 

[ this ν‚€μ›Œλ“œ μ‚¬μš© 예제 ]

class HumanBody{
	private String organs;
	private String muscle;
	private int bone;
	private int blood;
	
    // λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜μ™€ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λͺ…이 같을 경우 this ν‚€μ›Œλ“œλ₯Ό 톡해 ꡬ뢄 
	HumanBody(String organs, String muscle, int bone, int blood){
		this.organs = organs;
		this.muscle = muscle;
		this.bone = bone;
		this.blood = blood;
	}
	void printHumanBody() {
		System.out.println(organs + " " + muscle + " " + bone + " " + blood);
	}
	
}

public class TestCode {
	
	public static void main(String[] args) {
		HumanBody Hong = new HumanBody("Brain", "arm", 206, 120);
		Hong.printHumanBody();
	}
}
/* [좜λ ₯ κ²°κ³Ό]
	Brain arm 206 120 */

 

 

[ this() λ©”μ†Œλ“œ ]

더보기

[ this() λ©”μ†Œλ“œ ]

- 클래슀 μžμ‹ μ˜ μƒμ„±μž λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©

- μžμ‹ μ˜ μƒμ„±μž λ©”μ†Œλ“œλ₯Ό μž¬μ΄μš©ν•˜λŠ” 것

- 이 λ•Œ μƒμ„±μž λ©”μ†Œλ“œμ—μ„œ λ‹€λ₯Έ μƒμ„±μž λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  경우

   this() λ©”μ†Œλ“œμ˜ ν˜ΈμΆœμ€ λ°˜λ“œμ‹œ μƒμ„±μžμ˜ μ΅œμƒλ‹¨μ—μ„œλ§Œ κ°€λŠ₯

 

[ this() λ©”μ†Œλ“œ μ‚¬μš© 예제 ]

class HumanBody{
	private String organs;
	private String muscle;
	private int bone;
	private int blood;
	
    // -- constructor --
	HumanBody(String organs, String muscle, int bone, int blood){
		this.organs = organs;
		this.muscle = muscle;
		this.bone = bone;
		this.blood = blood;
	}
	HumanBody(int bone, int blood){
		this.bone = bone;
		this.blood = blood;
	}
	HumanBody(){
    	// 두 개의 μ •μˆ˜ν˜• λ§€κ°œλ³€μˆ˜ μž…λ ₯λ°›λŠ” μƒμ„±μžλ₯Ό 호좜 this()
		this(310, 240);   // μƒμ„±μžμ˜ 호좜 μœ„μΉ˜λŠ” μ΅œμƒλ‹¨μ΄μ–΄μ•Ό 함
		organs = "heart";
		muscle = "leg";
	}
	
    // -- print Method --
	public void printHumanBody(){
		System.out.println(organs + " " + muscle + " " + bone + " " + blood);
	}
}

public class TestCode {
	
	public static void main(String[] args) {
		HumanBody Hong = new HumanBody("Brain", "arm", 206, 120);
		Hong.printHumanBody();
		
		HumanBody Kim = new HumanBody();
		Kim.printHumanBody();
	}
}

/* [좜λ ₯ κ²°κ³Ό]
    Brain arm 206 120
    heart leg 310 240 */

 

 

 

[ super ν‚€μ›Œλ“œ ]

더보기

[ super ν‚€μ›Œλ“œ ]

- μƒμœ„ ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속받은 λ©€λ²„ν•„λ“œλ‚˜ λ©”μ†Œλ“œλ₯Ό ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ

  μ°Έμ‘°ν•˜μ—¬ λͺ…μ‹œν•  λ•Œ μ‚¬μš©

- this ν‚€μ›Œλ“œμ™€ 같이 μƒμœ„ 클래슀의 λ©€λ²„ν•„λ“œμ™€ ν•˜μœ„ 클래슀의 λ©€λ²„ν•„λ“œλͺ…이 같을 경우 

  super ν‚€μ›Œλ“œλ₯Ό 톡해 ꡬ뢄 κ°€λŠ₯

- this ν‚€μ›Œλ“œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ μ°Έμ‘° λ³€μˆ˜λ‘œ μ‚¬μš©ν•  수 μžˆλŠ” λŒ€μƒμ€ μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œλΏμž„.

  (static으둜 μ„ μ–Έλœ 클래슀 λ©”μ†Œλ“œx)

 

[ super ν‚€μ›Œλ“œ μ‚¬μš© 예제 ]

class Animal{
	public String dog = "schnauzer";
}

class Mammalia extends Animal{
	private String dog = "poodle";
	void display() {
		System.out.println("Mammalia.dog : " + dog);
		System.out.println("this.dog : " + this.dog);
		System.out.println("super.dog : " + super.dog);
	}
}

public class TestCode {
	
	public static void main(String[] args) {
		Mammalia animal = new Mammalia();
		animal.display();
	}
}

/* [좜λ ₯ κ²°κ³Ό]
    Mammalia.dog : poodle
    this.dog : poodle
    super.dog : schnauzer */

 

 

[ super() λ©”μ†Œλ“œ ]

더보기

[ super() λ©”μ†Œλ“œ ]

- μƒμœ„ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©

β‘  ν•˜μœ„ 클래슀 객체 생성 μ‹œ κ°μ²΄λŠ” ν•˜μœ„ 클래슀의 고유 λ©€λ²„λ³€μˆ˜μ™€ μƒμœ„ 클래슀의 멀버λ₯Ό λͺ¨λ‘ 포함

β‘‘ μƒμœ„ 클래슀의 λ©€λ²„λ³€μˆ˜ μ΄ˆκΈ°ν™” ν•„μš”

β‘’ ν•˜μœ„ 클래슀의 μƒμ„±μžμ—μ„œ λΆ€λͺ¨ 클래슀 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•¨μœΌλ‘œ μƒμœ„ 클래슀 μ΄ˆκΈ°ν™”

β‘£ μžλ°” μ»΄νŒŒμΌλŸ¬λŠ” λ¬΅μ‹œμ μœΌλ‘œ  μƒμœ„ 클래슀 μƒμ„±μžλ₯Ό ν•˜μœ„ 클래슀 μƒμ„±μžμ˜ 첫 쀄에 μžλ™μœΌλ‘œ μΆ”κ°€

β€» 이 λ•Œ ν΄λž˜μŠ€μ— μ •μ˜λœ μƒμ„±μžκ°€ μ—†μ–΄μ•Όλ§Œ μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ μΆ”κ°€

 

[ super() λ©”μ†Œλ“œ μ‚¬μš© 예제 ]

class Animal{
	public String dog;
	// -- structor --
	Animal(){
		dog = "schnauzer";
	}
	Animal(String dog){
		this.dog = dog;
	}
}
 
class Mammalia extends Animal{
	private String cat;
	
	Mammalia(){
		// ν•˜μœ„ 클래슀의 μƒμ„±μžμ—μ„œ μƒμœ„ 클래슀인 Animal의 μƒμ„±μž ν˜ΈμΆœμ„ 톡해 λ©€λ²„λ³€μˆ˜ μ΄ˆκΈ°ν™”
		super();
		cat = "sphinx";
	}
	void display() {
		System.out.println("dog : " + dog);
		System.out.println("cat : " + cat);
	}
}

public class TestCode {
	
	public static void main(String[] args) {
		Mammalia animal = new Mammalia();
		animal.display();
	}
}

/* [좜λ ₯ κ²°κ³Ό]
    dog : schnauzer
    cat : sphinx
*/
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)
    'ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • [Java] μžλ°” μΊμŠ€νŒ…
    • [Java] μžλ°” 좔상 λ©”μ†Œλ“œ, 좔상 ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€
    • [Java] μžλ°” 상속
    • [Java] 클래슀 λ©”μ†Œλ“œμ™€ μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œ
    めけゃくけゃ ι–‹η™Ίθ€…πŸ¦Ύ
    めけゃくけゃ ι–‹η™Ίθ€…πŸ¦Ύ
    πŸ‘Š λΈ”λ‘œκ·Έλ„ 근성이닀? πŸ‘Š

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”