当前页面: 开发资料首页 → J2SE 专题 → J2SE 5.0 的角落
摘要: J2SE 5.0 的角落
1,dynamic cast
类似C++的dynamic_cast
Class.cast
public T cast(Object obj)
obj
- the object to be cast ClassCastException
- if the object is not null and is not assignable to the type T. 2,返回值协变
interface SomeInterface{
Object get();
}
class CovariantImpl implements SomeInterface{
public String get(){
return "covariant";
}
}
public class TestUntitled2 extends TestCase {
public void testCovariant() {
SomeInterface obj = new CovariantImpl();
Assert.assertEquals("covariant", obj.get());
}
}
3,类型安全的代理
类似只读代理Collections.unmodifiableXXX(), 同步代理Collections.synchronizedXXX(), J2SE 5.0提供了类型安全的代理:Collections.checkedXXX()
4,Arrays.deepEquals()
不愧是放在java.util包里的
5,皇帝的“generic”新衣
加了一堆眼花缭乱的概念,却受“擦除法”实现方式所累,目前个人用到的最实用的功能仅仅是避免部分强制类型转换
因为要擦除,所以无法特化,只能使用继承加实例化模拟,彻底扼杀根据实参类型进行自动分派的一切想法
因为要擦除,所以与类型参数(TypeVariable)有关的一切运行期计算,如强制类型转换,都毫无意义
要想在泛型类方法里调用类型参数(TypeVariable)的某个业务方法,则类型参数必须<? extends SomeSuper>,一个推论就是<? extends MarkerInterface>在
讽刺的是,摆明了将问题扔给编译器解决,编译器却除了帮你强转,塞些TypeVariable信息外几乎没干什么事
而Class.forName()后,一切都不安全了
6,基于返回值的类型推导
不知算不算“擦除法”带来的特性
例一、Collections.emptyXXX()
public static finalList emptyList()
This example illustrates the type-safe way to obtain an empty list:
ListImplementation note: Implementations of this method need not create a separate List object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)s = Collections.emptyList();
EMPTY_LIST
例二、一步一次推导
但如果其返回值用于另外一个函数的泛型参数,则必须用临时变量过渡一下,可能是因为不能同时进行两步推导
public class ConditionParser {
private ConditionParser(){
}
public static
....
}
}
List< Condition
Condition
conditions.add(safe);//ok
List< Condition
conditions.add(ConditionParser.deserialize(condition.ToXML()));//error
7,类型安全的Varargs
其实是数组的简写形式,因此是类型安全的,带来方便的同时,尚未发现有什么副作用
8,serialVersionUID
到今天了还用实现来暴露意图,殆也,用Annotation来实现也比现在顺眼一点,眼睁睁的看着它混在业务数据对象中
9,面向对象的enum
区别于C++中的enum,J2SE 5.0中的enum是符合“UML有限子类”情形的面向对象的实现(尽管看起来像“有限实例”):可以实现接口,可以有构造函数,可以有方法,成员,除了不能继承和被继承,构造函数必须私有,其它的和普通Java类差不多
(to be continue...)
(The Java Programming Language Notes )
↑返回目录
前一篇: 谈谈J2SE中的序列化
后一篇: Five Reasons to Move to the J2SE 5 Platform