Java 如果集合中的元素类型为接口类型,如何填充集合?(爪哇)

Java 如果集合中的元素类型为接口类型,如何填充集合?(爪哇),java,collections,interface,Java,Collections,Interface,这是我早些时候面试时问的一个问题。我不知道为什么会有人想这样做,或者这是可能的,但有人会如何填充这个集合 Collection<MyLinks> links = null; //Populate this variable public interface MyLinks() { //Method headers only } 集合链接=null//填充此变量 公共接口MyLinks(){ //仅方法头 } 如果无法实例化MyLink对象,如何填充此集合?这是一个

这是我早些时候面试时问的一个问题。我不知道为什么会有人想这样做,或者这是可能的,但有人会如何填充这个集合

Collection<MyLinks> links = null;    //Populate this variable

public interface MyLinks() {
    //Method headers only
}
集合链接=null//填充此变量
公共接口MyLinks(){
//仅方法头
}

如果无法实例化MyLink对象,如何填充此集合?这是一个技巧性的问题吗?

这样的集合可以用其类实现该接口的任何对象填充。对象可以是不同的类(甚至是匿名类),只要这些类实现了该接口

class ConcreteMyLinks implements MyLinks...
class ConcreteMyLinks2 implements MyLinks...

ConcreteMyLinks obj = new ConcreteMyLinks();
ConcreteMyLinks2 obj2 = new ConcreteMyLinks2();
collection.add(obj);
collection.add(obj2);
collection.add(new MyLinks() {  /* implement interface here */ });
public interface MyInterface {
  int getANumber();
}

public class RandomNumberGenerator implements MyInterface {
  public int getANumber() {
    return 4; // choosen by a fair dice roll
  }
}

Collection<MyInterface> collection = new ArrayList<MyInterface>();
collection.add(new RandomNumberGenerator());

这样的集合可以由其类实现该接口的任何对象填充。对象可以是不同的类(甚至是匿名类),只要这些类实现了该接口

class ConcreteMyLinks implements MyLinks...
class ConcreteMyLinks2 implements MyLinks...

ConcreteMyLinks obj = new ConcreteMyLinks();
ConcreteMyLinks2 obj2 = new ConcreteMyLinks2();
collection.add(obj);
collection.add(obj2);
collection.add(new MyLinks() {  /* implement interface here */ });
public interface MyInterface {
  int getANumber();
}

public class RandomNumberGenerator implements MyInterface {
  public int getANumber() {
    return 4; // choosen by a fair dice roll
  }
}

Collection<MyInterface> collection = new ArrayList<MyInterface>();
collection.add(new RandomNumberGenerator());

您创建了一个实现接口的类,并用它填充。

您创建了一个实现接口的类,并用它填充。

用实现接口的对象填充集合

class ConcreteMyLinks implements MyLinks...
class ConcreteMyLinks2 implements MyLinks...

ConcreteMyLinks obj = new ConcreteMyLinks();
ConcreteMyLinks2 obj2 = new ConcreteMyLinks2();
collection.add(obj);
collection.add(obj2);
collection.add(new MyLinks() {  /* implement interface here */ });
public interface MyInterface {
  int getANumber();
}

public class RandomNumberGenerator implements MyInterface {
  public int getANumber() {
    return 4; // choosen by a fair dice roll
  }
}

Collection<MyInterface> collection = new ArrayList<MyInterface>();
collection.add(new RandomNumberGenerator());
公共接口MyInterface{
int getANumber();
}
公共类RandomNumberGenerator实现MyInterface{
public int getANumber(){
返回4;//通过公平掷骰选择
}
}
集合集合=新的ArrayList();
添加(新的RandomNumberGenerator());

提示:如果需要随机数生成器,请不要复制代码。

用实现接口的对象填充集合

class ConcreteMyLinks implements MyLinks...
class ConcreteMyLinks2 implements MyLinks...

ConcreteMyLinks obj = new ConcreteMyLinks();
ConcreteMyLinks2 obj2 = new ConcreteMyLinks2();
collection.add(obj);
collection.add(obj2);
collection.add(new MyLinks() {  /* implement interface here */ });
public interface MyInterface {
  int getANumber();
}

public class RandomNumberGenerator implements MyInterface {
  public int getANumber() {
    return 4; // choosen by a fair dice roll
  }
}

Collection<MyInterface> collection = new ArrayList<MyInterface>();
collection.add(new RandomNumberGenerator());
公共接口MyInterface{
int getANumber();
}
公共类RandomNumberGenerator实现MyInterface{
public int getANumber(){
返回4;//通过公平掷骰选择
}
}
集合集合=新的ArrayList();
添加(新的RandomNumberGenerator());
提示:如果您需要随机数生成器,请不要复制代码。

尝试以下方法:

    links = new ArrayList<MyLinks>();
    links.add(new MyLinks() { });
links=newarraylist();
links.add(新的MyLinks(){});
祝你好运

试试这个伴侣:

    links = new ArrayList<MyLinks>();
    links.add(new MyLinks() { });
links=newarraylist();
links.add(新的MyLinks(){});

祝你好运

上述解决方案是正确的,您也可以使用匿名类:

MyInterface object = new MyInterface() {
   //here override interfaces' methods
}

上述解决方案是正确的,您也可以使用匿名类:

MyInterface object = new MyInterface() {
   //here override interfaces' methods
}
  • 您可以创建实现该接口的具体类的实例,并将其添加到 收藏

    collection.add(new MyConcreteLinks());
    
  • 您可以创建匿名类实例,如:

    collection.add(new MyLinks() { /*overide mylinks method*/});
    
  • 您可以创建实现该接口的具体类的实例,并将其添加到 收藏

    collection.add(new MyConcreteLinks());
    
  • 您可以创建匿名类实例,如:

    collection.add(new MyLinks() { /*overide mylinks method*/});
    

  • null
    :)@JigarJoshi:Heh填充它。技术上正确,但毫无用处;这是一个很好的答案,在进入真正的答案之前先扔掉它。:-)这不能编译。为什么
    公共接口MyLinks
    后面会有括号?“我不知道为什么会有人想这样做”-可能是因为对接口编程是一种最佳做法。用
    null
    填充它:)@JigarJoshi:Heh。技术上正确,但毫无用处;这是一个很好的答案,在进入真正的答案之前先扔掉它。:-)这不能编译。为什么在
    公共接口MyLinks
    后面有括号?“我不知道为什么有人想这样做”-可能是因为这是对接口编程的最佳实践。正确。。。因为在上面的代码片段中,它没有:)如果它有-只需在新MyLink()之后在花括号之间实现存根。正确。。。因为在上面的代码片段中,它没有:)如果有,只需在花括号之间的new MyLinks()之后实现存根。