In actual projects, we often see that some people write a lot of code and abstract many classes. These classes do not use generics or delegation. After applying this technology, some people often tell us that code reusability and coupling are reduced. As a qualified object-oriented programmer, this technology has become a necessary skill, today, I will join you on a relaxing and pleasant trip!
Definition (for more information, see my annotations ):
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.
Note:
Generic is a Black Box. When designing code, we do not need to know what type is in the black box. We are concerned with the processing of the black box. In this way, we are not programming specific objects, we define a set of logic (execution standards), and when we are actually using it, what type is given, it has this logic (execution standards )!
Examples of life:
In our life, the wildcard example has been with us for a long time, that is, express delivery. Let's think about what the focus of express delivery companies is? It's not what we want to mail, but how we choose the path and do our best to save costs, so they define their own express delivery activities, let's take a look at all the parcels as Black Boxes. He planned how the train went, how the ship went, and how the ship went from Beijing to Sichuan, which of the following is the most cost-effective shipping to a courier? the benefit of this definition is that we are shipped from Beijing to Guangdong, just like an item with a proper weight, different geographic boundaries may be different)
Let's take a look at this figure and try again:
Do you think you have a frown! But we still don't fall into idea, because we are programmers and we are used to code communication. Based on this, I made a small example in c # For your reference and criticism:
Instance: Background:
The courier company needs to package the parcel class, and more than N small packages can be placed in the large package. Before shipping, package the packages.
Class description:
BagList ~~~ Large packages, including small packages
Bag ~~~~~~ Packages with specific items
Package code:
<Span style = "font-size: 18px; "> // a courier package warehouse public class bagList <T> {// package class private class bag {// The items in the instance package public bag (T t) {Something = t ;}// recipient address private string Address; public string address {get {return address ;}set {address = value ;}} // private T something; public T Something {get {return something;} set {something = value ;}// the next package private bag Next; public bag next {get {Return next;} set {next = value ;}}// a parcel collection by express delivery-warehouse private bag head; // public bagList () {head = null;} // Add a package to the warehouse public void AddHead (T t) {// instance a package bag Cbag = new bag (t ); // install the new package into the warehouse Cbag. next = head; // the door of the warehouse to the courier head = Cbag;} // method required to retrieve all the packages in the warehouse // The method required by the foreach statement public IEnumerator <T> GetEnumerator () {// instance a package warehouse bag baglist = head; // extracts all packages one by one while (baglist! = Null) {// convert to the iterator type -- to output the result one by one yield return baglist. something; // convert to the object baglist = baglist to be output next. next ;}}</span>
Client code (courier ):
<Span style = "font-size: 18px;"> class Program {static void Main (string [] args) {// an instance of a large package can be loaded with the package bagList received by the courier <int> baglist = new bagList <int> (); // Pack 0-9 items into the large package for (int bag = 0; bag <10; bag ++) {baglist. addHead (bag);} // display 0-9 items in foreach (int I in baglist) {System. console. writeLine (I + "the package has been imported into the warehouse! ") ;}// All packages are packaged. System. Console. WriteLine (" parcel package completed ") ;}</span>
Running result:
Summary:
Someone once said: "Everything is afraid of two serious words ". This sentence is really reasonable now, especially in this study. I found that we will not learn it, but we will not. We will treat it as learning, indulge in extra caution, and think about it, careless and redundant. The biggest manifestation of this learning is that we are wearing the hats of object-oriented programmers, but we do not know, we can not afford to go deep and word-dependent code check! This is my biggest feeling after reading the company code! In our future studies, we still need to learn a little bit!