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

How to use generics in C language to achieve code reuse

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

Share

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

This article mainly introduces how to use generics in C language to achieve the purpose of code reuse, the article is very detailed, has a certain reference value, interested friends must read!

With generics, you regain the commonality of the linked list code (implemented only once for all types), and when you initialize the list you tell it what types it accepts. This implementation is pretty simple, so let's go back to the Node class:

public class Node{ Object data; ...

Note that the type of data is Object,(in Employee Node it is Employee). We'll turn it into a generic (usually, represented by a capital T). We also define the Node class to indicate that it can be generalized to accept a T type.

public class Node

< T>

{ T data; ...

Read as: Node of type T. T represents the type that Node accepts when it is initialized. T can be Object, integer or Employee. This can only be determined when Node is initialized.

Note: Using T as the logo is just a convention, you can use other letter combinations instead, such as this:

public class Node

< UnknownType>

{ UnknownType data; ...

By using T as the unknown type, the next field (the reference to the next node) must be declared as a Node of type T (meaning that it accepts a generic Node of type T).

Node

< T>

next;

The constructor takes a simple argument of type T:

public Node(T data) { this.data = data; this.next = null; }

The rest of the Node class is pretty simple, everything you need to use Object, you now need to use T. The LinkedList class now accepts a Node of type T instead of a simple Node as the head node.

public class LinkedList

< T>

{ Node

< T>

headNode = null;

Again, the conversion is straightforward. Anywhere you need to use Object, now T, anywhere you need Node, now Node.

< T>

。The following code initializes two linked tables. One is integer.

LinkedList

< int>

ll = new LinkedList

< int>

();

The other is of type Employee:

LinkedList

< Employee>

employees = new LinkedList

< Employee>

();

The rest of the code is the same as the *** versions, except that there is no boxing, unpacking, and it is impossible to save the wrong type to the collection.

LinkedList

< int>

ll = new LinkedList

< int>

(); for ( int i = 0; i

< 10; i ++ ) { ll.Add(i); } Console.WriteLine(ll); Console.WriteLine(" Done."); LinkedList< Employee>

employees = new LinkedList

< Employee>

(); employees.Add(new Employee("John")); employees.Add(new Employee("Paul")); employees.Add(new Employee("George")); employees.Add(new Employee("Ringo")); Console.WriteLine(employees); Console.WriteLine(" Done. "); Console.WriteLine("The fourth integer is " + ll[3]); Employee d = employees[1]; Console.WriteLine("The second Employee is " + d);

Generics allow you to implement type-safe collections without copying/pasting lengthy code. And, because of that, the generic runtime is extended to special types. The Just In Time compiler allows code to be shared between different instances, *** which significantly reduces the code you need to write.

Thus, using generics achieves the goal of code reuse.

The above is "C language how to use generics to achieve the purpose of code reuse" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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