변수의 자료형
클래스 형으로 선언하는 자료형
예시)
학생의 속성 중 수업에 대한 부분,
수업에 대한 각 속성을 학생 클래스에 정의하지 않고 수업이라는 클래스로 분리해서 사용
이떄 과목은 참조 자료형으로 선언
한 번에 만드는 class 학생
학번 학생이름, 국어성적, 수학성적, 수강하는 과목 이름
나눠서 만드는 class 학생 + 과목
학생 class
학번 ,학생이름, 국어과목, 수학과목
과목 class
과목이름, 과목 점수
학생 클래스
public class Student {
int studentID;
String studentName;
Subject korea; //참조 자료형 선언 -> class생성된 것 아님
Subject math;
public Student() {
korea = new Subject(); //인스턴스 생성
math = new Subject();
}
public Student(int id, String name) {
studentID = id;
studentName = name;
korea = new Subject(); //인스턴스 생성
math = new Subject();
}
public void setKorea(String name , int score) {
korea.setSubjectName(name);
korea.setScore(score);
}
public void setMath(String name , int score) {
math.setSubjectName(name);
math.setScore(score);
}
public void showStudentInfo() {
int total = korea.getScore() + math.getScore();
System.out.println(studentName + "학생의 총점은" + total +"점 입니다.");
}
}
과목 class
public class Subject {
String subjectName;
int score;
public void setSubjectName(String name) {
subjectName = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getSubjectName() {
return subjectName;
}
}
동작시키는 class
public static void main(String[] args) {
Student jameStudent = new Student(11, "james");
jameStudent.setKorea("국어", 99);
jameStudent.setMath("수학", 99);
Student meoStudent = new Student(22, "meo");
meoStudent.setKorea("국어", 98);
meoStudent.setMath("수학", 75);
jameStudent.showStudentInfo();
meoStudent.showStudentInfo();
}
}
정보은닉 (information hiding)
private 접근 제어자
: 클래스의 외부에서 클래스 내부의 멤버 변수나 메서드에 접근하지 못하게 하는 경우 사용
-> 멤버 변수나 메서드를 외부에서 사용하지 못하도록 하여 오류를 줄일 수 있다.
변수에 대해서는 필요한 경우 get(), set() 메서드를 제공
메서드를 이용해서 사용가능
주의할 점)
class 이름 = public class 이름이 같아야 함.
class안에서 사용할 수 있음. -> public static void main(String[] args){}에서 사용불가
class Birthday{
private int day; // 다른 class에서 안 보임. 이 안에서만 사용 가능
public int month;
public int year;
}
public class BirthdayTest {
public static void main (String[]args) {
Birthday day = new Birthday(); // 이 부분이 오류남.
}
}
private를 썼을 때 값을 입력해주고 싶으면 get(), set()을 사용해서 입력받으면 된다
class Birthday{
private int day;
private int month;
private int year;
// private는 하이딩됨.
public int getDay() {
return day;
}
public void setDay(int day) {
if(month == 2) {
if(day <1 || day >28) {
System.out.println("날짜 오류입니다.");
}
else {
this.day = day;
}
}
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
public class BirthdayTest {
public static void main (String[]args) {
Birthday day = new Birthday();
day.setMonth(2); //2월을 먼저 받아야 if문 실행됨
day.setDay(30);
day.setYear(2023);
}
}
아직 왜 쓰는지 모르겠음,,, 그저,, 무결성에 오류가 나니 쓰는걸로,,
자신의 메모리를 가리킴 =생성된 인스턴스 스스로를 가리키는 예약어
하는 역할)
생성자에서 다른 생성자를 호출
자신의 주소를 반환
생성자에서 다른 생성자를 호출
class Person{
int age;
String name;
public Person() {
//name = "상수입"; 설명) this를 사용할 때 이 앞에 올 수 없음. = 문장이 올 수 없다. 제일 먼저 this가 와야 됨.
this("이름없음",1);
//name = "이름없음";
//age = 1; 와 같음
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person returnSelf() {
return this; //Person 클래스명과 동일하게 작성(자기자신 클래스 명으로 작성) = return type 반환
}
}
public class CallAnotherConst {
public static void main(String[] args) {
Person p1 = new Person();
System.out.println(p1.name);
System.out.println(p1.returnSelf());
}
}