Use the "delegate" to achieve the object instance

xiaoxiao2021-03-06  109

/ *

Use the "delegate" to achieve the object instance

(I don't know if you understand the topic? Features is still very practical!)

This article realizes examples of the Person class to nest in terms of age, height, weight and other fields!

Just like the SQL query ORDER BY field 1, field 2, ..., field N classes are the same!

* /

Using system;

Class class1 // client

{

Static void main ()

{

Person [] Persons =

{

// Name, age, height, weight

New Person ("Cecilia Cheung", 71,178,49)

New Person ("Zhang Ziyi", 23, 177, 33)

, New Person ("Zhou Xun", 71, 180, 8)

New Person ("Xu Jinglei", 12, 150, 70)

New Person ("Zhao Wei", 23, 166, 60)

New Person ("Song Dandan", 50, 183, 50)

New Person ("Cuihua", 23, 177, 34)

New Person ("Zhao Lirong", 50, 184, 40)

New Person ("Guo Jingjing", 50, 184, 41)

}

/ / "BubblesORTER.comPareop" The element order of the type of type array specifies the order of nested sorting, namely:

// Sort "Age" first, if you press "Height", if you still press "Weight"!

// Note this is just the result of sorting, if you want to edit it according to this idea, there is "recursive", but the level is not determined!

Sorter.compareop [] Personcompareop =

{

/ / You can change the order of elements to change the order of sorting fields

New Sorter.comPareOp (Person.PrevisoldertHannex))

New Sorter.comPareOp (Person.Previshigherthannex)

New sorter.compareop (Person.PrevisheavierthannexT)

}

// Go to the implementation of the Person class and the BubblesORTER class!

Sorter.bubblesort (Persons, PersonCompareop);

For (int i = 0; i

Console.writeline (Persons [i] .tostring ());

Console.readline ();

}

}

Class Person

{

Private string _name;

Private Int_age;

Private int _weight;

PRIVATE INT_HEIGHT;

Public Person (String Name, Int Age, Int Height, int Weight)

{

THIS._NAME = Name;

THIS._AGE = AGE;

THIS._HEIGHT = HEIGHT;

THIS._WEIGHT = Weight;

}

Public override string toString ()

{

Return string.format (_name ", {0: n}, {1: n}, {2: n}", _age, _height, _weight);

}

// The following "additional" defines a number of static methods that match the entrustment signature, for "commissioned calls"

// When the return value is "true", "the previous element <= after one element", otherwise, "false"! // as long as refer to here, we can add the sorting function added to the class and define the sort rules!

// How simple! The rest of the work is left to "Sorting Function (Tools) Class"!

Public Static Bool PrevisoldERTHANNEXT (Object Prev, Object Next)

{

// This must be used here "less than or equal to (<=)", "equal to" is to achieve:

// If the previous rules of the two elements (such as: age) are sorted by the next rule (such as: height)

// If "the previous element <= after one element", return "true", return "false"!

RETURN ((Person) ._ age <= ((person) Next);

}

Public Static Bool PrevishigherthannexT (Object Prev, Object Next)

{

Return ((Person) ._ height <= ((in "next) ._ height);

}

Public Static Bool PrevisheavierthannexT (Object Prev, Object Next)

{

Return ((Person) ._weight <= ((PERSON) Next);

}

}

// "Sort Function (Tools) Class"

Class Sorter

{

// Declaration

Public Delegate Bool CompareOP (Object X, Object Y);

// This sorting basic algorithm is that it is the most common and easy to understand, which belongs to the "exchange" sorting method!

Public static void bubblesort (Object [] a, compareop [] moreethanmethod // MoreThanmethod is sorted in order

{

For (int K = 0; k

{

For (int i = 0; i

{

For (int J = i 1; j

{

BOOL B = true;

For (int m = 0; m <= k; m )

{

// If the two elements "", "the first rule (such as: age)" is equal, according to the next rule (such as: height)

B = B && MoreTHANMETHOD [M] (A [J], A [i]);

}

IF (b) // Conditions for exchange elements

{

// 泡 法 交 元

Object o = a [i];

A [i] = a [j];

A [J] = O;

}

}

}

}

}

}

// Let's talk about it!

转载请注明原文地址:https://www.9cbs.com/read-99425.html

New Post(0)