Java与C++程序在运行时测试对象的类型
1. 先看Java程序
public class TestClassEqual {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Base base = new Base();
Base a = new A();
Base a2 = new A();
Base b = new B();
System.out.println(base.getClass()); // class Base
System.out.println(a.getClass()); // class A
System.out.println(b.getClass()); // class B
System.out.println(a instanceof A); // true
System.out.println(a instanceof Base); // true
System.out.println(a instanceof B); // false
System.out.println(base.getClass() == a.getClass()); // false
System.out.println(a.getClass() == b.getClass()); // false
System.out.println(a.getClass() == a2.getClass()); // true
}
}
class Base {
}
class A extends Base {
}
class B extends Base {
}
interface IC {
int c = 0;
void f();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Base base = new Base();
Base a = new A();
Base a2 = new A();
Base b = new B();
System.out.println(base.getClass()); // class Base
System.out.println(a.getClass()); // class A
System.out.println(b.getClass()); // class B
System.out.println(a instanceof A); // true
System.out.println(a instanceof Base); // true
System.out.println(a instanceof B); // false
System.out.println(base.getClass() == a.getClass()); // false
System.out.println(a.getClass() == b.getClass()); // false
System.out.println(a.getClass() == a2.getClass()); // true
}
}
class Base {
}
class A extends Base {
}
class B extends Base {
}
interface IC {
int c = 0;
void f();
}
得到输出结果(其实已经写在了注释里):
1 class Base
2 class A
3 class B
4 true
5 true
6 false
7 false
8 false
9 true
2 class A
3 class B
4 true
5 true
6 false
7 false
8 false
9 true
2. 然后再比较C++程序
1 //============================================================================
2 // Name : TestClassEqual.cpp
3 // Author : Nysanier
4 // Version :
5 // Copyright : Your copyright notice
6 // Description : Hello World in C++, Ansi-style
7 //============================================================================
8
9 #include "Base.h"
10 #include "A.h"
11 #include "B.h"
12
13 #include <typeinfo>
14 #include <iostream>
15 using namespace std;
16
17 int main() {
18 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
19
20 Base *base = new Base();
21 Base *a = new A();
22 Base *a2 = new A();
23 Base *b = new B();
24
25 cout << typeid(base).name() << endl; // P4Base
26 cout << typeid(a).name() << endl; // P4Base
27 cout << typeid(a2).name() << endl; // P4Base
28 cout << typeid(b).name() << endl; // P4Base
29
30 cout << typeid(*base).name() << endl; // 4Base
31 cout << typeid(*a).name() << endl; // 1A
32 cout << typeid(*a2).name() << endl; // 1A
33 cout << typeid(*b).name() << endl; // 1B
34
35 cout << (typeid(a) == typeid(base)) << endl; // 1
36 cout << (typeid(b) == typeid(base)) << endl; // 1
37 cout << (typeid(a) == typeid(b)) << endl; // 1
38 cout << (typeid(a) == typeid(a2)) << endl; // 1
39
40 cout << (typeid(*a) == typeid(*base)) << endl; // 0
41 cout << (typeid(*b) == typeid(*base)) << endl; // 0
42 cout << (typeid(*a) == typeid(*b)) << endl; // 0
43 cout << (typeid(*a) == typeid(*a2)) << endl; // 1
44
45 delete base;
46 delete a;
47 delete a2;
48 delete b;
49
50 return 0;
51 }
2 // Name : TestClassEqual.cpp
3 // Author : Nysanier
4 // Version :
5 // Copyright : Your copyright notice
6 // Description : Hello World in C++, Ansi-style
7 //============================================================================
8
9 #include "Base.h"
10 #include "A.h"
11 #include "B.h"
12
13 #include <typeinfo>
14 #include <iostream>
15 using namespace std;
16
17 int main() {
18 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
19
20 Base *base = new Base();
21 Base *a = new A();
22 Base *a2 = new A();
23 Base *b = new B();
24
25 cout << typeid(base).name() << endl; // P4Base
26 cout << typeid(a).name() << endl; // P4Base
27 cout << typeid(a2).name() << endl; // P4Base
28 cout << typeid(b).name() << endl; // P4Base
29
30 cout << typeid(*base).name() << endl; // 4Base
31 cout << typeid(*a).name() << endl; // 1A
32 cout << typeid(*a2).name() << endl; // 1A
33 cout << typeid(*b).name() << endl; // 1B
34
35 cout << (typeid(a) == typeid(base)) << endl; // 1
36 cout << (typeid(b) == typeid(base)) << endl; // 1
37 cout << (typeid(a) == typeid(b)) << endl; // 1
38 cout << (typeid(a) == typeid(a2)) << endl; // 1
39
40 cout << (typeid(*a) == typeid(*base)) << endl; // 0
41 cout << (typeid(*b) == typeid(*base)) << endl; // 0
42 cout << (typeid(*a) == typeid(*b)) << endl; // 0
43 cout << (typeid(*a) == typeid(*a2)) << endl; // 1
44
45 delete base;
46 delete a;
47 delete a2;
48 delete b;
49
50 return 0;
51 }
得到运行结果(其实结果已经在注释里):
1 !!!Hello World!!!
2 P4Base
3 P4Base
4 P4Base
5 P4Base
6 4Base
7 1A
8 1A
9 1B
10 1
11 1
12 1
13 1
14 0
15 0
16 0
17 1
2 P4Base
3 P4Base
4 P4Base
5 P4Base
6 4Base
7 1A
8 1A
9 1B
10 1
11 1
12 1
13 1
14 0
15 0
16 0
17 1
那么总结下能够发现,Java与C++都是面向对象的语言,他们都有多态特性,在运行时可以比较两个对象是否属于同一个类,如果不同返回的是false(0),Java的语言特性提供了instanceOf来判断一个对象是否是类外一个类的实例,如a instanceOf Base,返回的是true,说明a是Base的类型,但是在C++中我暂时还没有找到此方法来判断一个对象是否是某个基类的扩展类类型。
Java提供x.getClass,C++提供typeid(*x).name(),其作用都是一样的。