the space coding
◈ instanceof
instanceof 연산자는 프로그램 실행시 참조 데이터형을 검사하기 위해 사용되는 연산자 입니다.
자세 한건 코드에 주석 달아 났으니 참고 해주시면 되겠습니다.(예제도 있습니다.)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 |
package Day7;
import java.nio.channels.DatagramChannel;
class God{
}
class Angel extends God {
}
class Devil extends God{
}
public class Instan {
public static void main(String[] args) {
//instanceof 연산자는 프로그램 실행시 참조 데이터형을 검사하기 위해 사용되는 연산자 입니다.
// 래퍼런스가 가르키는 실제 객체가 어떤 클래스 타입인지 구분
// 왼 쪽이 오른쪽에 오는 클래스 객체이거나 하위 클래스의 객체일 경우 ture를 반환하고 그렇지않을 경우 false를 반환합니다.
//간단한 예제를 해보면 String변수 s 는 String클래스의 객체이므로 ture 반환하게 되는 것
String s= "s";
System.out.println(s instanceof String);
// 에러가 발생하는 경우 instanceof 연산자 왼쪽의 변수가 오른쪽의 클래스와 상속 관계가 없을 경우
// 객체가 아닌 기본 데이터형을 비교하려고 할때
God a=new Angel();
God d=new Devil();
// 좀더 예제를 들어 보자면
//변수 instanceof 클래스객체
if(a instanceof Angel) {
System.out.println("엔젤");
}
if(a instanceof Devil) {
System.out.println("엔젤아님");
}
if(d instanceof Angel) {
System.out.println("악마");
}
if(d instanceof Devil) {
System.out.println("악마아님");
}
}
}
|
cs |
'C++' 카테고리의 다른 글
[C++][1차원 포인터를 활용한 성적관리 프로그램] (0) | 2018.04.16 |
---|---|
[C++][string만들기] (0) | 2018.04.16 |
C++setw()=함수 (0) | 2018.03.21 |
[C++]깊은 대입연산자함수 (0) | 2018.03.21 |
[C++] operator 함수 (0) | 2018.03.21 |