Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- React
- 오버라이드
- FIle
- degit
- parcel
- webpack
- java#왕초보
- 시큐어코딩
- 코틀린
- 안드로이드
- 미니게임
- 숫자
- snowpack
- git
- 버전일치
- 게시판
- 답글
- Spinner
- 스타일보험
- Android
- SQL
- sub query
- 함수
- 왕초보
- 스프링
- kotlin
- 상속
- Spring
- java
- 쿠키
Archives
- Today
- Total
YSHUSH
Over ride3 본문
MainClass
public class MainClass {
public static void main(String[] args) {
// 자식 클래스의 객체를 총 10개의 객체를 생성
ChildOne arrOne[] = new ChildOne[10];
ChildTwo arrTwo[] = new ChildTwo[10];
/*
String name = "";
if(name.equals("one")) {
}else {
arrOne[0] = new ChildOne();
arrTwo[0] = new ChildTwo();
arrTwo[1] = new ChildTwo();
arrOne[1] = new ChildOne();
arrOne[2] = new ChildOne();
arrOne[3] = new ChildOne();
arrTwo[2] = new ChildTwo();
arrOne[4] = new ChildOne();
*/
// 자식 클래스의 객체를 총 10개의 객체를 생성
// ParentClass pc1 = new ChildOne();
// ParentClass pc2 = new ChildTwo();
ParentClass arrParent[] = new ParentClass[10];
arrParent[0] = new ChildOne();
arrParent[1] = new ChildTwo();
arrParent[2] = new ChildTwo();
arrParent[3] = new ChildOne();
arrParent[4] = new ChildOne();
arrParent[5] = new ChildOne();
arrParent[6] = new ChildTwo();
arrParent[7] = new ChildOne();
arrParent[8] = new ChildOne();
arrParent[9] = new ChildTwo();
for (int i = 0; i < arrParent.length; i++) {
arrParent[i].Method();
if(arrParent[i] instanceof ChildOne) {
ChildOne one = (ChildOne)arrParent[i];
one.func();
}
else if(arrParent[i] instanceof ChildTwo) {
ChildTwo two = (ChildTwo)arrParent[i];
two.proc();
}
}
/*
ChildOne o = (ChildOne)arrParent[0];
o.func();
// instanceof : 생성된 class를 찾아준다.
if(arrParent[0] instanceof ChildOne) {
ChildOne one = (ChildOne)arrParent[0];
one.func();
}
*/
}
}
ParentClass
public class ParentClass {
public void Method() {
System.out.println("ParentClass Method()");
}
}
ChildOneClass
public class ChildOne extends ParentClass{
public void Method() { // over ride
System.out.println("ChildOne Over Ride Method()");
}
public void func() {
System.out.println("ChildOne func()");
}
}
ChildTwoClass
public class ChildTwo extends ParentClass{
public void Method() { // over ride
System.out.println("ChildTwo Over Ride Method()");
}
public void proc() {
System.out.println("ChildTwo proc()");
}
}
'Coding > Java' 카테고리의 다른 글
Interface (0) | 2021.12.17 |
---|---|
Abstract class (0) | 2021.12.17 |
Over ride.2 (0) | 2021.12.16 |
Over ride.1 (0) | 2021.12.16 |
Inheritance.2 (0) | 2021.12.16 |