What does <T extends mean?

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I have seen a method like shown below:

protected  T save( T Acd, boolean en) { 

What does it do? What is these type of method declarations called in Java?

回答1:

It is called a generic method. This whole concept is called "Generics" in Java. That declaration means T can be any type that is subclass of ABC.



回答2:

Bounded Type Parameters:

There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound. Example:

Following example illustrate how extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). This example is Generic method to return the largest of three Comparable objects:

public class MaximumTest {    // determines the largest of three Comparable objects    public static > T maximum(T x, T y, T z)    {                             T max = x; // assume x is initially the largest              if ( y.compareTo( max ) > 0 ){          max = y; // y is the largest so far       }       if ( z.compareTo( max ) > 0 ){          max = z; // z is the largest now                        }       return max; // returns the largest object       }    public static void main( String args[] )    {       System.out.printf( "Max of %d, %d and %d is %d\n\n",                     3, 4, 5, maximum( 3, 4, 5 ) );         System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f\n\n",                    6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );         System.out.printf( "Max of %s, %s and %s is %s\n","pear",          "apple", "orange", maximum( "pear", "apple", "orange" ) );    } } 


回答3:

It means that you must send an ABC object or a child of ABC, no other classes allowed. Also, your Acd variable could use the methods in ABC class that are visible to the class that contians the save method.

This is useful when your T class extends interfaces. For example, you're creating a class that handles object array sorting and this class must implement tne Comparable interface, otherwise the array won't be allowed:

class Class1 implements Comparable {     //attributes, getters and setters...     int x;      //implementing the interface...     public int compareTo(Class1 c1) {         //nice implementation of compareTo         return (this.x > c1.x)? 1 : (this.x > {      public static void insertionSort(T[] array) {         //good implementation of insertion sort goes here...         //just to prove that you can use the methods of the Comparable interface...         array[0].compareTo(array[1]);     }      public static void main(String[] args) {         Class1[] arrC1 = new Class1[5];         Class2[] arrC2 = new Class2[5];         //fill the arrays...         insertionSort(arrC1); //good!         insertionSort(arrC2); //compiler error!     } } 


回答4:

This is a save method which excepts parameter T and boolean type where T must be upper bounded by ABC Class. ABC class or any subclass will be accepted.



回答5:

This is called generics in Java.

Official explanation: In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.

Informally: Strongly typed languages like Java cause more errors show up at compile time instead of runtime. This is a good thing. But it causes code duplication. To mitigate this generics was added to Java.



回答6:

This are generics. Generics with Type Bounds!

See here for refernce



文章来源: What does
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!