2016-04-24 63 views
0

我有一個抽象超類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的問題是,我也有其他不同的參數,在那裏,我真的不知道如何具體比較解析日期文本(請參閱我的代碼塊)和我將放入方法中的日期對象。

+2

你爲什麼使它通用? – Logan

+0

爲了能夠參數化類,我可以定義類的類型。我在這裏專門學習了泛型的使用。 – Afif

+0

難道你不能只是遍歷'TheaterSchedule'對象,並將日期與傳入的'date'對象進行比較嗎? – Logan

回答

1

泛型類型變量是程序員完全沒有用處,因爲它是不特定類型的E你不能對任何假設:你可以通過E實例周圍其他通用對象。

無論您需要如何處理TheaterSchedule<E>,您都必須對E類型應用綁定,以便您可以繼續工作。

這是更好的官方文檔here的解釋,但這樣做的問題是,你被允許申報的一個類型變量莫名其妙界的類型,例如

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

,這樣,無論是E,你可以肯定這是至少Show實例,並能夠調用一個專門針對Show定義的一切。這也意味着金屬的opposide:你將不會被允許通過getsShowOpenSince一個TheatherScheduler<String>因爲String沒有延伸Show

+0

我想知道你有一個E []返回類型,但同時你將該方法定義爲void? – Afif

+0

@ user65:只是一個錯字。 – Jack