Java Generic Chapter 9

  • Firstly we meet compile error when we use the generic type in programming, so we have the generic imported in javaScreen Shot 2016-04-10 at 11.51.03 PM.png
  • Then we define our own generic type, the constructor don’t need the T typeScreen Shot 2016-04-10 at 11.52.44 PM.png
  • When we derive class we have two method, one is to specify the generic type, the other one is to ignore itScreen Shot 2016-04-10 at 11.55.18 PM.pngusing the following method, we will use @SuppressWarning(“unchecked”);
  • Then the key is to understand there is no generic classScreen Shot 2016-04-10 at 11.57.24 PM.png
  • Then if our collections’ value type is not determined, how to do it … ? comesScreen Shot 2016-04-10 at 11.58.38 PM.png
  • but the ? cannot add the element since the type has not been determinedScreen Shot 2016-04-11 at 12.00.19 AM.png
  • The the ? can work with the extends key word. But since we still don’t know the specific type of the collections’ element’s type, so we still cannot add element in our generic containerScreen Shot 2016-04-11 at 12.02.08 AM.png
  • Not only we can set the restriction on passing the parameter, we also can set the restriction when we define a classScreen Shot 2016-04-11 at 12.03.08 AM.png
  • But sometimes we want to be function oriented, so we need our function to be compatible to some other type, so we need set generic method in our program, it is very similar to the cppScreen Shot 2016-04-11 at 12.05.45 AM.png
  • But we still cannot confuse our compilerScreen Shot 2016-04-11 at 12.06.45 AM.pngScreen Shot 2016-04-11 at 12.07.19 AM.png
  • Since the ? can also implement a really high level of generic, so the generic method and ? parameter can implement the same function    —   the key of conversion between these two style is to understand that the ? is generic so lots of different parameter type can get    but some feature will be lost                           First we should understand the difference  Screen Shot 2016-04-11 at 12.11.51 AMScreen Shot 2016-04-11 at 12.10.42 AM.png
  • The type inference of generic type can be used in constructor functionScreen Shot 2016-04-11 at 12.20.58 AM.png
  • Then we will introduce the super key word in java generic — TO MAKE SURE THE TYPE INFORMATION WE NEED IS REALLY CLEAR Screen Shot 2016-04-11 at 12.24.45 AM.png
  • Why we need this specific super grammer, to make the type inference as precise as possibleScreen Shot 2016-04-11 at 12.27.00 AM.png
  • Avoid the pitfall when we have to overload the method Screen Shot 2016-04-11 at 12.28.34 AM.png
  • The type earsion, how to avoid this pitfall when we use the java generic programming Screen Shot 2016-04-11 at 12.32.01 AM.pngScreen Shot 2016-04-11 at 12.32.26 AM.png
  • How to understand the java’s generic design principal understand the upper bound and lower bound the upper bound is extend    THE SUPER IS USED SET THE LOWER BOUND (THAT IS THE SPECIFIC CHILD TYPE)

Leave a comment