다형성
Java 프로그래밍 - 다형성
class Person {
public void print() {
System.out.println("Person.print");
}
}
class Student extends Person {
public void print() {
System.out.println("Student.print");
}
public void print2() {
System.out.println("Student.print2");
}
}
class CollegeStudent extends Person {
public void print() {
System.out.println("CollegeStudent.print");
}
}
public class Main {
public static void main(String[] args) {
// 1. 다형성
System.out.println("== 다형성 ==");
Person p1 = new Person();
Student s1 = new Student();
Person p2 = new Student();
// Student s2 = new Person();
p1.print();
s1.print();
s1.print2();
p2.print();
// p2.print2();
Person p3 = new CollegeStudent();
// CollegeStudent c1 = new Student();
p3.print();
// 2. 타입 변환
System.out.println("== 타입 변환 ==");
Person pp1 = null;
Student ss1 = null;
Person pp2 = new Person();
Student ss2 = new Student();
Person pp3 = new Student(); // 업캐스팅
pp1 = pp2;
pp1 = ss2;
ss1 = ss2;
// ss1 = (Student)pp2;
ss1 = (Student)pp3; // 다운캐스팅
// CollegeStudent cc1;
// CollegeStudent cc2 = new CollegeStudent();
// ss1 = (Student) cc2;
// cc1 = (CollegeStudent) ss2;
// 3. instanceof
System.out.println("== instanceof ==");
Person pe1 = new Person();
Student st1 = new Student();
Person pe2 = new Student();
Person pe3 = new CollegeStudent();
System.out.println("== instance of ==");
System.out.println(pe1 instanceof Person);
System.out.println(pe1 instanceof Student);
System.out.println(st1 instanceof Student);
System.out.println(st1 instanceof Person);
System.out.println(pe2 instanceof Person);
System.out.println(pe2 instanceof Student);
System.out.println(pe3 instanceof Person);
System.out.println(pe3 instanceof CollegeStudent);
if (pe1 instanceof Student) {
Student stu1 = (Student) pe1;
}
if (st1 instanceof Person) {
Person per1 = (Person)st1;
}
}
}
== 다형성 ==
Person.print
Student.print
Student.print2
Student.print
CollegeStudent.print
== 타입 변환 ==
== instanceof ==
== instance of ==
true
false
true
true
true
true
true
true
// Practice
// 아래의 클래스와 상속 관계에서 다형성을 이용하여
// Car 객체를 통해 아래와 같이 출력될 수 있도록 Test code 부분을 구현해보세요.
// 빵빵!
// 위이잉!
// 삐뽀삐뽀!
class Car {
Car(){}
public void horn() {
System.out.println("빵빵!");
}
}
class FireTruck extends Car {
public void horn() {
System.out.println("위이잉!");
}
}
class Ambulance extends Car {
public void horn() {
System.out.println("삐뽀삐뽀!");
}
}
public class Practice {
public static void main(String[] args) {
// Test code
Car car = new Car();
car.horn();
car = new FireTruck();
car.horn();
car = new Ambulance();
car.horn();
Car car2[] = {new Car(), new FireTruck(), new Ambulance()};
for (Car item: car2) {
item.horn();
}
}
}
빵빵!
위이잉!
삐뽀삐뽀!
빵빵!
위이잉!
삐뽀삐뽀!
Leave a comment