操纵通用对象

时间:2016-04-24 00:46:19

标签: java arrays generics

我有一个抽象的超类Show,它有一个日期归档openDate。它有几个子类,最后我创建了一个通用的TheaterSchedule类,它可以超级class和所有子类作为参数。然后在该泛型类中,我执行以下操作来添加子类的对象:

public static void main(String[] args) {
SimpleDateFormat dateText = new SimpleDateFormat("MM/dd/yy");
TheaterSchedule<Show> mixed = new TheaterSchedule<Show>();
try {
  mixed.addShow(new Musical("Hamilton", "Lin-Manuel Miranda", "Lin-Manuel Miranda", "Richard Rodgers Theatre", dateText.parse("08/6/15")));
  mixed.addShow(new MusicalComedy("Wicked", "Winnie Holzman", "Stephen Schwartz", "Gershwin Theatre", dateText.parse("10/30/03")));
  mixed.addShow(new Drama("The Curious Incident of the Dog in the Nighttime","Simon Stephens","Ethel Barrymore Theatre", dateText.parse("10/5/14")));
  mixed.addShow(new Musical("The Lion King", "Roger Allers and Irene Mecchi", "Elton John and Tim Rice", "Minskoff Theatre", dateText.parse("11/13/97")));
  mixed.addShow(new Comedy("An Act of God", "David Javerbaum", "Booth Theatre", dateText.parse("06/6/2016")));
  mixed.addShow(new Musical("Kinky Boots", "Harvey Fierstein", "Cyndi Lauper", "Al Hirschfeld Theatre", dateText.parse("04/4/13")));
  TheaterSchedule.addMusicalComedy(mixed, "The Book of Mormon", "Trey Parker, Robert Lopez and Matt Stone", "Trey Parker, Robert Lopez and Matt Stone", "Eugene O'Neill Theatre", dateText.parse("03/24/11"));
} catch(java.text.ParseException ex) {
  System.out.println(ex.getMessage());
}
System.out.println("Selected Broadway Shows:");
TheaterSchedule.printSchedule(mixed);

现在我要创建一个方法,该方法可以占用特定日期并返回一个对象数组(Show类的子类),这些对象一直存在,直到那个Date.I期望以下输出:

Broadway shows open since 04/01/14:
Wicked - Playwright: Winnie Holzman, Composer: Stephen Schwartz at
Gershwin Theatre since 10/30/03
The Lion King - Playwright: Roger Allers and Irene Mecchi, Composer:
Elton John and Tim Rice at Minskoff Theatre since 11/13/97
Kinky Boots - Playwright: Harvey Fierstein, Composer: Cyndi Lauper at
Al Hirschfeld Theatre since 04/04/13
The Book of Mormon - Playwright: Trey Parker, Robert Lopez and Matt
Stone, Composer: Trey Parker, Robert Lopez and Matt Stone at Eugene
O'Neill Theatre since 03/24/11 

为了获得输出,我必须进行以下操作:

 try {
  Date when = dateText.parse("04/01/14");
  Show[] current = TheaterSchedule.<Show>getShowsOpenSince(mixed, when);
  System.out.println();
  System.out.println("Broadway shows open since " + dateText.format(when) + ":");
  for (int i=0; i<current.length; i++)
    System.out.println(current[i]);
} catch(java.text.ParseException ex) {
  System.out.println(ex.getMessage());
}

我需要构建getShowsOpenSince方法,该方法将在给定时间之前和之前返回节目数组。我创建了方法标题如下:

public static <E> E [] getShowsOpenSince(TheaterSchedule<E> object,Date date) {

现在在方法内部,我必须进行某些操作,检查ArrayList中对象的日期,并返回与dates匹配的对象的数组(不是ArrayList!)。我Show()超类型构造函数有一个名为openDate的Date参数。我希望像compareTo列表中的对象一样mixed.addShow(...)。但问题是我在那里也有不同的其他参数而且我真的不喜欢不知道如何将解析后的日期文本(请参阅我的代码块)与我将放在方法中的日期对象进行具体比较。

1 个答案:

答案 0 :(得分:1)

泛型变量对于程序员来说完全没用,因为它是E未指定的类型,你不能对它做任何假设:你可以通过E实例到其他通用对象。

无论您需要对TheaterSchedule<E>执行什么操作,都必须对E类型应用绑定,以便您可以使用某些内容。

官方文档here更好地解释了这一点,但重点是允许您声明类型变量以某种方式绑定到某个类型,例如

public <E extends Show> E[] getsShowOpenSince(TheatherScheduler<E>, Date date) { ... }

这样,无论E是什么,您都可以确定至少有Show个实例,并且能够调用为Show定义的所有内容。这也意味着金属的反对:你不会被传递给getsShowOpenSince TheatherScheduler<String>,因为String不会延伸Show。< / p>