gpt4 book ai didi

java - HashTable传值是Interface?

转载 作者:行者123 更新时间:2023-12-01 17:01:24 25 4
gpt4 key购买 nike

我看到一些代码,其中 HashTable(集合)值作为接口(interface)传递

public class Channels {

private static final Hashtable<String, IChannel> channels =
new Hashtable<String, IChannel>();

public void putChannel(String serviceChannelName, IChannel channel) {
channels.put(serviceChannelName, channel);
}
}

喜欢

public interface IChannel {

public void start();

public void stop();
}

到底要使用什么以及如何传递接口(interface)对象(虽然我们无法创建接口(interface)对象。??请帮助我

最佳答案

您传递的不是接口(interface)对象,而是接口(interface)实现,这为您提供了将接口(interface)的不同实现放入 map 的空间。

关于java - HashTable传值是Interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27527347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com