回复:null是什么数据类型
2007-03-26 15:28
477 查看
例1
码如下:
编译通过了,而且运行时也没有错误,问题是null到底是什么数据类型,或者说java如何进行转型的
答
1. String 是 Object 的子类 。所以函数在调用时优先选择作为子类的String。 运行结果是String Version。
2. 若没有返回为String 的 test 函数,则函数调用 超类 的Object 对象。 运行结果为Object Version。
例2
Q. How does Java compiler resolve the ambiguity to decide which methods to call?
A:
In the following example, four test() methods, if we pass ambiguous /b{null} to the test, which one should (will) be called? The 3 on top has super/subclass/sub-subclass relationship. The most specific one (down hierarchy) will be called. The 4th with String as a parameter, but it is a sibling of Tester. Ambiguity compile time error results.
c.test (null);
即 test 在调用函数时优先选择作为子类的子类的 SubTester 所以运行结果是SubTester version。
例3
那么第一个method方法是会打出object version的,而第二个method则会出现编译错误,这又是怎么回事呢?
很明显,String s 与 StringBuffer s 都是Object 的子类,地位相同,所以造成冲突。
同理 在例2 中加入void test( String s) {System.out.println("String Version");}
也会引起冲突,造成编译错误
码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Test2 method(Object o) System.out.println(); method(String s) System.out.println(); main(String[] args) Test2 test = Test2(); test.method(); |
答
1. String 是 Object 的子类 。所以函数在调用时优先选择作为子类的String。 运行结果是String Version。
2. 若没有返回为String 的 test 函数,则函数调用 超类 的Object 对象。 运行结果为Object Version。
例2
Q. How does Java compiler resolve the ambiguity to decide which methods to call?
A:
In the following example, four test() methods, if we pass ambiguous /b{null} to the test, which one should (will) be called? The 3 on top has super/subclass/sub-subclass relationship. The most specific one (down hierarchy) will be called. The 4th with String as a parameter, but it is a sibling of Tester. Ambiguity compile time error results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1516 | Tester test(Object s) System.out.println (); test(Tester s) System.out.println (); test(SubTester s) System.out.println (); main (String args[]) Tester c = Tester (); c.test (); c.test ( Object()); SubTester Tester |
即 test 在调用函数时优先选择作为子类的子类的 SubTester 所以运行结果是SubTester version。
例3
Test2 method(Object o) System.out.println(); method(String s) System.out.println(); method( s) System.out.println(); main(String[] args) Test2 test = Test2(); Object o = Object(); test.method((Object)); test.method(); |
很明显,String s 与 StringBuffer s 都是Object 的子类,地位相同,所以造成冲突。
同理 在例2 中加入void test( String s) {System.out.println("String Version");}
也会引起冲突,造成编译错误
相关文章推荐
- sysname数据类型是什么
- 1Map的遍历 2Map.entrySet() 3Set,Entry数据类型是什么?
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了
- 我要从com向应用程序返回一个结构,结构中包括二进制数据,请问用什么方式传替比较好呢?BSTR,VARIANT还是其他什么类型比较好呢
- stm32 数据类型的定义(常用的U8,U16,U32到底代表什么)
- SQLServer陷阱(一) NULL与布尔数据类型
- MySQL 中你应该使用什么数据类型表示时间?
- 常见的object-c的数据类型有那些, 和C的基本数据类型有什么区别?如:NSInteger和int
- _variant_t 是什么数据类型??
- java中不同的数据类型传值到方法中,会产生什么影响
- javascript:数据类型Null与Undefined
- mongodb_数据类型(null/字符串/数字/日期/内嵌文档/数组等)(转并学习)
- 【转】uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了
- request('id')语句,返回的是什么类型的数据
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型c++/c? (在andriod源码中)
- Java数据类型Null判断
- java的set数据类型,是什么?怎么用?
- ElemType是什么数据类型?
- 随笔-uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了
- Android之AIDL的全称是什么?如何工作?能处理哪些类型的数据?