Cognition of generic classes and cognition of generic classes

Source: Internet
Author: User

Cognition of generic classes and cognition of generic classes

According to my understanding, What Is generics? Generic is an open type that can be determined only when compiled, unlike the traditional closed type. According to Baidu encyclopedia, generic is a feature of programming languages. Allow programmers to define variable parts when writing code in a strongly typed programming language, which must be specified before use. Different programming languages have different support for generics from their compilers and runtime environments. A data type that parameterizes types to achieve code reuse and improve the efficiency of software development. A generic class is a reference type and a heap object. It mainly introduces the concept of a type parameter. To deepen understanding, I will use a custom set to explain generics:

If you want to define a custom set, it is nothing more than a class to simulate. The core attribute in the class is an array, and other Members include add, remove, and indexer, code 1 public class IntList 2 {3 int [] arr = new int [2]; 4 int index = 0; 5 public int count = 0; 6 public void Add (int num) 7 {8 if (index> = arr. length) 9 {10 int [] tempArr = new int [arr. length * 2]; 11 arr. copyTo (tempArr, 0); 12 arr = tempArr; 13} 14 arr [index] = num; 15 index ++; 16 count ++; 17} 18 public int this [int index] 19 {20 get21 {22 if (index <arr. length) 23 {24 return arr [index]; 25} 26 else27 {28 throw new Exception ("array out of bounds "); 29} 30} 31 set32 {33 if (index <arr. length) 34 {35 arr [index] = value; 36} 37 else38 {39 throw new Exception ("value assignment index out of bounds"); 40} 41} 42} 43}View Code

From the code, we can see that in the add method, when the index length is greater than or equal to the length of the array, the array will be expanded. In essence, it is to instantiate a new array, copy the meta array data to the new array, and then assign the reference of the new array to the original array. We can check the actual running result.

1 IntList intlist = new IntList (); 2 intlist. add (2); 3 intlist. add (3); 4 intlist. add (4); 5 intlist. add (5); 6 Console. writeLine (intlist. count );View Code

At this time, we have basically completed the User-Defined set function, and so on. This is only a set of the int type. If you want to define the string or other types of set function, what should we do? Do we need to define a class for each type? In fact, this is actually possible, but our dignity as a procedural ape cannot be discarded, so now a savior appears, it is called a generic class, and the code is as follows:

1 public class MyList <T> 2 {3 T [] arr = new T [2]; 4 int index = 0; 5 public int count = 0; 6 public void Add (T num) 7 {8 if (index> = arr. length) 9 {10 T [] tempArr = new T [arr. length * 2]; 11 arr. copyTo (tempArr, 0); 12 arr = tempArr; 13} 14 arr [index] = num; 15 index ++; 16 count ++; 17} 18 public T this [int index] 19 {20 get21 {22 if (index <arr. length) 23 {24 return arr [index]; 25} 26 else27 {28 throw new Exception ("array out of bounds "); 29} 30} 31 set32 {33 if (index <arr. length) 34 {35 arr [index] = value; 36} 37 else38 {39 throw new Exception ("value assignment index out of bounds"); 40} 41} 42} 43}View Code

At first glance, this code seems to be no different from the first code. After a closer look, I found that the int type is missing. Instead, I used a letter T to replace the int type, it seems that there is a little more to define classes. Public class MyList <T>, this T is actually a type placeholder, you can use any character to replace it, but in habit, it will use T to placeholder, so, in general, we are done. Let's take a look at the running results.

 

1 MyList <string> mlist = new MyList <string> (); 2 mlist. Add ("1"); 3 mlist. Add ("2 m ");View Code

 

Well, it seems that all functions are done in general, but is it really over? The answer is No!
Today we will mainly discuss how to use generic classes, instead of simply using them. Below I will expand it in the form of code. That's right, below we will talk about generic type restrictions. First we will define the required classes: 1 public interface IPig2 {3} 4 public class Pig: IPig5 {6} 7 public class SmallPig: pig8 {9}View Code
  • T can only be Value Type 1 public class PigList1 <T> where T: struct // generic type can only be Value Type 2 {3}View Code
  • T can only be reference type 1 public class PigList2 <T> where T: class // generic type can only be reference type 2 {3}View Code
  • T can only be the specified type or the specified type of subclass 1 public class PigList3 <T> where T: Pig // The generic type can only be the specified type or the specified type of subclass 2 {3}View Code
  • T can only be the class 1 public class PigList4 that implements the specified interface <T> where T: IPig // generic type can only be the class that implements the specified interface, or implement subclass 2 {3} of the specified Interface}View Code
In this way, we have a basic understanding of generic classes. I hope you can discuss them with each other. If your experience is limited, it is inevitable that you will be able to get an axe from the class!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.