简体   繁体   English

Java匿名内部类List

[英]Java anonymous inner class of List

I want to attach a method to a LinkedList object at run time via an anonymous inner class. 我想通过匿名内部类在运行时将方法附加到LinkedList对象。 Is this possible? 这可能吗?

For example: 例如:

    LinkedList<String> paths = new LinkedList<String>() {
        void addAllIfNotNull(Collection<String> c) {
            if(c != null) {
                addAll(c);
            }
        }
    };
    paths.add(list1);
    paths.add(list2);
    paths.add(list3);
    ...
    paths.add(listN);

Without having to do a if(list_i is not null) addAll(list_i) ? 无需执行if(list_i不为null)addAll(list_i)?

This doesn't look like it is possible because the LinkedList does not have an AddAllIfNotNull method. 这似乎不可能,因为LinkedList没有AddAllIfNotNull方法。 Is there a way to make this work? 有没有办法让这项工作?

This is not possible via an anonymous inner class. 这是不可能通过匿名内部类。 It can be done with a non-anonymous inner class or a stand-alone class, but not anonymous since the compiler will have no way of recognizing the method since to the compiler the variable is of type LinkedList , and if you wanted to cast the variable, what would you cast it to since the type you want is anonymous? 它可以使用非匿名内部类或独立类来完成,但不是匿名的,因为编译器无法识别该方法,因为编译器的变量是LinkedList类型,并且如果你想要转换它变量,你会把它投射到什么,因为你想要的类型是匿名的?

Question for you: what happens when you try this? 问题:你试试看会发生什么? Does it compile when you try to call the method? 当你试图调用方法时它会编译吗?

eg, 例如,

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class LinkedListFoo {
   public LinkedListFoo() {
      List<String> list = new ArrayList<>();

      MyLinkedList<String> paths = new MyLinkedList<>();
      paths.addAllIfNotNull(list);     
   }

   // non-anonymous inner class
   private class MyLinkedList<T> extends LinkedList<T> {
      void addAllIfNotNull(Collection<T> c) {
         if(c != null) {
             addAll(c);
         }
     }
   }
}

You can't add a new method with a new signature, since the type stays LinkedList<String> . 您无法使用新签名添加新方法,因为该类型保留LinkedList<String>

The way to do what you want, is to overide the existing method addAll() from LinkedList<T> , this way : 做你想要的方法是从LinkedList<T>覆盖现有方法addAll() ,这样:

    LinkedList<String> paths = new LinkedList<String>() {
        public boolean addAll(Collection<? extends String> c) {
            System.out.println("In overiden addAll");
            if(c != null) {
                return super.addAll(c);
            }
            return false;
        }
    };
    // This now works
    paths.addAll(null);

I think Hovercraft's answer should be accepted, but another option for you is to override some method on LinkedList<T> --say, boolean addAll(Collection<? extends E> c) , in which you could do the null check before calling super.addAll(c) (effectively preventing it from throwing NullPointerException ). 我认为Hovercraft的答案应该被接受,但另一个选择是覆盖LinkedList<T>上的一些方法--say, boolean addAll(Collection<? extends E> c) ,你可以在调用super.addAll(c)之前进行null检查super.addAll(c) (有效地阻止它抛出NullPointerException )。 That way you can call addAll and still use an anonymous class. 这样你就可以调用addAll并仍然使用匿名类。 However, this technique should be used cautiously. 但是,应谨慎使用此技术。 java.util.LinkedList#addAll can be reasonably expected to throw the exception given a null parameter. java.util.LinkedList#addAll可以合理地期望在给定null参数的情况下抛出异常。 Using a named class avoids that confusion. 使用命名类可以避免这种混淆。

This is not possible and you should not do this. 这是不可能的,你不应该这样做。 Instead, make it a static method. 相反,使它成为一个静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM