to represent type parameters; 2. Define a generic interface and use to represent type parameters; 3. Define a generic method and use to represent type Parameters; 4. When instantiating a generic class or interface, specify specific type parameters; 5. Use wildcards to represent subtypes or supertypes of a certain generic type."/> to represent type parameters; 2. Define a generic interface and use to represent type parameters; 3. Define a generic method and use to represent type Parameters; 4. When instantiating a generic class or interface, specify specific type parameters; 5. Use wildcards to represent subtypes or supertypes of a certain generic type.">

Home >Java >javaTutorial >How to use Java generics

How to use Java generics

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2024-01-26 15:05:25710browse

Java generics mainly include "define generic classes", "define generic interfaces", "define generic methods", "instantiate generic classes or interfaces", "use wildcards" and "use generics". Six uses of "type qualification": 1. Define a generic class and use to represent type parameters; 2. Define a generic interface and use to represent type parameters; 3. Define a generic method , use to represent type parameters; 4. When instantiating a generic class or interface, specify specific type parameters; 5. Use wildcards to represent a subtype or supertype of a certain generic type.

How to use Java generics

Java generics are mainly used in the following ways:

  1. Define generic classes

You can define a generic class, use to indicate type parameters, for example:

public class MyList<T> {
    private T[] array;

    public MyList(T[] array) {
        this.array = array;
    }

    public T get(int index) {
        return array[index];
    }
}
  1. Define a generic interface

You can define a generic interface, use to represent type parameters, for example:

public interface MyInterface<T> {
    T doSomething();
}
  1. Define a generic method

You can define a generic method, use to represent type parameters , for example:

public <T> T doSomething(T param) {
    // ...
}
  1. Instantiate a generic class or interface

When instantiating a generic class or interface, specific type parameters must be specified, for example:

MyList<String> list = new MyList<>(new String[]{"a", "b", "c"});
  1. Use wildcards

You can use wildcards to represent subtypes or supertypes of a certain generic type, including ?, ? extends T and ? super T. , for example:

MyList<? extends Number> list1 = new MyList<>(new Integer[]{1, 2, 3});
MyList<? super Integer> list2 = new MyList<>(new Number[]{1.0, 2.0, 3.0});

Among them, list1 can accept any type that is a subtype of Number (such as Integer, Float, etc.) as an element, while list2 can accept any type that is a supertype of Integer (such as Number, Object, etc.) as elements.

  1. Using generic qualification

You can use generic qualification to limit the scope of type parameters, including extends and super, for example:

public <T extends Number> void doSomething(T param) {
    // ...
}

Among them, indicates that the type parameter T must be a subtype of Number.

The above is the detailed content of How to use Java generics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn