Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 버리자
- 데이터사이언스
- javaBeans
- 로그인화면
- opener
- \
- 동기부여
- 미래직장
- 자바빈즈
- 영감
- ;
- static
- iframe
- 팝업창
- 파일 호출
- 페이지 이동
- BEANS
- session.removeAttribute
- scanner
- session.setAttribute
- Import
- 향상된 for문
- "
- 파일호출
- 빈즈
- session.getAttribute
- 페이지이동
- target
- 동기 부여
- Alert
Archives
- Today
- Total
갈림길 이정표
추상 클래스 연습 본문
package EmployeeProblem;
public abstract class Employee {
// 모든 subclass '공통' 부분 멤버필드 선언
private String irum;
private int nai;
// 모든 subclass '공통' 부분 생성자 선언
public Employee(String irum, int nai) { // (1st)입력 받을 데이터 정의
this.irum = irum; //입력값을 저장시키기 위해 (2nd)받은 데이터 변수에 저장
this.nai = nai;
}// this.변수 씀으로써 입력시 값 바로 치환
// 모든 subclass '공통'메소드 선언
abstract int pay(); //→ 정의가 바뀔 수 있는 부분이므로 오버라이딩
// 만약 뒤에 붙는 소수점이 싫다면 int로 데이터 타입 바꿈
abstract void print(); //→ 정의가 바뀔 수 있는 부분이므로 오버라이딩
void display() { //→ 모든 subclass에서 '공통'으로 쓰일 메소드 (고정)
System.out.print(irum + ", " + nai);// (3rd)저장된 변수 이용하여 메소드 실행
}
}
//System.out.println은 출력 실행 후 커서 다음줄로 이동
package EmployeeProblem;
public class Temporary extends Employee{
private int ilsu;
private int ildang;
//↙여기 다 지역 변수 (나중에 값 입력받기 위한 정의된 데이터)
public Temporary(String irum, int nai, int ilsu, int ildang) { //반드시 생성자 명은 class명과 같아야 발동.
// (1st)입력 받을 데이터 정의
super(irum, nai); // (2nd)받은 데이터 부모 class변수에 저장 (여기 멤버필드가 아니니까)
this.ilsu = ilsu; // (3rd)받은 데이터 변수에 저장
this.ildang = ildang;
}// super.변수 씀으로써 입력시 값 바로 부모클래스 멤버 변수에 치환
@Override
int pay() { // (4th)저장된 변수 이용하여 메소드 실행
int pay = ilsu * ildang;
return pay; // pay()를 반환시킴 (메소드 실행 시 나올 값)
}
@Override
void print() {
display(); // (5th)메소드 속에 기존 부모 class 메소드도 활용 가능
System.out.println(", 월급: " + pay()); //※ 메소드 반환값이 필요하므로 그냥 pay가 아닌 pay()
}
}
package EmployeeProblem;
public class Regular extends Employee{
private int salary;
public Regular(String irum, int nai, int salary) {
super(irum, nai); //argument가 여러개일 경우: 구두점(.)안찍어도 됨
this.salary = salary; //하나하나 일 경우: 구두점(.) 찍어야 됨
}
@Override
int pay() { //여기는 왜 argument선언 안하지?
//왜 데이터 타입 정의 해야 되지?
return salary;
}
@Override
void print() {
display();
System.out.println(", 급여: " + pay());
}
}
package EmployeeProblem;
public class Salesman extends Regular{
private int sales;
private double commission;
public Salesman(String irum, int nai, int salary, int sales, double commission) {
// (1st)입력 받을 데이터 정의
super(irum, nai, salary); // (2nd)받은 데이터 부모 class변수에 저장 (여기 멤버필드가 아니니까)
//부모 클래스 생성자 형태랑 똑같 // 부모 클래스 생성자 선언 당시 이용했던 argument 모두 그대로 해야 받을 수 있음
// salary까지는 Regular class에서 받고 나머지는 Employee까지 올라감
this.sales = sales; // (3rd)받은 데이터 변수에 저장
this.commission = commission;
// this(sales, commission); → 요건 불가능
// super 라는 것은 subclass에서 여러 argument를 super class로 넘겨 super class에서 처리하도록 되지만
// this.변수 에서는 덩어리 argument로 값을 치환할 경우 어떻게 처리해야 될 지 정의 되어 있지 않아 err.
}
@Override
int pay() { // (4th)저장된 변수 이용하여 메소드 실행
// double allowance = sales * commission;
// double pay = salary + allowance; //우리는 Regular class pay 메소드에 추가로 영업 수당을 더하면 된다.
// return pay(); //super.pay() 자체가 Regular class의 salary 값이 였기 때문에
return super.pay() + (int)(sales * commission); //우리가 추가해야 될 부분은 commission뿐
} // super.pay(): 부모 class pay메소드 값으로 치환!
// (4th-1)Salesman pay() 메소드 실행 시 superclass에서 받은 super.pay() 값에 정보를 추가하여 return으로 반환
// 만약 뒤에 붙는 소수점이 싫다면 int로 데이터 타입 바꿈(만약 강제형변환[casting] 하고 싶다면 (데이터 타입)변수 명
@Override
void print() {
display(); // (5th) display() 메소드는 부모인 Regular에 없으므로 그 상위 부모 Employee class에서 참조
System.out.println(", 수령액: " + pay());
}
}
//메소드 호출은 = 메소드 명 ()
//※변수 호출은 클래스 내에선 그냥 변수명 (보통 지역변수), 전역변수는 this.변수명
package EmployeeProblem;
public class Manager extends Regular{
private double incentive;
public Manager(String name, int nai, int salary) {
super(name, nai, salary);
}
@Override
public int pay() {
// double pay = salary + incentive;
// return pay; //super.pay() 자체가 Regular class의 salary 값이 였기 때문에
if(super.pay() >= 2500000) incentive = super.pay() * 0.6;
else if(super.pay() >= 2000000) incentive = super.pay() * 0.5;
else if(super.pay() < 2000000) incentive = super.pay() * 0.4; //else는 두가지 옵션만 있을 때 쓰나?
return super.pay() + (int)incentive; //우리가 추가해야 될 부분은 incentive뿐
}
@Override
void print() {
display();
System.out.println(", 수령액: " + pay());
}
}
package EmployeeProblem;
public class EmployeeMain {
public static void main(String[] args) {
// 임시직: 이름, 나이, 일수, 일당
Temporary tem = new Temporary("홍길동", 23, 20, 150000); //실제 데이터 값 입력
tem.print();
// 정규직: 이름, 나이, 고정급여
Regular reg = new Regular("신기해", 25, 2345000);
reg.print();
// 영업직: 이름, 나이, 고정급, 실적, 수수료율
Salesman sal = new Salesman("손오공", 27, 2890000, 30000, 0.25);
sal.print();
// 관리직: 이름, 나이, 고정급
Manager man = new Manager("사오정", 33, 4578000);
man.print();
}
}
'Programming Language > Java 문제풀이(feat. 이것이 자바다)' 카테고리의 다른 글
DTO 연습 (0) | 2020.07.23 |
---|---|
클래스 포함관계 연습 (0) | 2020.07.19 |