๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /Study

์ฐธ์กฐ์ž๋ฃŒํ˜• reference data type

by 6161990 2021. 7. 19.

๐Ÿ“Œ ์ฐธ์กฐ์ž๋ฃŒํ˜• reference data type

  • ์ฐธ๊ณ  : ๊ฐ์ œ ์ง€ํ–ฅ ์–ธ์–ด OOP, ๊ฐ์ œ ์ง€ํ–ฅ ์–ธ์–ด์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์™€ ํŠน์ง•
  • ํด๋ž˜์Šค๋ฅผ ์ž๋ฃŒํ˜•์œผ๋กœ ์„ ์–ธํ•จ. ๊ทธ๋•Œ์˜ ๋ณ€์ˆ˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค(์ฐธ์กฐ) ๋ณ€์ˆ˜
  • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€๋งŒ, ์ฐธ์กฐ์ž๋ฃŒํ˜•์€ ํด๋ž˜์Šค์— ๋”ฐ๋ผ ๋‹ค๋ฆ„.
  • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์€ ์‹ค์ œ ๋ฐ์ดํ„ฐ(๊ฐ’)์„ ์ €์žฅํ•˜์ง€๋งŒ, ์ฐธ์กฐ์ž๋ฃŒํ˜•์€ ํ• ๋‹น๋œ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•จ.
  • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์€ ๋…ผ๋ฆฌํ˜•, ๋ฌธ์žํ˜•, ์ •์ˆ˜ํ˜•, ์‹ค์ˆ˜ํ˜•์œผ๋กœ ๋‚˜๋ˆ ์ง€๊ณ , 8๊ฐœ์˜ ์ž๋ฃŒํ˜•์ด ์žˆ๋‹ค.
  • ์ฐธ์กฐ์ž๋ฃŒํ˜•์€ 4byte์˜ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜๋ฉฐ ์ •์ˆ˜ํ˜• ์ฃผ์†Œ๋งŒ์„ ์ €์žฅํ•จ(๊ฐ’์€ ์ €์žฅํ•˜์ง€ ๋ชปํ•จ)

 


๐Ÿ“ ์ฐธ์กฐ์ž๋ฃŒํ˜• ์ง์ ‘ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ธฐํ•™์ƒํด๋ž˜์Šค Student ์— ์žˆ๋Š” ๊ณผ๋ชฉ ์ด๋ฆ„, ๊ณผ๋ชฉ ์„ฑ์  ์†์„ฑ์„

๊ณผ๋ชฉ ํด๋ž˜์ŠคSubject ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  Subject ์ฐธ์กฐ ์ž๋ฃŒํ˜• ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ Student์— ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•จ.

 


๊ณผ๋ชฉ ํด๋ž˜์Šค

public class Subject { 
  public int SubjectID; 
  public String SubjectName; 
  public int score; 
}


ํ•™์ƒํด๋ž˜์Šค

public class Student { 
  public int studentID; 
  public String studentName; 
  Subject korea; 
  Subject math; 

  public Student(int id, String name){
    studentId = id;
    studentName = name; 
    korea = new Subject();
    math = new Subject(); 
  } 

  public void setKoreaSubject(String name, int score){
    korea.SubjectName = name;
    korea.score = score; 
  }

  public void setMathSubject(String name, int score){ 
    math.SubjectName = name; 
    math.score = score; 
  } 

  public void showStudentScore(){ 
    int total = korea.score + math.score; 
    System.out.println(studentName + "ํ•™์ƒ์˜ ์ด์ ์€" + total + "์  ์ž…๋‹ˆ๋‹ค."); 
  } 
}


์„ฑ์  ํ…Œ์ŠคํŠธ

public class StudentTest { 

  public static void main(String[] args) { 
    Student studentLee = new Student(101, "์ด๋ง๋…„") 
    studentLee.setKoreaSubject("๊ตญ์–ด", 75); 
    studentLee.setMathSubject("์ˆ˜ํ•™", 65); 
    studentLee.showStudentScore(); 

    Student studentKim = new Student(102, "๊น€ํ’"); 
    studentKim.setKoreaSubject("๊ตญ์–ด", 50); 
    studentKim.setMathSubject("์ˆ˜ํ•™", 38); 
    studentKim.showStudentScore(); 
  } 
}