Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between using generic masking types in java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what are the differences in the use of generic masking types in java". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the difference in using generic masking types in java?"

Differences in using generic masking types

In the C++ language, there is a good template function to write general versions with parameterized types, so that the compiler can automatically generate specific versions for different types. In the Java language, there is a similar feature called generic. When writing classes and methods, specific types are generally used, and generics can be used to parameterize types so that more generic code can be written.

Many people think that the concepts of C++ template (template) and Java generics (generic) are equivalent, but the implementation mechanisms are completely different. C++ templates are a set of macro instructions, and the compiler creates a copy of the template code for each type; the implementation of Java generics is based on the concept of "type erasure" and is essentially a syntactic sugar for type restriction.

1. Generic class

Taking a support class as an example, define a generic support class for generics:

/ * * General support class * / @ Getter@Setter@ToStringpublic class GenericHolder {/ * General value * / private T value; / * * constructor * / public GenericHolder () {} / * * constructor * / public GenericHolder (T value) {this.value = value;}} 2. Generic interface

Define the data provider interface for generics:

/ * * data provider interface * / public interface DataProvider {/ * * get data function * / public T getData ();} 3. Generic method

Define a shallow copy function for generics:

/ * * shallow copy function * / public static T shallowCopy (Object source, Class clazz) throws BeansException {/ / determine source object if (Objects.isNull (source)) {return null;} / / New target object T target; try {target = clazz.newInstance ();} catch (Exception e) {throw new BeansException ("New Class instance exception", e) } / / copy object properties BeanUtils.copyProperties (source, target); / / return target object return target;} 4. Generic wildcard character

Generic wildcards generally use "?" Instead of specific type arguments, you can set "?" As the parent of all types. When the specific type is uncertain, you can use the generic wildcard "?" When you do not need to use the specific features of the type, but only use the functions in the Object class, you can use the generic wildcard "?" .

In the Java specification, the generic wildcard "?" is not recommended. The above function can be changed to:

/ * * print value function * / public static void printValue (GenericHolder holder) {System.out.println (holder.getValue ());} 5. Upper and lower bounds of generics

When using generics, we can also impose upper and lower bounds on the passed generic type arguments, for example, type arguments are only allowed to pass in a certain type of parent class or a certain type of subclass. The declaration of the upper and lower bounds of generics must be put together with the declaration of generics.

Upper wildcard (extends):

The upper wildcard is "extends" and can accept its specified type or its subclass as a generic parameter. It also has a special form in which it can be specified not only as a subclass of the specified type, but also to implement certain interfaces. For example: List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report