日期:2014-05-20  浏览次数:20654 次

求问此语句何意

Java中的一句代码:
Comparable []c={4,9,23,1,45,27,5,2};

Comparable作为接口,可以直接这样用吗?基于何种缘由呢?
Java

------解决方案--------------------
java.lang 
类 Integer
java.lang.Object
  java.lang.Number
      java.lang.Integer
所有已实现的接口: 
Serializable, Comparable<Integer> 

从Integer类的API可以看出Integer类实现了Comparable接口
Comparable []c  ={4,9,23,1,45,27,5,2}可以理解为
Integer    []c1 ={4,9,23,1,45,27,5,2}; Comparable []c2 =c1;
其实就是:接口 = 接口的实现类
------解决方案--------------------
声明一个为COMPARABLE 的类的数组。数组元素必须都是implement comparable接口的实例。 
------解决方案--------------------
int值被自动装箱为Integer了
像这样的接口数组定义都是合法的
CharSequence []cs= {"s","f"};

引用:
Comparable []c  ={4,9,23,1,45,27,5,2}可以理解为
Integer    []c1 ={4,9,23,1,45,27,5,2}; Comparable []c2 =c1;

这句话错了,因为Comparable []c  ={4.3,9.8f,23l,1,4e5,27,5,2}也是合法的
只要值都实现了Comparable就行,类型可以不一样
------解决方案--------------------
Comparable []c={4,9,23,1,45,27,5,2};
实际上是
Comparable []c={Integer.valueOf(4),Integer.valueOf(9),Integer.valueOf(23),Integer.valueOf(1)...};