1

Extending Java default collections is not considered good practice. But i am wondering if, for the sake of cleaner code, one would not extend, say ArrayList to get rid of the generic. For example

public class DoodleList extends ArrayList<Doodle> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

So that in one's code you do some thing like "someFramework.getContentAs(DoodleList.class)" without having those nasty warnings about generics...etc

NOTE Wrapping the List might not be an option in my case : the class is to be used in JSON mapping where the desired output is :

{doodles : [doodle1,doodle2,doodle3]}

not {doodles : {innerCollection : [doodle1,doodle2,doodle3]}}

4

2 回答 2

1

通过摆弄我的继承气味来隐藏令人讨厌的警告。

为什么不只使用注释来抑制警告。例如:

@SuppressWarnings("unchecked")
someFramework.getContentAs(ArrayList.class)"
于 2013-04-25T17:24:04.770 回答
1

如果您真的只想摆脱警告,请使用SuppressWarnings.

但是,如果DoodleList包含的不仅仅是一个ArrayList<Doodle>例如实现特定于涂鸦的逻辑,我认为扩展ArrayList<Doodle>.

于 2013-04-25T17:36:55.570 回答