Ex01_polymorphism
package com.mywork.ex;
class Product {
public void info() {
System.out.println("Product");
}
}
class Computer extends Product {
@Override
public void info() {
System.out.println("Computer");
}
}
class Notebook extends Computer {
@Override
public void info() {
System.out.println("Notebook");
}
}
public class Ex01_polymorphism {
public static void main(String[] args) {
Computer[] computers = new Computer[10];
Notebook[] notebooks = new Notebook[10];
for (int i = 0; i < computers.length; i++) {
computers[i] = new Computer();
}
for (int i = 0; i < notebooks.length; i++) {
notebooks[i] = new Notebook();
}
Product[] products = new Product[20];
products[0] = new Computer();
products[1] = new Computer();
products[2] = new Notebook();
products[0].info();
products[1].info();
products[2].info();
}
}
Ex02_polymorphism
package com.mywork.ex;
class Shape {
public double calcArea() {
return 0;
}
}
class Rect extends Shape {
private int width;
private int height;
public Rect(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public double calcArea() {
return width * height;
}
}
class Circle extends Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calcArea() {
return Math.PI * Math.pow(radius, 2);
}
}
class Triangle extends Shape {
private int width;
private int height;
public Triangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public double calcArea() {
return (width * height) / 2.0;
}
}
public class Ex02_polymorphism {
public static void main(String[] args) {
Shape[] arr = new Shape[3];
arr[0] = new Rect(2, 3);
arr[1] = new Triangle(2, 3);
arr[2] = new Circle(1);
for (int i = 0; i < arr.length; i++) {
System.out.println("크기 : " + arr[i].calcArea());
}
}
}
Ex03_polymorphism
package com.mywork.ex;
class Animal {
public void move() {
}
}
class Dog extends Animal {
@Override
public void move() {
System.out.println("개가 달린다.");
}
}
class Dolphin extends Animal {
@Override
public void move() {
System.out.println("돌고래는 헤엄친다.");
}
}
class Eagle extends Animal {
@Override
public void move() {
System.out.println("독수리는 난다.");
}
public void fly() {
}
}
public class Ex03_polymorphism {
public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Dolphin();
animals[2] = new Eagle();
for (int i = 0; i < animals.length; i++) {
animals[i].move();
}
if (animals[2] instanceof Eagle) {
Eagle eagle = (Eagle) animals[2];
eagle.fly();
}
((Eagle) animals[2]).fly();
}
}
Ex04_polymorphism
package com.mywork.ex;
class Person {
public void eat(String food) {
System.out.println(food + " 먹는다.");
}
public void sleep() {
System.out.println("잔다.");
}
}
class Student extends Person {
public void study() {
System.out.println("공부한다.");
}
}
class Worker extends Person {
public void work() {
System.out.println("일한다.");
}
}
public class Ex04_polymorphism {
public static void main(String[] args) {
Person person1 = new Student();
person1.eat("밥");
person1.sleep();
if (person1 instanceof Student) {
Student student = (Student)person1;
student.eat("밥");
student.sleep();
student.study();
}
Person person2 = new Worker();
if (person2 instanceof Worker) {
Worker worker = (Worker)person2;
worker.work();
}
Person person3 = new Student();
((Student)person3).study();
}
}
Ex05_polymorphism
package com.mywork.ex;
class Car {
public void drive() {
}
}
class Ev extends Car {
public void charging() {
}
}
class Hybrid extends Ev {
public void addOil() {
}
}
public class Ex05_polymorphism {
public static void main(String[] args) {
Car[] motors = new Car[2];
motors[0] = new Ev();
motors[0].drive();
((Ev)motors[0]).charging();
motors[1] = new Hybrid();
motors[1].drive();
((Hybrid)motors[1]).charging();
((Hybrid)motors[1]).addOil();
((Hybrid)motors[0]).drive();
((Hybrid)motors[0]).charging();
((Hybrid)motors[0]).addOil();
if (motors[0] instanceof Hybrid) {
((Hybrid)motors[0]).drive();
((Hybrid)motors[0]).charging();
((Hybrid)motors[0]).addOil();
}
}
}