자바 - 상속구조 이해를 위한 실습

지환·2023년 10월 7일
0

JAVA

목록 보기
21/39

출처 | https://www.youtube.com/watch?v=2yHph3E4CCs&list=PLOSNUO27qFbtjCw-YHcmtfZAkE79HZSOO&index=25

Step1

Ellipse

package com.GraphicEditor;

public class Ellipse {
	public void drawEllipse()
	{
		System.out.println("Drawing : Ellipse");
	}

}

GraphicEditor

package com.GraphicEditor;

public class GraphicEditor {
	private static final int ARRAY_LENGTH = 5;
	public static void main(String[] args) {
		Rectangle[] rc = new Rectangle[ARRAY_LENGTH];
		Ellipse[] ep = new Ellipse[ARRAY_LENGTH];
		Line[] li = new Line[ARRAY_LENGTH];
		
		for(int i = 0; i<ARRAY_LENGTH; i++)
		{
			int randomNumber = (int)(Math.random() * 3);
			if( randomNumber == 0)
			{
				rc[i] = new Rectangle();
			}
			else if(randomNumber == 1)
			{
				ep[i] = new Ellipse();
			}
			
			else
			{
				li[i] = new Line();
			}
		}
		
		for(int j = 0; j<ARRAY_LENGTH; j++)
		{
			if(rc[j] != null)
			{
				rc[j].drawRectangle();
			}
			
			else if(ep[j] != null )
			{
				ep[j].drawEllipse();
			}
			else
			{
				li[j].drawLine();
				
			}
		}
		
	}

}

Line

package com.GraphicEditor;

public class Line {
	public void drawLine()
	{
		System.out.println("Drawing  : Line"); 
	}

}

Rectangle

package com.GraphicEditor;

public class Rectangle {
	public void drawRectangle()
	{
		System.out.println("Drawing : Rectangle");
	}

}

Step2

Ellipse

package com.GraphicEditor;

public class Ellipse {
	public void drawEllipse()
	{
		System.out.println("Drawing : Ellipse");
	}

}

GraphicEditor

package com.GraphicEditor.step2;

public class GraphicEditor {
	private static final int ARRAY_LENGTH = 5;
	public static void main(String[] args) {
		Shape[] shapes = new Shape[ARRAY_LENGTH];
		/*
		 * 다른 객체들을 담는 배열 선언 + 상속 관계시 Up-Casting이 된다
		 * 
		 */
		
		for(int i = 0; i<ARRAY_LENGTH; i++)
		{
			int randomNumber = (int)(Math.random() * 3);
			if( randomNumber == 0)
			{
				shapes[i] = new Rectangle();
			}
			else if(randomNumber == 1)
			{
				shapes[i] = new Ellipse();
			}
			
			else
			{
				shapes[i] = new Line();
			}
		}
		
		
//		for(int j = 0; j <ARRAY_LENGTH; j++)
//		{
//			
//			if(shapes[i] instanceof Rectangle)
//		}
//		

		/*
		 * shapes에는 Rectangle + Line + Ellipse 도 들어있다.
		 */
		for(Shape shape : shapes)
		{
			if(shape instanceof Rectangle)
				// 현재 shape이 참조하는 이 대상이 Rectangle 객체로 다운캐스팅 되는지 물어보는거
				// Rectangle를 참조하면 instanceof를 통해서 True를 발생시키고 그렇게 되면 downcasting이 가능하다라는얘기
			{
				/*
				 * Rectangle rc	= (Rectangle) shape;
				 * rc.draw(); --> 한줄로 바꿈 
				 */
				((Rectangle)shape).drawRectangle();
			}
			else if(shape instanceof Ellipse)
			{
				((Ellipse)shape).drawEllipse();
			}
			else if(shape instanceof Line)
			{
//				Line li = (Line)shape;
//				li.draw();
				((Line)shape).drawLine();
			}
			
		}
		
	}

}

-----------------------------------------------
Drawing  : Line
Drawing  : Line
Drawing : Rectangle
Drawing  : Line
Drawing : Rectangle

Line

package com.GraphicEditor;

public class Line {
	public void drawLine()
	{
		System.out.println("Drawing  : Line"); 
	}

}

Rectangle

package com.GraphicEditor;

public class Rectangle {
	public void drawRectangle()
	{
		System.out.println("Drawing : Rectangle");
	}

}

Shape

package com.GraphicEditor.step3;

public class Shape {
	public void draw()
	{
		System.out.println("Drawing : Shape");
	}

}

Step3

Line - Rectangle - Ellipse : draw 오버라이딩 한다.

다형성의 3가지 요소

  1. 메소드 재정의(오버라이딩)
  2. 상속
  3. 객체간의 형변환

Ellipse


package com.GraphicEditor.step3;

public class Ellipse extends Shape{
	@Override
	public void draw()
	{
		System.out.println("Drawing : Ellipse");
	}

}

GraphicEditor

package com.GraphicEditor.step3;

public class GraphicEditor {
	private static final int ARRAY_LENGTH = 5;
	public static void main(String[] args) {
		Shape[] shapes = new Shape[ARRAY_LENGTH];
		/*
		 * 다른 객체들을 담는 배열 선언 + 상속 관계시 Up-Casting이 된다
		 * 
		 */
		
		for(int i = 0; i<ARRAY_LENGTH; i++)
		{
			int randomNumber = (int)(Math.random() * 3);
			if( randomNumber == 0)
			{
				shapes[i] = new Rectangle();
			}
			else if(randomNumber == 1)
			{
				shapes[i] = new Ellipse();
			}
			
			else
			{
				shapes[i] = new Line();
			}
		}
		
		
//		for(int j = 0; j <ARRAY_LENGTH; j++)
//		{
//			
//			if(shapes[i] instanceof Rectangle)
//		}
//		

		/*
		 * shapes에는 Rectangle + Line + Ellipse 도 들어있다.
		 */
		for(Shape shape : shapes)
		{
			shape.draw();
			/*
			 * 다형성이 구현된 부분 
			 */
			
		}
		
	}

}

Line

package com.GraphicEditor.step3;

public class Line extends Shape{
	@Override
	public void draw()
	{
//		super.draw();
//		
//		int id = 10;
//		System.out.println(id);
		System.out.println("Draw : Line");
	}

}

Rectangle

package com.GraphicEditor.step3;

public class Rectangle extends Shape{
	@Override
	public void draw()
	{
		System.out.println("Drawing : Rectangle");
	}

}

Shape

package com.GraphicEditor.step3;

public class Shape {
	public void draw()
	{
		System.out.println("Drawing : Shape");
	}

}
profile
아는만큼보인다.

0개의 댓글