Java 在泛型中传递接口将做什么? 公共接口IComponent{ 公共虚无游戏(); public void setPlaybackSpeed(); 公共字符串getName(); } 公共类播放列表实现IComponent{ 公共字符串名称; public ArrayList playlist=新建ArrayList(); 公共播放列表(字符串播放名){ this.playliName=播放名; } 公共游戏{ System.out.println(“播放歌曲”); } public void setPlaybackSpeed(){ System.out.println(“将播放速度设置为指定值”); } 公共字符串getName(){ 返回此文件名; } 公共无效添加(IComponent组件){ } 公共无效删除(IComponent组件){ } }

Java 在泛型中传递接口将做什么? 公共接口IComponent{ 公共虚无游戏(); public void setPlaybackSpeed(); 公共字符串getName(); } 公共类播放列表实现IComponent{ 公共字符串名称; public ArrayList playlist=新建ArrayList(); 公共播放列表(字符串播放名){ this.playliName=播放名; } 公共游戏{ System.out.println(“播放歌曲”); } public void setPlaybackSpeed(){ System.out.println(“将播放速度设置为指定值”); } 公共字符串getName(){ 返回此文件名; } 公共无效添加(IComponent组件){ } 公共无效删除(IComponent组件){ } },java,generics,Java,Generics,我听不懂这句话 public interface IComponent { public void play(); public void setPlaybackSpeed(); public String getName(); } public class Playlist implements IComponent { public String playlistName; public ArrayList<IComponent> playlist = new Array

我听不懂这句话

public interface IComponent {
public void play();
public void setPlaybackSpeed();
public String getName();



}
public class Playlist implements IComponent {

public String playlistName;
public ArrayList<IComponent> playlist = new ArrayList();

public Playlist(String playlistName) {
    this.playlistName = playlistName;
}



public void play(){
  System.out.println("Playing the song");
}

public void setPlaybackSpeed(){
  System.out.println("Setting the speed of playback to the specified value");
}

public String getName(){
  return this.playlistName;
}


public void add(IComponent component){

}

public void remove(IComponent component){

}
}
public ArrayList playlist=new ArrayList();

当我们在泛型中传递任何类时,它只允许传递该类型数据的对象。当我们传递接口时将发生什么,将编码什么。

您必须传递一个实现接口中调用的方法的实例

这就是接口和泛型的美妙之处:您可以通过更改实现类以定义良好的方式更改行为

我想说你的班级命名还有很多需要改进的地方。我会选择
Song
而不是
IComponent
。后者对我来说太普通了

您没有显示
IComponent
界面,但是如果它包含
play
方法,那么对我来说是有意义的。这种设计允许用户播放单个歌曲或整个播放列表。这就是所谓的模式


你有更多的工作要做的方法。您应该能够轻松地实现
播放
添加
,以及
删除

您实际上是将功能和属性混合在一起。这里playlist实际上是一个容器,它应该包含可播放的项目,而不是能够播放它们

制作播放列表泛型:

public ArrayList<IComponent> playlist = new ArrayList();
现在您可以使用这些:

public class IComponentImpl implements IComponent {
public void play(){
  System.out.println("Playing the song");
}

public void setPlaybackSpeed(){
  System.out.println("Setting the speed of playback to the specified value");
}
...// other methods related to playable objects.
}
IComponent song1=新的icomponentmpl();
IComponent song2=新的icomponentmpl();
PlayList PlayList=新建播放列表();
播放列表。添加(歌曲1);
播放列表。添加(歌曲2);
//返回一首歌
playlist.get(在位置).play();

这就是您可以同时使用泛型和接口的方式。

我无法理解这段代码将要执行的操作。我正在尝试理解代码public ArrayList playlist=new ArrayList();将按此处的操作,接口在泛型中传递。这意味着
播放列表
包含作为
IComponent
子类的对象。
public class IComponentImpl implements IComponent {
public void play(){
  System.out.println("Playing the song");
}

public void setPlaybackSpeed(){
  System.out.println("Setting the speed of playback to the specified value");
}
...// other methods related to playable objects.
}
IComponent song1 = new IComponentImpl();
IComponent song2 = new IComponentImpl();

PlayList<IComponent> playlist = new PlayList<>();

playlist.add(song1);
playlist.add(song2);
// which return a song
playlist.get(at_position).play();