Java's Generics Series - How Class uses generics (with examples)

Original URL: Java's Generics Series - How Class uses generics (with examples)_IT Knives Out Blog-CSDN Blog

Introduction

This article uses examples to introduce the use of generics before methods in Java.

How to write class types

Generic writing of the class corresponding to the object

  • Class classA
  • Class<T> classA
  • Class<?> classB

Class与Class<?>

The ? in Class<?> is a wildcard character, indicating any class that meets the conditions for generic class definition. The effect is basically the same as using Class directly, but it is more standardized and can avoid unnecessary unchecked errors during certain type conversions.

In the JDK, the definition of the ordinary Class.newInstance() method returns Object, and the return type must be cast to another type. With a generic Class<T>, the Class.newInstance() method has a specific return type.

Example

illustrate

In projects, we often encounter situations where List is converted into other types of List, for example: List<User> is converted into List<UserDTO>.

advantage

  1. Convert with one line of code
  2. The bottom layer uses Spring's BeanUtils, which is stable

code

The above is part of the article. To facilitate maintenance, the full text has been transferred to this URL: SpringBoot-Tool Class for Copying Objects-Self-Study Elf

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/128311174