Get the App
SLTechnology News&Howtos  ›  Network Security  › 

The "Generic" of the dark horse programmer

Shulou Source: shulou.com Published: 2022-06-01 05:54:30 10月03日 Update

-- ASP.Net+Unity development, .net training, looking forward to communicating with you! --

1. Generic 1. Overview

The purpose of generics is to solve the security problem of storing objects in the collection. If different types of objects are stored in the collection, then after reading them, the objects taken out will think that they do not know the type, and there will be security problems. However, this will not prompt errors at compile time, but will cause problems at run time, so generics will move run-time errors to compile time, so errors will be avoided.

two。 Generics in a collection

There are generics in the collection, and generics are also defined in the iterator. after reading it, we also need to specify the type of the iterator, so we don't need to cast the type, because the type is specified in the iterator. so the read object type is the type we specified.

two。 Custom generic classes, which can be specified according to our requirements

3. Methods of generics:

1. A generic type

two。 Multiple generics:

3. Generics on static methods

Define generics between the return value and the modifier

The definition of generics on static methods needs to be defined by itself, and should not be unified with generics on classes, because static methods are generated as soon as the class is loaded, and if they are consistent with generics on classes, there is no object generation when static methods are loaded, and there is no specified generic type, then there will be errors, which are related to the time when the generic definition appears.

Package www.fuxi.jihe

Public class Demo {

Public static void show (Q Q) {/ / do not unify with generics on classes here

System.out.println ("show:" + Q)

}

Public static void main (String [] agrs) {

Demo.show ("hello")

}

}

Results:

Show:hello

4. Generics on interfaces

Here are the generics of the object:

5. Advanced applications of generics: wildcards

For wildcards? Representation, also known as a placeholder, can represent any type

Results:

Abc

one hundred and twenty three

Zhangsan

--

twenty-five

one hundred

two hundred

-- ASP.Net+Unity development, .net training, looking forward to communicating with you! --

Tags: Type object method period static error problem Unification iteration Security result wildcard Communication training Development build compile run different Advanced Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei MySQL Shulou Technology macOS