课程: Java: Advanced Concepts for High-Performance Development

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Bounded generics in Java

Bounded generics in Java

- [Instructor] Generic methods allow us to be flexible about the types that we parse in as arguments. But sometimes you might want a middle ground between flexibility and strictness. So in the previous video, I showed how generics can be used to let the call and order method parse in different types. So I wrote this convert array to list method, which allows people to parse in an array containing any type of objects, and it returns a list containing the same objects of the same type. But I might want to be a little bit stricter. For example, I might want to only let people parse in numbers, and not allow people to parse in arrays of strings, for example. So the solution to this problem is, bounded generics. I can specify that only classes that extend another specific class can be used as the type. So where I've got the T in angle brackets in my method, I can add after the T, extends number. And that means that people will…

内容