class Program { public delegate int Run(int i, int j); public static void Main(string[] args) { MainFunction(Add); MainFunction(Delete); MainFunction(Ride); MainFunction(Divide); } public static void MainFunction(Run r) { // do someThing r(10, 10); // do someThing } public static int Add(int i, int j) { return i + j; } public static int Delete(int i, int j) { return i - j; } public static int Ride(int i, int j) { return i * j; } public static int Divide(int i, int j) { return i * j; } }
简单的代码 例子。
复杂的写法
class Program { public delegate void Run(int i, int j); public static void Main(string[] args) { MainFunction(Add); MainFunction(Delete); MainFunction(Ride); MainFunction(Divide); } public static void MainFunction(Run r) { r(10, 10); // do someThing } public static void Add(int i, int j) { // do someThing var r= i + j; // do someThing } public static void Delete(int i, int j) { // do someThing var r = i - j; // do someThing } public static void Ride(int i, int j) { // do someThing var r = i * j; // do someThing } public static void Divide(int i, int j) { // do someThing var r = i * j; // do someThing } }
看到了吧,do something 要复制 这么多次。。。 这就是封装。面向对象哦。
时间: 2024-10-18 21:08:00