this - 자기자신에 접근하는 레퍼런스
instance method의 첫번째 매개변수로 존재하고 있다.
this() - this 호출 -> () : 호출한다
자기자신을 호출한다. -> constructor를 호출한다.
: constructor에서 또 다른 cons를 호출. ( 오버로딩된 constructor를 호출)
클래스 관계
~has a : ~가 a를 가지고 있다.
: 객체가 필요에 의해서 객체를 가져다 쓰는 관계
: 독립된 객체 : 데이터 클래스
데이터 클래스를 갖다쓰는 클래스 : 데이터 관리 클래스
public class ThisExam {
private int num1,num2;
public ThisExam() {
num1=num2=0;
}
public ThisExam(int num) {
this.num1 = num;
num2 = 0;
}
public ThisExam(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum1() {
return this.num1;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public int getNum2() {
return this.num2;
}
public static void main(String[] args) {
ThisExam tc1 = new ThisExam();
ThisExam tc2 = new ThisExam(1);
ThisExam tc3 = new ThisExam(2,5);
System.out.println(tc1.getNum1()+"\t"+tc1.getNum2()); //0,0
System.out.println(tc2.getNum1()+"\t"+tc2.getNum2()); //1,0
System.out.println(tc3.getNum1()+"\t"+tc3.getNum2()); //2,5
/////////////////this 실습//////////////
}
}
tc1,tc2,tc3에 각자 하부에 객체가 생성되며 이들을 별도의 이름으로 호출하지 않고 this 를 이용하여 자기 자신을 호출하여 변수가 중복되어도 가르키는 객체는 다르다는 것을 명확히 인지시킬 수 있다는 점을 알 수 있다.
public ThisCall() {
this(0,0);
}
public ThisCall(int num) {
this(num,0);
}
public ThisCall(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}// 오류 발생시 여기만 고치면 된다 -> 관리가 쉬워진다.
constructor를 this를 활용하여 훨씬 간결하게 작성할 수 있으며 그 결과 유지보수를 보다 쉽게 할 수 있다.
class A{ // data class
String n;
public String getN() {
return n;
}
public void setN(String n) {
this.n = n;
}
}
class B{ //data class
int n=0;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}
public class ThisExam3 {
// has ~A 관계
private A name;
private B age;
//constructor
public ThisExam3() {
name = new A();
age = new B();
}
//method
public void setName(String nam) {
name.setN(nam);
}
public String getName() {
return name.getN();
}
public int getAge() {
return age.getN();
}
public void setAge(int num) {
age.setN(num);
}
public static void main(String[] args) {
ThisExam3 cc = new ThisExam3();
cc.setName("Superman");
cc.setAge(1000);
System.out.println(cc.getName());
System.out.println(cc.getAge());
}
}
public class Score{ //data class
//field
private String name;
private int kor;
private int eng;
private int mat;
private int total;
private float avg;
//constructor
public Score() {
name = "hi";
kor = 0;
eng=0;
mat=0;
total=0;
avg=0;
}
//method
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTotal() {
return total= kor+eng+mat;
}
public float getAvg() {
return avg = getTotal()/3.f;
}
}
import java.util.Scanner;
public class ScoreMag {
private String []txt = {"kr", "en", "mt", "sci", "so","avg","tot"};
private Score [] stu;
private int cnt,req;
public ScoreMag(int i) {
stu = new Score[i];
cnt = 0;
req = 0;
}
public void setStu(int i, Score stu) {
this.stu[i] = stu;
}
private void disp(int i) {
for(int j =0; j<3;j++) {
System.out.print(txt[j]+"\t");
}
System.out.print(txt[5]+"\t"+txt[6]+"\n");
System.out.print(stu[i].getKor()+"\t");
System.out.print(stu[i].getEng()+"\t");
System.out.print(stu[i].getMat()+"\t");
System.out.print(stu[i].getTotal()+"\t");
System.out.print(stu[i].getAvg()+"\n");
}
public static void main(String[] args) {
System.out.println("학생 수를 입력하세요");
Scanner sc = new Scanner(System.in);
int man = sc.nextInt();
ScoreMag sm = new ScoreMag(man);
Score sr = null;
do{
System.out.println("1. 입력, 2. 출력");
sm.req = sc.nextInt();
if(sm.req ==1 && sm.cnt<man) {
sr = new Score();
System.out.println("이름 국어 수학 영어 성적을 입력하세요");
sr.setName(sc.next());
sr.setKor(sc.nextInt());
sr.setMat(sc.nextInt());
sr.setEng(sc.nextInt());
sm.setStu(sm.cnt,sr);
sm.disp(sm.cnt);
sm.cnt++;
}else {
break;
}
}while(sm.req !=2);
System.out.println("완료 되었습니다.");
}
}
성적을 입력하고 관리할 수 있는 프로그램 예시. Score class에 저장된 것들을 호출하여 ScoreMag에서 사용한다.
Score sr과 ScoreMag sm 을 new를 이용해서 객체를 생성해주는 과정을 어디서 실행하냐에 따라 결과에 많은 영향을 미칠 수 있다.
'빅데이터교육과정 > JAVA' 카테고리의 다른 글
DAY 6. 상속(is~A) / Override (0) | 2021.02.04 |
---|---|
DAY 5. Class : has~ a 응용 (0) | 2021.02.04 |
DAY4. Class. (0) | 2021.02.02 |
DAY 3. 배열, 함수 응용 과제(성적입력) (0) | 2021.02.01 |
DAY 3. 배열(Array)과 형변환 (0) | 2021.02.01 |