βΆ λ€μ΄ μΊμ€ν κ³Ό 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);
}
}
β-μ¬μ€ λ¬Έμ μ μλλ₯Ό μ λͺ¨λ₯΄κ² λ€. 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 |
λκΈ