我有一個抽象超類Show
,它有一個日期提交openDate
。它有幾個子類,最後我創建了一個通用的TheaterSchedule
類,可以把超類和所有子類作爲參數。然後在泛型類我做了以下添加的子類的對象:操作通用對象
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
的子類的方法類),直到該日期之前一直存在。我期望以下輸出:
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
爲了得到輸出我必須做follwoing:
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中的對象的日期,並返回一個數組(ArrayList不是!)對象的方法內那場比賽的dates.My Show()
超類型構造有一個名爲openDate
。我想去做compareTo
也許在mixed.addShow(...)
對象日期參數list.But的問題是,我也有其他不同的參數,在那裏,我真的不知道如何具體比較解析日期文本(請參閱我的代碼塊)和我將放入方法中的日期對象。
你爲什麼使它通用? – Logan
爲了能夠參數化類,我可以定義類的類型。我在這裏專門學習了泛型的使用。 – Afif
難道你不能只是遍歷'TheaterSchedule'對象,並將日期與傳入的'date'對象進行比較嗎? – Logan