多形性Overrideの継承
public class People{
public void printInfo() {
System.out.println("나는 사람입니다.");
}
}
public class Man extends People{
@Override
public void printInfo() {
super.printInfo();
System.out.println("그리고 전 남자입니다.");
}
}
public class Woman extends People{
@Override
public void printInfo() {
super.printInfo();
System.out.println("그리고 전 여자입니다.");
}
}
public class Seokhwi extends Man{
@Override
public void printInfo() {
super.printInfo();
System.out.println("그리고 전 seokhwi입니다");
}
}
public class Test {
public static void main(String[] args) {
People people1=new People();
people1.printInfo();
People people2 = new Man();
people2.printInfo();
People people3 = new Man();
people3.printInfo();
BaeSeokHwi Seokhwi = new BaeSeokHwi();
baeSeokHwi.printInfo();
People[] arr = {people1,people2,people3,baeSeokHwi};
for (People people : arr) {
System.out.println("-----------------------------------------------");
if(people instanceof People){
System.out.println("is People Type");
}
if(people instanceof Man){
System.out.println("is Man Type");
}
if(people instanceof Woman){
System.out.println("is Woman Type");
}
if(people instanceof Seokhwi){
System.out.println("is Seokhwi Type");
}
}
}
}
結果
나는 사람입니다.
나는 사람입니다.
그리고 전 남자입니다.
나는 사람입니다.
그리고 전 남자입니다.
나는 사람입니다.
그리고 전 남자입니다.
그리고 전 배석휘입니다
-----------------------------------------------
is People Type
-----------------------------------------------
is People Type
is Man Type
-----------------------------------------------
is People Type
is Man Type
-----------------------------------------------
is People Type
is Man Type
is BaeSeokHwi Type
Reference
この問題について(多形性Overrideの継承), 我々は、より多くの情報をここで見つけました https://velog.io/@seokhwi/상속-다형성-Overrideテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol