Template methord

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication3
 7 {
 8     abstract class EvenNumberGenerator
 9     {
10         public void Run(int min, int max)
11         {
12             for (int i = min; i < max; i++)
13             {
14                 if ((i % 2) == 0)
15                 {
16                     ProcessNumber(i);
17                 }
18             }
19         }
20         public abstract void ProcessNumber(int number);
21     }
22     class ConsoleEvenNumberPrint : EvenNumberGenerator
23     {
24         public override void ProcessNumber(int number)
25         {
26             Console.WriteLine("From console print, number{0}", number);
27         }
28     }
29     class FileEvenNumberPrint : EvenNumberGenerator
30     {
31         public override void ProcessNumber(int number)
32         {
33             Console.WriteLine("From File print: number{0}", number);
34         }
35 
36     }
37     class Program
38     {
39         static void Main(string[] args)
40         {
41             ConsoleEvenNumberPrint _consolePrint = new ConsoleEvenNumberPrint();
42             _consolePrint.Run(0, 100);
43             FileEvenNumberPrint _filePrint = new FileEvenNumberPrint();
44             _filePrint.Run(200, 300);
45             Console.ReadKey();
46         }
47     }
48 }

两个继承类都需要一样的方法,所以EvenGenerator被提到父类上做reuse code。记得abstract class不可以instantiate,所以一定要继承类才可以call EventGenerator。但同时两个类的print功能不一样,但是是共性的,所以用abstract方法提到上面,各自的类写自己的实现过程。

考虑:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication5
 7 {
 8     abstract class Person
 9     {
10         public void AddFingerNailToLength()
11         {
12             var numberOfNail = GetFingerNailLength()*5;
13             for (int i = 0; i < numberOfNail; i++)
14             {
15                 Console.WriteLine("Add fingernail {0}", i);
16             }
17         }
18         public int GetFingerNailLength()
19         {
20             return 2;
21         }
22     }
23     class Empoyee : Person
24     {
25         //Empoyee有10个fingernail,要怎么implemented?
26 
27     }
28     class Janitor : Person
29     {
30         //Janitor有3个fingernail,要怎么implemented?
31     }
32     class Program
33     {
34         static void Main(string[] args)
35         {
36             var people = new List<Person>();
37             people.Add(new Empoyee());
38             people.Add(new Janitor());
39             foreach (var item in people)
40             {
41                 item.AddFingerNailToLength();   //这里是对的,我们没有实例化Person对象,而是他的继承对象
42             }
43             Console.ReadKey();
44         }
45     }
46 }

我们要继承类分别有不一样的fingernail该怎么办呢?换成Template methord。由主类来叫一个methord,methord对应不同的继承类不同

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication5
 7 {
 8     abstract class Person
 9     {
10         public void AddFingerNailToLength()
11         {
12             var numberOfNail = GetFingerNailLength()*5;
13             for (int i = 0; i < numberOfNail; i++)
14             {
15                 Console.WriteLine("Add fingernail {0}", i);
16             }
17         }
18         abstract public int GetFingerNailLength();
19        //改之前的这个方法为abstract, 相当于一个继承类的方法的模板了
20     }
21     class Empoyee : Person
22     {
23         public override int GetFingerNailLength()
24         {
25             return 5;
26         }
27 
28     }
29     class Janitor : Person
30     {
31         public override int GetFingerNailLength()
32         {
33             return 1;
34         }
35     }
36     class Program
37     {
38         static void Main(string[] args)
39         {
40             var people = new List<Person>();
41             people.Add(new Empoyee());
42             people.Add(new Janitor());
43             foreach (var item in people)
44             {
45                 item.AddFingerNailToLength();   //这里是对的,我们没有实例化Person对象,而是他的继承对象
46             }
47             Console.ReadKey();
48         }
49     }
50 }
原文地址:https://www.cnblogs.com/shawnzxx/p/3032977.html