如何使用instanceof函数打印继承中的某个类别?
How to print a certain category in inheritance using instanceof function?
我的三个子class是脱口秀、歌曲和电台广告class。我这里只有脱口秀节目。例如,当我为子类别输入 s、p 或 m 时。当我只想打印一个子类别时,它会打印该子class 的所有内容。
示例数据如下。
1000,S,The Newest Star,Space,51:20,T2.mp3
1001,P,Interview With George,Politics,15:00,george.mp3
1002,P,Crooks in Politics,Politics,21:35,crooks.mp3
1010,M,Cooking For Vegetarians,Food,5:00,vege2.mp3
1222,S,Where Is The Sun,Larries,23:33,larscience.mp3
但是如果我选择 "P" 作为脱口秀的子类别,它应该只打印这两个,如下所示。
1001,P,Interview With George,Politics,15:00,george.mp3
1002,P,Crooks in Politics,Politics,21:35,crooks.mp3
这是我的代码的一部分
private void searchCategoryToPrint()
{
String inputCategory = JOptionPane.showInputDialog("Choose from Radio Categories: Talk Show, Song, or Commercial\n" +
"and enter the chosen category");
String subCategory = JOptionPane.showInputDialog("Talk Show: S Science, P Politics, and M Miscellaneous\n");
if("talk show".equalsIgnoreCase(inputCategory))
{
for(Radio radioShows : radioList)
{
if("SPM".contains(subCategory.toUpperCase().substring(0)) && radioShows instanceof TalkShow)
{
System.out.println(radioShows.formatForFile());
}
}
}
您应该更改 if
条件以检查广播节目是否与您从对话框中选择的子类别相同:
if(radioShows.getCategory().equalsIgnoreCase(subCategory.substring(0)) && radioShows instanceof TalkShow) {
System.out.println(radioShows.formatForFile());
}
我的三个子class是脱口秀、歌曲和电台广告class。我这里只有脱口秀节目。例如,当我为子类别输入 s、p 或 m 时。当我只想打印一个子类别时,它会打印该子class 的所有内容。
示例数据如下。
1000,S,The Newest Star,Space,51:20,T2.mp3
1001,P,Interview With George,Politics,15:00,george.mp3
1002,P,Crooks in Politics,Politics,21:35,crooks.mp3
1010,M,Cooking For Vegetarians,Food,5:00,vege2.mp3
1222,S,Where Is The Sun,Larries,23:33,larscience.mp3
但是如果我选择 "P" 作为脱口秀的子类别,它应该只打印这两个,如下所示。
1001,P,Interview With George,Politics,15:00,george.mp3
1002,P,Crooks in Politics,Politics,21:35,crooks.mp3
这是我的代码的一部分
private void searchCategoryToPrint()
{
String inputCategory = JOptionPane.showInputDialog("Choose from Radio Categories: Talk Show, Song, or Commercial\n" +
"and enter the chosen category");
String subCategory = JOptionPane.showInputDialog("Talk Show: S Science, P Politics, and M Miscellaneous\n");
if("talk show".equalsIgnoreCase(inputCategory))
{
for(Radio radioShows : radioList)
{
if("SPM".contains(subCategory.toUpperCase().substring(0)) && radioShows instanceof TalkShow)
{
System.out.println(radioShows.formatForFile());
}
}
}
您应该更改 if
条件以检查广播节目是否与您从对话框中选择的子类别相同:
if(radioShows.getCategory().equalsIgnoreCase(subCategory.substring(0)) && radioShows instanceof TalkShow) {
System.out.println(radioShows.formatForFile());
}