日期:2014-05-18 浏览次数:20768 次
public class Processor { public string name() { return this.GetType().Name; } public Object process(Object input) { return input; } } public class Upcase:Processor { public string process(Object input) { // Covariant return return ((string)input).ToUpper(); } } public class Downcase:Processor { public string process(Object input) { return ((string)input).ToLower(); } } public class Splitter : Processor { public string process(Object input) { // The split() argument divides a String into pieces: string[] arrStr = ((string)input).Split(' '); string str = ""; for (int i = 0; i < arrStr.Length; i++) str = str + arrStr[i]; return str; } } public class Apply { public static void process(Processor p, Object s) { Console.WriteLine("Using Processor " + p.name()); Console.WriteLine(p.process(s)); } public static string s = "Disagreement with beliefs is by definition incorrect"; public static void Main(string[] args) { process(new Upcase(), s); process(new Downcase(), s); process(new Splitter(), s); } }
class ProcessorBase<T> { public virtual T DoProcess() { } } class StringProcessor : ProcessorBase<string> { public override string DoProcess() { } }