λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ—‚.λŒ€μ™Έν™œλ™/πŸ“.μΉ΄μΉ΄μ˜€ν΄λΌμš°λ“œμŠ€μΏ¨

[JAVA] μžλ°” μΈν„°νŽ˜μ΄μŠ€

by πŸ’Ύκ³ κ΅¬λ§ˆλ§›νƒ•λ¨Ήκ³ μ‹Άλ‹€ 2023. 12. 18.
728x90

🧸 μΈν„°νŽ˜μ΄μŠ€

닀쀑 상속은 두 κ°œμ΄μƒμ˜ ν΄λž˜μŠ€λ‘œλΆ€ν„° μƒμ†λ°›λŠ” 것을 λ§ν•œλ‹€. μžλ°”λŠ” μ›μΉ™μ μœΌλ‘œ 쀑 상속을 μ§€μ›ν•˜μ§€ μ•ŠλŠ”λ‹€. ν•˜μ§€λ§Œ 닀쀑 상속을 μœ„ν•΄ μΈν„°νŽ˜μ΄μŠ€ 클래슀λ₯Ό μ œκ³΅ν•˜κ²Œ λ˜μ—ˆλ‹€.

 

μΈν„°νŽ˜μ΄μŠ€ ν΄λž˜μŠ€λŠ” λ³€μˆ˜λ₯Ό μ§€μ›ν•˜μ§€ μ•ŠμœΌλ‚˜(λͺ…μΉ­ 좩돌 방지) μƒμˆ˜λŠ” μ„ μ–Έ κ°€λŠ₯(public static final)ν•˜λ‹€. 그리고 좔상 λ©”μ†Œλ“œ(μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ν•˜λ©΄ 좩돌 방지가 되기 λ•Œλ¬Έμ—)만 μ„ μ–Έ κ°€λŠ₯ν•˜λ‹€.

 

즉, μ½”λ“œμ˜ μž¬μ •μ˜λ₯Ό μœ„ν•¨μ΄ μ•„λ‹ˆλΌ μ œμ–΄λ§Œμ„ μœ„ν•œ ν΄λž˜μŠ€μ΄λ‹€.

 

πŸ—„ 클래슀의 μ’…λ₯˜

더보기
  1.  μΌλ°˜ 클래슀 = κ΅¬ν˜„ 클래슀
    1. μΈμŠ€ν„΄μŠ€ 생성 κ°€λŠ₯ (new μ—°μ‚°μž μ‚¬μš© κ°€λŠ₯)
  2. 좔상 클래슀
    1. 일반 λ©”μ†Œλ“œ + 좔상 λ©”μ†Œλ“œ
    2. ν•˜λ‚˜ μ΄μƒμ˜ μΆ”상 λ©”μ†Œλ“œλ₯Ό κ°€μ§€κ³  μžˆλŠ” ν΄λž˜μŠ€
    3. μΈμŠ€ν„΄μŠ€ μƒμ„± λΆˆκ°€λŠ₯
  3. μΈν„°νŽ˜μ΄μŠ€ 클래슀
    1. μƒμˆ˜μ™€ 좔상 λ©”μ†Œλ“œ
    2. μΈμŠ€ν„΄μŠ€ μƒμ„± λΆˆκ°€λŠ₯

2, 3λ²ˆμ„ μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” 상속 ꡬ쑰λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

 

🧩  μ†ŒμŠ€ μ½”λ“œ

더보기

🧩 슈퍼클래슀

package mycom.duck.dfinal;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import mycom.duck.frame.MyFrame;

public abstract class Duck {
    protected int x;
    protected int y;
    public static final int DUCK_SIZE=30;

    public Duck(){
    	Random random = new Random();
    	this.x = random.nextInt(MyFrame.FRAME_WIDTH-100)+60;
    	this.y = random.nextInt(MyFrame.FRAME_HEIGHT-150)+80;
    }

    public Duck(int mx, int my){
    	this.x = mx;
    	this.y = my;
    }

    public abstract void display(Graphics g);
}

🧩  μ„œλΈŒν΄λž˜μŠ€

package mycom.duck.dfinal;

import java.awt.Color;
import java.awt.Graphics;

public class RedDuck extends Duck implements IFlyable {

    public RedDuck(){
    }

    public RedDuck(int mx, int my){
    	super(mx, my);
    }

    @Override
    public void display(Graphics g){
    	g.setColor(Color.RED);
    	g.fillOval(x, y, Duck.DUCK_SIZE, Duck.DUCK_SIZE);
    }

    @Override
    public void fly(Graphics g){
    	g.setColor(Color.RED);
    	g.drawString("λ‚ λ‹€", x-15, y+40);
    }

}​

🧩  μΈν„°νŽ˜μ΄μŠ€ 클래슀

package mycom.duck.dfinal;

import java.awt.Graphics;

public interface IFlyable {

    public abstract void fly(Graphics g);

}

 

 

🧸 상속 ꡬ쑰λ₯Ό μ΄μš©ν•˜μ—¬ μ œμ–΄ν•˜λŠ” 방법

displayAllDucks λ©”μ†Œλ“œλŠ” μΌλ°˜ μƒμ†μ„ μ΄μš©ν•˜μ—¬ μ œμ–΄ν•œ κ²ƒμ΄λ‹€. μ—…μΊμŠ€νŒ…μ„ ν•˜λ”라도 μ˜€λ²„λ‘œλ”©ν•œ λ©”μ†Œλ“œλŠ” μ„œλΈŒν΄λž˜μŠ€μ˜ κ²ƒμ΄ ν˜ΈμΆœλ˜κΈ° λ•Œλ¬Έμ΄λ‹€.

 

flyAllDucks λ©”μ†Œλ“œλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•˜μ—¬ μ œμ–΄ν•œ 것이닀. μΈν„°νŽ˜μ΄μŠ€ 클래슀둜 ν˜• λ³€ν™˜μ„ ν•˜λ©΄ ν•΄λ‹Ή μΈν„°νŽ˜μ΄μŠ€μ— μžˆλŠ” κ²ƒλ§Œ μ‚¬μš©κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ—(μ—¬κΈ°μ„œλŠ” fly λ©”μ†Œλ“œλ§Œ μ‚¬μš©κ°€λŠ₯) duck 쀑에 fly λ©”μ†Œλ“œλ₯Ό μˆ˜ν–‰ν•  수 μžˆλŠ” 객체만 μˆ˜ν–‰λ˜κ²Œ λœλ‹€. 🚨 μ—¬κΈ°μ„œ if문으둜 ν˜• λ³€ν™˜μ΄ κ°€λŠ₯ν•œ 지 ν™•μΈν•˜κ³  μˆ˜ν–‰ν•΄μ•Όν•œλ‹€. 

 

package mycom.duck.dfinal;

import java.awt.Graphics;
import java.util.Random;

public class DuckManager {
	private Duck[] ducks = new Duck[20];
	
	public DuckManager() {
		makeDucks();
	}
	
	private void makeDucks(){
    	Random random = new Random();
		int type = 0;
		if (ducks != null) {
			for (int i=0; i<ducks.length; i++) {
				type = random.nextInt(2);
				switch (type) {
					case 0:
						ducks[i] = new RedDuck();
						break;
					case 1:
						ducks[i] = new MallardDuck();
						break;
					default:
						ducks[i] = new MallardDuck();
				}
			}
		}
    }

    public void displayAllDucks(Graphics g){
    	for (Duck duck: ducks) {
    		duck.display(g);
    	}
    }
    
    public void flyAllDucks(Graphics g){
    	for (Duck duck: ducks) {
    		if (duck instanceof IFlyable) {
    		// 1번 방법
                IFlyable duckfFlyable = (IFlyable) duck;
    		duckfFlyable.fly(g);
            
                // 2번 방법
                ((IFlyable) duck).fly(g);
    		}
    	}
    }
}
728x90