What's the harm in using Anonymous class?

后端 未结 6 1251
-上瘾入骨i
-上瘾入骨i 2021-01-31 08:41

The question arose while reading a answer to this question - How do I join two lists in java. This answer gave the solution

List newList = new Arra         


        
6条回答
  •  半阙折子戏
    2021-01-31 09:11

    In your example it really looks evil and ugly, at least to me - it's difficult to understand what's going on in the code. But there are some patterns of using anonymous classes that people are used to because they see them very often, eg

        Arrays.sort(args, new Comparator() {
            public int compare(String o1, String o2) {
                return  ... 
            }});
    

    I would call the above a best practice case.

提交回复
热议问题