The examples in this article describe the use of delegates in C #. Share to everyone for your reference. The specific analysis is as follows:
For users to find the conditions of the ever-changing, we write conditions to find, it is impossible to write dead, so, if you write a class to let others use, others need not that kind of inquiry, you have to change the conditions.
Then we can make use of this class of people to define a rule (conditions), direct transmission of conditions to you, you help me to query the results, C # can be used to solve the delegate, the corresponding Java can be implemented by interface
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the same as the same as the other, at the same-------------------------- |
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace Findertest {//Sex enumeration public enum genders {male=1,female=2}//Student class public class Student {public Student () {} PU Blic Student (int _id, string _name, genders _gender, DateTime _birthday, String _telephone) {this._id = _id;//student ID this._ name = _name;//Student name this._gender = _gender;//Student Sex this._birthday = _birthday;//student Birthday This._telephone = _telephone;//student Phone number} int _id; public int Id {get {_id;} set {_id = value;}} string _name; public string Name {get {_name;} set {_name = value;}} Genders _gender; Public genders Gender {get {return _gender;} set {_gender = value;}} DateTime _birthday; Public DateTime Birthday {get {_birthday;} set {_birthday = value;}} private String _telephone; public string Telephone {get {_telephone.} set {_telephone = value;}} public void Show () {Console.WriteLine ( String. Format ("My name: {0}/t number: {1}/t sex: {2}", _name,_id,_gender)); } } } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 The |
using System; using System.Collections.Generic; using System.tex T Using System.Collections; namespace Findertest {//semester enum public enum semesters {x1 = 1, x2 = 2, x3 = 3} public delegate bool predicate (Student s); /define a delegate//class class public class Class:arraylist {public class () {} public class (string _name, String _master, semesters _se Mester) {this._name = _name; this._master = _master; this._semester = _semester; _allstudents = new ArrayList ();} privat E string _name;//class name public string name {get {return _name;} set {_name = value;}} private String _master;//monitor Publ IC string Master {get {_master;} set {_master = value;} Private semesters _semester;//semester public SemesteRS Semester {get {_semester;} set {_semester = value;}}//class student collection ArrayList _allstudents; Public ArrayList allstudents {get {return _allstudents.}} public ArrayList findall (predicate match) {if (match = = nul L) {return this._allstudents;} ArrayList result = new ArrayList (); for (int i = 0; i < This._allstudents.count i++) {Student one = (Student) this._allstudents[i]; if (match (one)) {ResU Lt. ADD (one); } return result; } } } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
using System; Using System.Collections.Generic; Using System.Text; Using System.Collections; Namespace Findertest {class Program {static void Main (string[] args) {Class C1 = new Class ("0603", "JSP", semesters.x1) ; Student S1 = new Student (1, "Zs", Genders.male, DateTime.Parse ("1988-02-23"), "13088522635"); Student s2 = new Student (2, "LS", Genders.female, DateTime.Parse ("1986-12-03"), "13188522888"); Student s3 = new Student (3, "ww", Genders.female, DateTime.Parse ("1988-11-15"), "13288576885"); Student S4 = new Student (4, "Zl", Genders.male, DateTime.Parse ("1984-02-21"), "13388534635"); Student S5 = new Student (5, "QQ", Genders.female, DateTime.Parse ("1988-02-23"), "13488524335"); Student s6 = new Student (6, "CB", Genders.male, DaTetime.parse ("1989-02-23"), "13588527636"); C1. Allstudents.add (S1); C1. Allstudents.add (S2); C1. Allstudents.add (S3); C1. Allstudents.add (S4); C1. Allstudents.add (S5); C1. Allstudents.add (S6); ArrayList list= C1. FindAll (match); Find data//ArrayList list = C1 for girls in the class. FindAll (MATCH1); Lookup number from 1 to 5 student foreach (Student s in list) {s.show ();}} The condition is female public static bool match (Student s) {if (S.gender.equals (Genders.female)) {return true;} return false;//condition to learn Number from 1 to 5 public static bool Match1 (Student s) {if (S.id.compareto (1) >= 0 && S.id.compareto (5) <= 0) {return True return false; } } } |
I hope this article will help you with your C # programming.