1 / 20

Chapter 6 Java Generic Type

Chapter 6 Java Generic Type. x.zhang@seu.edu.cn. Content. Significance of Generic Type Definition of Generic Type Usage of Generic Type. Significance of Generic Type. What is Generic Type Why Generic Type? How about the inconvenience without GT?. Significance of Generic Type. Example

milica
Télécharger la présentation

Chapter 6 Java Generic Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Java Generic Type x.zhang@seu.edu.cn

  2. Content • Significance of Generic Type • Definition of Generic Type • Usage of Generic Type Java CoSE

  3. Significance of Generic Type • What is Generic Type • Why Generic Type? • How about the inconvenience without GT? Java CoSE

  4. Significance of Generic Type • Example • Write a MyArrayList • Without GT • The element is Object • Think: Is there any problem? How to solve? Java CoSE

  5. Significance of Generic Type • Example • Write MyArrayListusing GT • GT improves the simplicity and correctness Java CoSE

  6. Significance of Generic Type • Merit of GT • Type-safe • Eliminate Type Casting • Usually used in defining type of elements in Collections Java CoSE

  7. Definition of Generic Type • Generic Class • Generic Interface Java CoSE

  8. 泛型的定义 • Bounded Type Parameter (受限类型参数) • Sometimes, parameter type in GT should be limited • Can Person be the parameter type in SortedCollection? Java CoSE

  9. Definition of Generic Type • Bounded Type Parameter • The meaning Keyword extends is generic • Comparableinterface is called upper bound • Eis called a Bounded Type Parameter Java CoSE

  10. 泛型的定义 • Bounded Type Parameter • Multiple-bounded Java CoSE

  11. Usage of Generic Type • Example: Write a Static Method to Sum a List • 1. without GT • Short: We do not know the element type, and cannot guarantee each element in List L is able to sum up. Java CoSE

  12. Usage of Generic Type • 2. With GT Java CoSE

  13. Usage of GT • 3. With GT and Wildcard Java CoSE

  14. Some Tips • ArrayList<Number> a = new ArrayList<Integer>(); // right or not? • ArrayList<? extends Number> a = new ArrayList<Integer>(); // right or not? • In invocation of method public void add(Number a), what type of objects can we transfer into a method as parameter • Just the type Number // right or not? • Number and all its subtypes // right or not? Java CoSE

  15. Usage of Generic Type • Usage of Wildcard • Define Upper bound • List<? extends Number> • Define Lower bound • List<? super Integer> • List<Integer>, List<Number>, List<Serializable>, List<Comparable<Integer>>, List<Object> • Multiple-bounded is not allowed • List<? extends Number & Serializable> // Compiling error • Guess: is List<?> different with List<Object>? Java CoSE

  16. Example

  17. Example

  18. Example

  19. Self-study • Text Processing • Basic elements in text processing (chapter 7) • String / StringBuffer / StringBuilder // try to benchmark • Scanner • StringTokenizer • Javaand IR (Information Retrieval) • Tokenizing (分词) • Stopwords Removal (去除停用词) • Rooting(取词根) • Indexing (索引) Java CoSE

  20. Forecast • AWTand SwingIntroduction • SwingContainer(JFrame, JPanel) • Swing Components • Layout Manager • Eventand Event-based Programming • Menu Java CoSE

More Related