close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_fs.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Jul 10, 2008, 4:46:15 PM (18 years ago)
- Author:
-
waue
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
|
v7
|
v8
|
|
| 346 | 346 | [[BR]] |
| 347 | 347 | |
| 348 | | '''!GenericFoo<Integer> foo1 = null;[[BR]] |
| 349 | | |
| 350 | | !GenericFoo<Boolean> foo2 = null;'''[[BR]] |
| 351 | | |
| 352 | | |
| | 348 | '''!GenericFoo<Integer> foo1 = null;'''[[BR]] |
| | 349 | '''!GenericFoo<Boolean> foo2 = null;'''[[BR]] |
| 353 | 350 | |
| 354 | 351 | 那麼 foo1 就只接受!GenericFoo<Integer>的實例,而foo2只接受!GenericFoo<Boolean>的實例。[[BR]] |
| 355 | | [[BR]] |
| 356 | | |
| 357 | | 現在您有這麼一個需求,您希望有一個參考名稱foo可以接受所有下面的實例(List、Map或List介面以及其實介面的相關類別,在J2SE 5.0中已經針對泛型功能作了改寫,在這邊仍請將之當作介面就好,這是為了簡化說明的考量):[[BR]][[BR]] |
| 358 | | |
| 359 | | '''foo = new !GenericFoo<!ArrayList>(); |
| 360 | | foo = new !GenericFoo<!LinkedList>();'''[[BR]][[BR]] |
| | 352 | |
| | 353 | 現在您有這麼一個需求,您希望有一個參考名稱foo可以接受所有下面的實例(List、Map或List介面以及其實介面的相關類別,在J2SE 5.0中已經針對泛型功能作了改寫,在這邊仍請將之當作介面就好,這是為了簡化說明的考量):[[BR]] |
| | 354 | |
| | 355 | '''foo = new !GenericFoo<!ArrayList>();'''[[BR]] |
| | 356 | '''foo = new !GenericFoo<!LinkedList>();'''[[BR]] |
| 361 | 357 | |
| 362 | 358 | 簡單的說,實例化型態持有者時,它必須是實作List的類別或其子類別,要宣告這麼一個參考名稱,您可以使用 '?' 通配字元,並使用"extends"關鍵字限定型態持有者的型態,例如[[BR]] |
| 363 | 359 | [[BR]] |
| 364 | | |
| 365 | | '''!GenericFoo<? extends List> foo = null; |
| 366 | | foo = new !GenericFoo<!ArrayList>(); |
| | 360 | {{{ |
| | 361 | GenericFoo<? extends List> foo = null; |
| | 362 | foo = new !GenericFoo<ArrayList>(); |
| 367 | 363 | ..... |
| 368 | | foo = new !GenericFoo<!LinkedList>(); |
| 369 | | ....'''[[BR]] |
| 370 | | [[BR]] |
| 371 | | |
| 372 | | |
| 373 | | |
| | 364 | foo = new GenericFoo<LinkedList>(); |
| | 365 | ... |
| | 366 | }}} |
| | 367 | |
| 374 | 368 | 如果指定了不是實作List的類別或其子類別,則編譯器會回報錯誤,例如:[[BR]] |
| 375 | | [[BR]] |
| 376 | 369 | |
| 377 | 370 | '''!GenericFoo<? extends List> foo = new !GenericFoo<!HashMap>();'''[[BR]] |
| … |
… |
|
| 387 | 380 | |
| 388 | 381 | 這樣的限定是很有用的,例如如果您想要自訂一個showFoo()方法,方法的內容實作是針對List而制定的,例如:[[BR]] |
| 389 | | [[BR]] |
| 390 | 382 | |
| 391 | 383 | public void showFoo(!GenericFoo foo) {[[BR]] |
| 392 | 384 | // 針對List而制定的內容[[BR]] |
| 393 | 385 | }[[BR]] |
| 394 | | [[BR]] |
| 395 | | |
| 396 | | 您當然不希望任何的型態都可以傳入showFoo()方法中,您可以使用以下的方式來限定,例如: |
| 397 | | '''public void showFoo(!GenericFoo<? extends List> foo) {[[BR]] |
| 398 | | |
| 399 | | }'''[[BR]] |
| 400 | | [[BR]] |
| 401 | | |
| 402 | | |
| | 386 | |
| | 387 | 您當然不希望任何的型態都可以傳入showFoo()方法中,您可以使用以下的方式來限定,例如:[[BR]] |
| | 388 | '''public void showFoo(!GenericFoo<? extends List> foo) {}'''[[BR]] |
| | 389 | |
| 403 | 390 | 這麼一來,如果有粗心的程式設計人員傳入了您不想要的型態,例如!GenericFoo<Boolean>型態的實例,則編譯器都會告訴它這是不可行的,在宣告名稱時如果指定了<?>而不使用"extends",則預設是允許Object及其下的子類,也就是所有的Java物件了,那為什麼不直接使用!GenericFoo宣告就好了,何必要用!GenericFoo<?>來宣告?使用通配字元有點要注意的是,透過使用通配字元宣告的名稱所參考的物件,您沒辦法再對它加入新的資訊,您只能取得它的資訊或是移除它的資訊,例如: |
| 404 | 391 | {{{ |