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

The "Generic" of the dark horse programmer

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

-- 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! --

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

Network Security

Wechat

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

12
Report