上QQ阅读APP看书,第一时间看更新
How it works...
The first class we created, StackInt, is a stack that can work only with the Int type. It's good if you are going to use it with Int type only. However, a famous data structure like it can be used with different types, such as String or Double. It's not possible to create different stack class for each type, and here comes the magic of generics; instead we created another class called Stack marked with <T> to say it's going to deal with the generic type T, which can be Int, String, Double, and so on. Then, we create two stack instances, stackOfStrings and stackOfInt, and both share the same code as their class is built with generics.