Eggs Sunny Side Up
본문 바로가기
언어/JAVA

OOP_추상화

by guswn100059 2023. 3. 11.

월급계산기

방법1)

슈퍼클래스

package 연습장;

public abstract class Employee {
	
	String empno;
	String name;
	int pay;
	
	public abstract int getMoneyPay();
	
	public String print() {
		return (empno+":"+name+":"+pay);
	}
	
}

 

서브클래스1

package 연습장;

public class RegularEmployee extends Employee{
	
	
	int bonus;
	
	//생성메소드
	public RegularEmployee(String empno, String name, int pay, int bonus) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
		this.bonus = bonus;
	}
	
	public int getMoneyPay() {
		int result = (pay+bonus)/12;
		return result;
	}
}

 

서브클래스2

package 연습장;

public class TempEmployee extends Employee{
	
	
	public TempEmployee(String empno, String name, int pay) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
	}
	
	public int getMoneyPay() {
		int result = pay/12;
		return result;
	}
}

 

서브클래스3

package 연습장;

public class PartTimeEmployee extends Employee{
	
	
	int workDay;
	
	
	public PartTimeEmployee(String empno, String name, int pay, int workDay) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
		this.workDay = workDay;
	}
	
	public int getMoneyPay() {
		int result = pay*workDay;
		return result;
	}

}

 

Main

package 연습장;

public class EmployeeMain {

	public static void main(String[] args) {
		
		RegularEmployee re = new RegularEmployee("SMHRD001",  "홍길동", 4000, 400);
		System.out.println(re.print());
		System.out.println(re.getMoneyPay()+"만원");
		
		
		TempEmployee te = new TempEmployee("SMHRD002",  "박땡수", 3000);
		System.out.println(te.print());
		System.out.println(te.getMoneyPay()+"만원");
		
		
		PartTimeEmployee pte = new PartTimeEmployee("SMHRD003",  "김땡독", 10, 10);
		System.out.println(pte.print());
		System.out.println(pte.getMoneyPay()+"만원");
		
	}

}

방법2)

슈퍼클래스

package 연습장;

public abstract class Employee {
	
	String empno;
	String name;
	int pay;
	
	public abstract void getMoneyPay();
	
	public void print() {
		System.out.println(empno+":"+name+":"+pay);
	}
	
}

 

서브클래스1

package 연습장;

public class RegularEmployee extends Employee{
	
	
	int bonus;
	
	//생성메소드
	public RegularEmployee(String empno, String name, int pay, int bonus) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
		this.bonus = bonus;
	}
	
	public void getMoneyPay() {
		System.out.println((pay+bonus)/12+"만원");
	}
}

 

 

서브클래스2

package 연습장;

public class TempEmployee extends Employee{
	
	
	public TempEmployee(String empno, String name, int pay) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
	}
	
	public void getMoneyPay() {
		System.out.println(pay/12+"만원");
	}
}

 

서브클래스3

package 연습장;

public class PartTimeEmployee extends Employee{
	
	
	int workDay;
	
	
	public PartTimeEmployee(String empno, String name, int pay, int workDay) {
		super();
		this.empno = empno;
		this.name = name;
		this.pay = pay;
		this.workDay = workDay;
	}
	
	public void getMoneyPay() {
		System.out.println(pay*workDay+"만원");
	}

}

 

Main

package 연습장;

public class EmployeeMain {

	public static void main(String[] args) {
		
		RegularEmployee re = new RegularEmployee("SMHRD001",  "홍길동", 4000, 400);
		run(re);
		
		
		TempEmployee te = new TempEmployee("SMHRD002",  "박땡수", 3000);
		run(te);
		
		
		PartTimeEmployee pte = new PartTimeEmployee("SMHRD003",  "김땡독", 10, 10);
		run(pte);
		
	}
	
	public static void run(Employee e) {
		e.print();
		e.getMoneyPay();
	}

}

'언어 > JAVA' 카테고리의 다른 글

JDBC 기초_회원가입창 만들기  (2) 2023.03.14
Interface(인터페이스)  (0) 2023.03.13
OOP_상속  (0) 2023.03.11
ArrayList_제네릭 기법 + for-each문  (0) 2023.03.10
OOP_클래스와 객체, 배열  (0) 2023.03.09

댓글