[Java] 클래슀, 상속, getter, setter, ν˜•λ³€ν™˜, super, μ˜€λ²„λΌμ΄λ”©μ„ ν™œμš©ν•œ μˆ™μ œ

2021. 3. 16. 19:37γ†κ°œλ… 정리 μž‘μ—…μ‹€/Java

728x90
λ°˜μ‘ν˜•

μ•ˆλ…•ν•˜μ„Έμš”? μ£Όλ‹ˆν•˜λž‘ μž…λ‹ˆλ‹€.

03μ›” 16일 μˆ™μ œλ₯Ό ν•œλ²ˆ 해보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€!

μˆ™μ œ λ‚΄μš©μ„ κ³΅μœ ν•΄μ„œ μ—¬λŸ¬λΆ„λ“€κ»˜μ„œλ„ κ³΅λΆ€ν•˜μ‹œλŠ”λ°, λ§Žμ€ 도움 λ˜μ‹œκΈ° λ°”λΌκ² μŠ΅λ‹ˆλ‹€!

그럼 λ°”λ‘œ μ‹œμž‘ ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€!

 

πŸ“Œ λ‚˜λŠ” μ„€κ³„μž!


Super Class와 Sub Class(2개 정도)λ₯Ό μ΄μš©ν•˜μ—¬ 데이터λ₯Ό ν‘œν˜„ν•˜λŠ” ν”„λ‘œκ·Έλž¨ κ΅¬ν˜„ 각 ν΄λž˜μŠ€λ§ˆλ‹€ λ³€μˆ˜, λ©”μ„œλ“œ, μƒμ„±μžλ₯Ό μš°μ„  μ •μ˜ν•΄μ•Ό ν•œλ‹€. getter, setter, 상속, ν˜•λ³€ν™˜, super ν‚€μ›Œλ“œ, μ˜€λ²„λΌμ΄λ”©μ„ μ‚¬μš©ν•  것!

   

πŸ“ 클래슀 κ΅¬ν˜„ν‘œ

         πŸ‘‰μ£Όμ œ: μ»€ν”Όμˆ

클래슀 이름 μ„€                 λͺ…
Cafe.java Main Method
People.java
private String name;    // 이름
private String phoneNum;   // ν•Έλ“œν° 번호
private String age;   // λ‚˜μ΄

### Method ###

getName() { ... }  // μ΄λ¦„μ˜ getter

setName() { ... }  // μ΄λ¦„μ˜ setter
getPhoneNum() { ... }  // ν•Έλ“œν°λ²ˆν˜Έμ˜ getter
setPhoneNum() { ... }  // ν•Έλ“œν°λ²ˆν˜Έμ˜ setter
getAge() { ... }  // λ‚˜μ΄μ˜ getter
setAge() { ... }  // λ‚˜μ΄μ˜ setter
toString() { ... }  // Main Methodμ—μ„œ λ°°μ—΄ 좜λ ₯ μ‹œ λ‚˜μ˜¬ 16μ§„μˆ˜ μ£Όμ†Œκ°’ λŒ€μ‹  μ›ν•˜λŠ” 좜λ ₯κ°’ 좜λ ₯을 μœ„ν•΄ Overriding


Guest.java private int money;   // μ§€λΆˆκΈˆμ•‘
private int point;  // 포인트 적립금
private String order;  // μ£Όλ¬Έ λ‚΄μš©

### Method ###

order() {...}  // μ£Όλ¬Έν•˜λŠ” 행동
takeMoney() {...}  // 돈 μ§€λΆˆ 행동
bounusPoint() {...}  // 포인트 적립 행동


Staff.java private String takeOrder;   // μ£Όλ¬Έλ°›μ€λ‚΄μš©
private String coffeeMachine;   // μ»€ν”Όλ§Œλ“œλŠ” 행동
private String pass;   // 컀피 κ±΄λ„€λŠ” 행동
private int money;  // μˆ˜μž…κΈˆ
private int point; // μ§€λΆˆ 포인트

### Method ###

order() {...}  // μ£Όλ¬Έ 받을 행동
work() {...}  // 컀피 제쑰 행동
pointSave() {...}  // 포인트 적립 행동
passCoffee() {...}  // μ†λ‹˜μ—κ²Œ 컀피λ₯Ό κ±΄λ„€λŠ” 행동 

 

 

✏️ Cafe.java (Main Method)

package homework_210316;

public class Cafe {

	public static void main(String[] args) {
		People[] saram = {
							new Staff("ν•œμ§€μ€", "010-123-2345", 34, "μ–΄λ–€ κ±° λ“œλ¦΄κΉŒμš”?", "...μ•„μ΄μŠ€μ•„λ©”λ¦¬μΉ΄λ…Έ λ§Œλ“œλŠ” 쀑...", "μ•„μ΄μŠ€μ•„λ©”λ¦¬μΉ΄λ…Έ", 2000, 200),
							new Guest("홍세쀀", "010-345-2345", 32, "μ•„μ΄μŠ€μ•„λ©”λ¦¬μΉ΄λ…Έ μƒ· μΆ”κ°€", 2000, 200)
						};
		
		for ( int i = 0; i < saram.length; ++i ) {
			System.out.println("======================");
			System.out.println(saram[i]);
			
			if (saram[i] instanceof Staff) {
				Staff tmp = (Staff)saram[i];
				tmp.order();
				
			} else if (saram[i] instanceof Guest) {
				Guest tmp = (Guest)saram[i];
				tmp.order();

			}
		}
			

		
		for( int j = 0; j < saram.length; ++j ) {
			System.out.println("======================");
			System.out.println(saram[j]);
			if (saram[j] instanceof Staff) {
				Staff tmp = (Staff)saram[j];
				tmp.work();
				
			} else if (saram[j] instanceof Guest) {
				Guest tmp1 = (Guest)saram[j];
				tmp1.takeMoney();
			}

		}
		
		for( int k = 0; k < saram.length; ++k ) {
			System.out.println("======================");
			System.out.println(saram[k]);
			if (saram[k] instanceof Staff) {
				Staff tmp = (Staff)saram[k];
				tmp.pointSave();
				tmp.passCoffee();
				
			} else if (saram[k] instanceof Guest) {
				Guest tmp1 = (Guest)saram[k];
				tmp1.bonusPoint();
			}

		}

		
		
		
		} // main 끝
} // Class 끝



 

✏️ People.java

package homework_210316;

public class People {
	
	private String name;
	private String phoneNum;
	private int age;
	
	
		public People(String name, String phoneNum, int age) {
			this.name = name;
			this.phoneNum = phoneNum;
			this.age = age;
		}
	
	
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPhoneNum() {
			return phoneNum;
		}
		public void setPhoneNum(String phoneNum) {
			this.phoneNum = phoneNum;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}

		@Override
		public String toString() {
			String tmp = super.toString();
			return "이름 : " + this.name + "\n" + "ν•Έλ“œν°λ²ˆν˜Έ : " + this.phoneNum  + "\n" + "λ‚˜μ΄ :" + this.age + "\n";
		
		}	
        
} // Class 끝

 

 

 

✏️ Staff.java

package homework_210316;

public class Staff extends People {
	
	private String takeOrder;
	private String coffeeMachine;
	private String pass;
	private int money;
	private int point;
	
	public Staff(String name, String phoneNum, int age, String takeOrder, String coffeeMachine, String pass, int money, int point) {
		super(name, phoneNum, age);
		this.takeOrder = takeOrder;
		this.coffeeMachine = coffeeMachine;
		this.pass = pass;
		this.money = money;
		this.point = point;
		
	}
	
	public void order() {
		System.out.println(this.takeOrder);
	}
	
	public void work() {
		System.out.println(this.money + "원 μž…λ‹ˆλ‹€!");
		System.out.println(this.coffeeMachine);
	}
	
	public void pointSave() {
		System.out.println("point " + this.point + " 점 적립 λ˜μ…¨μŠ΅λ‹ˆλ‹€!");
	}
	
	public void passCoffee() {
		System.out.println("μ£Όλ¬Έν•˜μ‹  " + this.pass + " λ‚˜μ™”μŠ΅λ‹ˆλ‹€!");
	}

	


} // Class 끝

 

 

✏️ Guest.java

package homework_210316;

public class Guest extends People {
	
	private int money;
	private int point;
	private String order;
	
	public Guest(String name, String phoneNum, int age, String order, int money, int point) {
		super(name, phoneNum, age);
		this.order = order;
		this.money = money;
		this.point = point;
	}
	
	public void order() {
		System.out.println(this.order + "ν•œμž” 뢀탁 λ“œλ¦½λ‹ˆλ‹€!");
	}
	
	public void takeMoney() {
		System.out.println(this.money + "원 μ—¬κΉ„μŠ΅λ‹ˆλ‹€!");
	}
	
	public void bonusPoint() {
		System.out.println(this.point + "포인트 μ˜¬λžλ„€!");
	}

	

} // Class 끝
728x90
λ°˜μ‘ν˜•