목차
접기
728x90
반응형
참조 자료형
- String은 JDK(Java Development Kit)에서 제공하는 참조 자료형이다.
package algo2;
public class Student {
public int studentID;
public String studentName;
public Subject korea;
public Subject math;
}
package algo2;
public class Subject {
public String subjectName;
public int subjectScore;
}
package algo2;
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
student1.studentName = "홍길동";
student1.studentID = 1111;
Subject subjectKorea = new Subject();
subjectKorea.subjectName = "국어";
subjectKorea.subjectScore = 80;
student1.korea = subjectKorea;
Subject subjectMath = new Subject();
subjectMath.subjectName = "수학";
subjectMath.subjectScore = 100;
student1.math = subjectMath;
System.out.println("이름 : " + student1.studentName);
System.out.println("학생번호 : " + student1.studentID);
System.out.println("시험과목 : " + student1.korea.subjectName);
System.out.println("점수 : " + student1.korea.subjectScore);
System.out.println("시험과목 : " + student1.math.subjectName);
System.out.println("점수 : " + student1.math.subjectScore);
}
}- 학생의 수강과목 수학, 국어의 시험 점수를 저장하는 프로그램
- 학생 클래스에 수강과목, 시험점수를 관리하는 멤버 변수를 추가하는 것보다 과목 클래스를 따로 만들어서 구분
- 다른 클래스를 변수로 사용하는 것도
참조 자료형에 속한다.
한개의 클래스에 모든 정보를 담으려 하지 말고 관련된 정보들을 모아 클래스를 구분해서
참조 자료형으로 변수를 선언해서 사용하는 것이 훨씬 효율적이다.728x90
반응형
LIST
'Java' 카테고리의 다른 글
| [ java ] 정보 은닉, 접근 제어자 (0) | 2020.12.17 |
|---|---|
| [ java ] 생성자(constructor) (0) | 2020.12.14 |
| [ java ] 객체 지향 기초 개념 7가지 (0) | 2020.12.14 |
| [ java ] 변수, 클래스, 객체, 메모리 외 기초 설명 (0) | 2020.12.14 |
| [ java ] 반복문 사용의 경우, break문과 continue문의 차이 (0) | 2020.12.13 |