Develop/곤부📙
getter / setter
밀김
2023. 4. 21. 17:51
728x90
package test;
public class Exam {
private int kor;
private int math;
private int eng;
public Exam(){ //exam 생성자
// this.kor = 10;
// this.math = 10;
// this.eng = 10;
//기본 생성자 > 이제 안씀.
this(10,10,10);
}
public Exam(int kor, int math, int eng) { //오버로드 생성자
this.kor = kor;
this.eng = eng;
this.math = math;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
}
package test;
public class NewlecExam extends Exam{//상속받음
private int com;
public NewlecExam() {
this(10,10,10,10);
}
public NewlecExam(int kor, int math, int eng, int com) {
super(kor,math,eng); //부모꺼
this.com = com; //자기꺼
}
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
public int total(int kor, int math, int eng, int com) {
int total = kor+math+eng+com;
return total;
}
}
package test;
public class NewlecExam2 extends Exam{
private int java;
public NewlecExam2() {
this(10,10,10,10);
}
//오버로드생성자는 기본생성자를 무조건 갖고있어야한다.
public NewlecExam2(int kor,int math,int eng,int java) {
super(kor,math,eng);
this.java = java;
}
public int getJava() {
return java;
}
public void setJava(int java) {
this.java = java;
}
}
package test;
public class Program {
public static void main(String[] args) {
//출력만하는곳
NewlecExam exam = new NewlecExam();
NewlecExam2 exam2 = new NewlecExam2();
//사용할수있게
int a = exam.getCom(); //가져옴
System.out.println(a);
exam.setCom(90); //90으로 설정함
int b = exam.getCom(); //90으로 set한걸 다시 가져옴
System.out.println(b);
int total = exam.total(100,100,100,100);
System.out.println(total);
exam2.setJava(100);
int c = exam2.getJava();
System.out.println(c);
}
}
728x90