λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Java

[Java] λ‹€μš΄ μΊμŠ€νŒ…κ³Ό instanceof

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 20.
λ°˜μ‘ν˜•

β–Ά λ‹€μš΄ μΊμŠ€νŒ…κ³Ό instanceof

ν•„μš”μ— 따라 λ‹€μ‹œ μ›λž˜ μΈμŠ€ν„΄μŠ€μ˜ μžλ£Œν˜•μœΌλ‘œ λ˜λŒμ•„κ°€μ•Όν•˜λŠ” κ²½μš°κ°€ μžˆλ‹€. μ΄λ ‡κ²Œ μƒμœ„ 클래슀둜 ν˜• λ³€ν™˜λ˜μ—ˆλ˜ ν•˜μœ„ 클래슀λ₯Ό λ‹€μ‹œ μ›λž˜ μžλ£Œν˜•μœΌλ‘œ ν˜• λ³€ν™˜ν•˜λŠ” 것을 λ‹€μš΄ μΊμŠ€νŒ…μ΄λΌ ν•œλ‹€.

​

λ‹€μš΄ μΊμŠ€νŒ…μ„ ν•˜κΈ° 전에 μƒμœ„ 클래슀둜 ν˜• λ³€ν™˜λœ μΈμŠ€ν„΄μŠ€μ˜ μ›λž˜ μžλ£Œν˜•μ„ 확인해야 λ³€ν™˜ν•  λ•Œ 였λ₯˜λ₯Ό 막을 수 μžˆλ‹€. 이λ₯Ό ν™•μΈν•˜λŠ” μ˜ˆμ•½μ–΄κ°€ instanceof 이닀.

package polymorphism;

import java.util.ArrayList;

class Animal { // μƒμœ„ 클래슀 Animal
	public void move() {
		System.out.println("동물이 μ›€μ§μž…λ‹ˆλ‹€.");
	}
}

class Human extends Animal { // Animal을 상속받은 Human 클래슀
	public void move() {
		System.out.println("μ‚¬λžŒμ΄ 두 발둜 κ±·μŠ΅λ‹ˆλ‹€. ");
	}

	public void readBook() {
		System.out.println("μ‚¬λžŒμ΄ 책을 μ½μŠ΅λ‹ˆλ‹€. ");
	}
}

class Tiger extends Animal { // Animal을 상속받은 Tiger 클래슀
	public void move() {
		System.out.println("ν˜Έλž‘μ΄κ°€ λ„€ 발둜 λœλ‹ˆλ‹€. ");
	}

	public void hunting() {
		System.out.println("ν˜Έλž‘μ΄κ°€ 사λƒ₯을 ν•©λ‹ˆλ‹€. ");
	}
}

class Eagle extends Animal { // Animal을 상속받은 Eagle 클래슀
	public void move() {
		System.out.println("λ…μˆ˜λ¦¬κ°€ ν•˜λŠ˜μ„ λ‚©λ‹ˆλ‹€ ");
	}

	public void flying() {
		System.out.print("λ…μˆ˜λ¦¬κ°€ λ‚ κ°œλ₯Ό μ­‰ 펴고 멀리 λ‚ μ•„κ°‘λ‹ˆλ‹€");
	}
}

public class AnimalTest {
	ArrayList<Animal> aniList = new ArrayList<Animal>();

	public static void main(String[] args) {
		AnimalTest1 aTest = new AnimalTest1();
		aTest.addAnimal();
		System.out.println("μ›λž˜ ν˜•μœΌλ‘œ λ‹€μš΄ μΊμŠ€νŒ…");
		aTest.testCasting();
	}

	public void addAnimal() {
		aniList.add(new Human()); // ArrayList에 μΆ”κ°€λ˜λ©΄μ„œ Animalν˜•μœΌλ‘œ ν˜• λ³€ν™˜
		aniList.add(new Tiger());
		aniList.add(new Eagle());

		for (Animal ani : aniList) { // λ°°μ—΄ μš”μ†Œλ₯Ό Animalν˜•μœΌλ‘œ κΊΌλ‚΄μ„œ move()λ₯Ό ν˜ΈμΆœν•˜λ©΄ μž¬μ •μ˜λœ ν•¨μˆ˜κ°€ 호좜됨
			ani.move();
		}
	}

	public void testCasting() {
        for (int i = 0; i < aniList.size(); i++) {
           Animal ani = aniList.get(i);
           if (ani instanceof Human) {
               Human h = (Human) ani;
               h.readBook();
           } else if (ani instanceof Tiger) {
               Tiger t = (Tiger) ani;
               t.hunting();
           } else if (ani instanceof Eagle) {
               Eagle e = (Eagle) ani;
               e.flying();
           } else {
               System.out.println("μ§€μ›λ˜μ§€ μ•ŠλŠ” ν˜•μž…λ‹ˆλ‹€.")
           }
       }
   }
}

 

 

 

μ—°μŠ΅λ¬Έμ œ Q4) λ‹€μŒ μ½”λ“œλŠ” 였λ₯˜κ°€ λ°œμƒν•œλ‹€. 였λ₯˜κ°€ λ°œμƒν•˜λŠ” 원인을 μ„€λͺ…ν•˜μ‹œμ˜€.

package Test;

public class Employee {
	public String name;
	public String grade;
	
	public Employee(String name) {
		this.name = name;
	}
}

 

package Test;

public class Engineer extends Employee {
	private String skillset;
	
	public String getSkillSet() {
		return skillset;
	}
	
	public void setSkillSet(String skillset) {
		this.skillset = skillset;
	}
}

 

A)

더보기
package Test;

public class Employee {
	public String name;
	public String grade;

    public Employee() {}
	
	public Employee(String name) {
		this.name = name;
	}
}

λ˜λŠ”

package Test;

public class Engineer extends Employee {
    public Engineer(String name) {
        super(name);
    }

	private String skillset;
	
	public String getSkillSet() {
		return skillset;
	}
	
	public void setSkillSet(String skillset) {
		this.skillset = skillset;
	}
}

 

 

 

μ—°μŠ΅λ¬Έμ œ Q5) λ‹€μŒ 좜λ ₯ κ²°κ³Όκ°€ λ‚˜μ˜€λ„λ‘ λΉˆμΉΈμ„ μ±„μš°μ‹œμ˜€.

κ²°κ³Όκ°’

 

package Test;

import java.util.ArrayList;

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

class Circle extends Shape {
	public void draw() {
		System.out.println("Circle");
	}
}

class Triangle extends Shape {
	public void draw() {
		System.out.println("Triangle");
	}
}

public class ShapeTest {
	public static void main(String[] args) {

	    β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘

		list.add(new Circle());
		list.add(new Triangle());
		list.add(new Rectangle());

		for (Shape s : list) {
			β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘
		}

	}
}

 

A)

더보기
package Test;

import java.util.ArrayList;

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

class Circle extends Shape {
	public void draw() {
		System.out.println("Circle");
	}
}

class Triangle extends Shape {
	public void draw() {
		System.out.println("Triangle");
	}
}

public class ShapeTest {
	public static void main(String[] args) {

	    ArrayList<Shape> list = new ArrayList<Shape>();

		list.add(new Circle());
		list.add(new Triangle());
		list.add(new Rectangle());

		for (Shape s : list) {
			s.draw();
		}

	}
}

 

 

​

μ‹¬ν™”λ¬Έμ œ ) μ‚Όκ°ν˜•, μ‚¬κ°ν˜•, μ›μ˜ 넓이, 높이, 면적을 각각 κ΅¬ν•˜μ‹œμ˜€.

κ²°κ³Όκ°’

 

A)

더보기
package example;

public class Shape {
	protected int x;
	protected int y;
	protected int height;
	protected int width;
	public static final double r = 3.14159;

	public Shape() {
	}

	public Shape(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public double getArea() {
		return height * width;
	}

	public void showInfo() {
		System.out.println("면적 : " + height * width);
	}
}

 

Circle 클래슀

 

Triangle 클래슀

 

Rectangle 클래슀

 

λ‚΄κ°€ λ§Œλ“  ShapeTest 클래슀

 

μ„ μƒλ‹˜μ΄ λ§Œλ“  ShapeTest 클래슀

​-사싀 문제의 μ˜λ„λ₯Ό 잘 λͺ¨λ₯΄κ² λ‹€. protected λ³€μˆ˜μ™€ 상속을 μ‚¬μš©ν•˜λΌλŠ” 문제면 맞게 ν‘Όκ±°κ°™κ³ ....

-x와 yλŠ” λ‚΄κ°€ μž„μ˜λ‘œ 값을 넣어도 λ˜λŠ”κ±΄κ°€? 일단은 좜λ ₯에선 뺐음

-μˆ˜ν•™ 곡뢀λ₯Ό ν•˜μž..

 

 

 

μ‹¬ν™”λ¬Έμ œ) 여기에 Cylinder 클래슀λ₯Ό μΆ”κ°€ν•΄μ„œ μ›μ˜ λΆ€ν”Όλ₯Ό κ΅¬ν•˜μ‹œμ˜€.

쑰건. Circle 클래슀λ₯Ό 상속 받을 것

​

<μ‹€ν–‰ κ²°κ³Ό>

 

λ°˜μ‘ν˜•

'Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Java] 좔상 클래슀  (0) 2021.02.20
[Java] μ‹€μŠ΅  (0) 2021.02.20
[Java] λ‹€ν˜•μ„± ν™œμš©ν•˜κΈ°  (0) 2021.02.20
[Java] λ‹€ν˜•μ„±  (0) 2021.02.19
[Java] 가상 λ©”μ„œλ“œ  (0) 2021.02.19

λŒ“κΈ€