模式设计(二)Factory Method(完整)

一、 工厂方法(Factory Method)模式

工厂方法(FactoryMethod)模式是类的创建模式,其用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。在工厂方法模式中,核心的工厂类不再负责所有产品的创建,而是将具体创建工作交给子类去做。这个核心类仅仅负责给出具体工厂必须实现的接口,而不接触哪一个产品类被实例化这种细节。这使得工厂方法模式可以允许系统在不修改工厂角色的情况下引进新产品。在Factory Method模式中,工厂类与产品类往往具有平行的等级结构,它们之间一一对应。

 

抽象工厂(Creator)角色:是工厂方法模式的核心,与应用程序无关。任何在模式中创建的对象的工厂类必须实现这个接口,这里为Evolution抽象类。

具体工厂(Concrete Creator)角色:这是实现抽象工厂接口的具体工厂类,包含与应用程序密切相关的逻辑,并且受到应用程序调用以创建产品对象。这里为EvolutionToYellow,EvolutionToWirte,EvolutionToBlack三个类。

抽象产品(Product)角色:工厂方法模式所创建的对象的超类型,也就是产品对象的共同父类或共同拥有的接口。这里为Person抽象类。

具体产品(Concrete Product)角色:这个角色实现了抽象产品角色所定义的接口。某具体产品有专门的具体工厂创建,它们之间往往一一对应,这里为YellowPerson,WritePerson,BlackPerson 三个类。

具体代码(C#):

  1 None.gif using  System;
  2 None.gif
  3 None.gif namespace  Factory_Method
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
  6InBlock.gif    ///============== Program Description==============
  7InBlock.gif    ///Name:FactoryMethod.cs
  8InBlock.gif    ///Objective:FactoryMethod
  9InBlock.gif    ///Date:2006-02-09 
 10InBlock.gif    ///Written By coffee.liu
 11InBlock.gif    ///================================================
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    class Class1
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**//**//// <summary>
 16InBlock.gif        /// 应用程序的主入口点。
 17ExpandedSubBlockEnd.gif        /// </summary>

 18InBlock.gif        [STAThread]
 19InBlock.gif        static void Main(string[] args)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            Evolution yev=new EvolutionToYellow();
 22InBlock.gif            Evolution bev=new EvolutionToBlack();
 23InBlock.gif            Evolution wev=new EvolutionToWrite();
 24InBlock.gif            Person yp=yev.evolution();
 25InBlock.gif            yp.GetInfo();
 26InBlock.gif            yp.Say();
 27InBlock.gif            Console.WriteLine(".");
 28InBlock.gif            Person bp=bev.evolution();
 29InBlock.gif            bp.GetInfo();
 30InBlock.gif            bp.Say();
 31InBlock.gif            Console.WriteLine(".");
 32InBlock.gif            Person wp=wev.evolution();
 33InBlock.gif            wp.GetInfo();
 34InBlock.gif            wp.Say();
 35ExpandedSubBlockEnd.gif        }

 36ExpandedSubBlockEnd.gif    }

 37InBlock.gif    public abstract class Person
 38ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 39InBlock.gif        protected string sex,race;
 40InBlock.gif        public string GetSex()
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            return sex;
 43ExpandedSubBlockEnd.gif        }

 44InBlock.gif        public string GetRace()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            return race;
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif        public abstract void GetInfo();
 49InBlock.gif        public abstract void Say();
 50ExpandedSubBlockEnd.gif    }

 51InBlock.gif
 52InBlock.gif    public class YellowPerson:Person
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif        public YellowPerson()
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            sex="Man";
 57InBlock.gif            race="Yellow";
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif        public YellowPerson(string Ysex)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            sex=Ysex;
 62InBlock.gif            race="Yellow";
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif        public override void GetInfo()
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif        public override void Say()
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            Console.WriteLine("I am a Yellow Baby");
 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73ExpandedSubBlockEnd.gif    }

 74InBlock.gif
 75InBlock.gif    public class BlackPerson:Person
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 77InBlock.gif        public BlackPerson()
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            sex="Man";
 80InBlock.gif            race="Black";
 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif        public BlackPerson(string Bsex)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            sex=Bsex;
 85InBlock.gif            race="Black";
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif        public override void GetInfo()
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif        public override void Say()
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif            Console.WriteLine("I am a Black Baby");
 94ExpandedSubBlockEnd.gif        }

 95ExpandedSubBlockEnd.gif    }

 96InBlock.gif
 97InBlock.gif    public class WritePerson:Person
 98ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 99InBlock.gif        public WritePerson()
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            sex="Man";
102InBlock.gif            race="Write";
103ExpandedSubBlockEnd.gif        }

104InBlock.gif        public WritePerson(string Wsex)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            sex=Wsex;
107InBlock.gif            race="Write";
108ExpandedSubBlockEnd.gif        }

109InBlock.gif        public override void GetInfo()
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
112ExpandedSubBlockEnd.gif        }

113InBlock.gif        public override void Say()
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            Console.WriteLine("I am a Write Baby");
116ExpandedSubBlockEnd.gif        }

117ExpandedSubBlockEnd.gif    }

118ExpandedSubBlockStart.gifContractedSubBlock.gif    public abstract class Evolutiondot.gif{
119InBlock.gif         public abstract Person evolution();
120ExpandedSubBlockEnd.gif    }

121InBlock.gif    public class EvolutionToYellow:Evolution
122ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
123InBlock.gif        public override Person evolution()
124ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
125InBlock.gif            return new YellowPerson();
126ExpandedSubBlockEnd.gif        }

127InBlock.gif
128ExpandedSubBlockEnd.gif    }

129InBlock.gif    public class EvolutionToBlack:Evolution
130ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
131InBlock.gif        public override Person evolution()
132ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
133InBlock.gif            return new BlackPerson();
134ExpandedSubBlockEnd.gif        }

135ExpandedSubBlockEnd.gif    }

136InBlock.gif    public class EvolutionToWrite:Evolution
137ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
138InBlock.gif        public override Person evolution()
139ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
140InBlock.gif            return new WritePerson();
141ExpandedSubBlockEnd.gif        }

142ExpandedSubBlockEnd.gif    }

143InBlock.gif    
144ExpandedBlockEnd.gif}

145 None.gif


程序代码(Pascal)
  1 None.gif program FactoryMethod;
  2 None.gif      //==============  Program Description ==============
  3 None.gif     // Name:FactoryMethod.dpr
  4 None.gif     // Objective:FactoryMethod
  5 None.gif     // Date : 2006 - 04 - 23
  6 None.gif     // Written By coffee.liu
  7 None.gif     //================================================
  8 None.gif{$APPTYPE CONSOLE}
  9 None.gif
 10 None.gifuses
 11 None.gif  SysUtils;
 12 ExpandedBlockStart.gifContractedBlock.gif  type Person = Class protectedclass Class protectedclass
 13InBlock.gif       protected
 14InBlock.gif        sex:string;
 15InBlock.gif        race:string;
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function GetSex()Function GetSex()Function GetSex()function GetSex():string;
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function GetRace()Function GetRace()Function GetRace()function GetRace():string;
 18InBlock.gif        public procedure GetInfo;virtual;abstract;
 19InBlock.gif        public procedure say;virtual;abstract;
 20InBlock.gif
 21InBlock.gif   end;
 22ExpandedSubBlockStart.gifContractedSubBlock.gif   type YellowPerson=Class (Class (Class (class(Person)
 23InBlock.gif       constructor YellowPerson();
 24InBlock.gif        public procedure GetInfo;override;
 25InBlock.gif        public procedure Say;override;
 26InBlock.gif     end;
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    type BlackPerson=Class (Class (Class (class(Person)
 28InBlock.gif       constructor BlackPerson();
 29InBlock.gif        public procedure GetInfo;override;
 30InBlock.gif        public procedure Say;override;
 31InBlock.gif     end;
 32ExpandedSubBlockStart.gifContractedSubBlock.gif    type WritePerson=Class (Class (Class (class(Person)
 33InBlock.gif       constructor WritePerson();
 34InBlock.gif        public procedure GetInfo;override;
 35InBlock.gif        public procedure Say;override;
 36InBlock.gif     end;
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    type Evolution=Class publicclassClass publicclass
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function evolution()Function evolution()Function evolution()function  evolution():Person;virtual;abstract;
 39InBlock.gif      end;
 40ExpandedSubBlockStart.gifContractedSubBlock.gif    type EvolutionToYellow=Class (Class (Class (class(Evolution)
 41InBlock.gif        constructor  EvolutionToYellow();
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function evolution()Function evolution()Function evolution()function evolution():Person;override;
 43InBlock.gif     end;
 44ExpandedSubBlockStart.gifContractedSubBlock.gif     type EvolutionToBlack=Class (Class (Class (class(Evolution)
 45InBlock.gif        constructor EvolutionToBlack();
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function evolution()Function evolution()Function evolution()function evolution():Person;override;
 47InBlock.gif     end;
 48ExpandedSubBlockStart.gifContractedSubBlock.gif     type EvolutionToWrite=Class (Class (Class (class(Evolution)
 49InBlock.gif        constructor EvolutionToWrite();
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function evolution()Function evolution()Function evolution()function evolution():Person;override;
 51InBlock.gif     end;
 52InBlock.gif{ Person }
 53InBlock.gif
 54ExpandedSubBlockStart.gifContractedSubBlock.gifFunction Person()Function Person()Function Person()function Person.GetRace: string;
 55InBlock.gifbegin
 56InBlock.gif result:=race;
 57InBlock.gifend;
 58InBlock.gif
 59ExpandedSubBlockStart.gifContractedSubBlock.gifFunction Person()Function Person()Function Person()function Person.GetSex: string;
 60InBlock.gifbegin
 61InBlock.gif  result:=sex;
 62InBlock.gifend;
 63InBlock.gif
 64InBlock.gif{ YellowPerson }
 65InBlock.gif
 66InBlock.gifconstructor YellowPerson.YellowPerson;
 67InBlock.gifbegin
 68InBlock.gif            sex:='Man';
 69InBlock.gif            race:='Yellow';
 70InBlock.gifend;
 71InBlock.gifprocedure YellowPerson.GetInfo;
 72InBlock.gif   begin
 73InBlock.gif    inherited;
 74InBlock.gif      WriteLn('the '+race+' Person Info:'+sex);
 75InBlock.gif   end;
 76InBlock.gifprocedure YellowPerson.Say;
 77InBlock.gifbegin
 78InBlock.gif  inherited;
 79InBlock.gif      WriteLn('I am a Yellow Baby');
 80InBlock.gifend;
 81InBlock.gif
 82InBlock.gif{ WritePerson }
 83InBlock.gif
 84InBlock.gifprocedure WritePerson.GetInfo;
 85InBlock.gifbegin
 86InBlock.gif  inherited;
 87InBlock.gif     WriteLn('the '+race+' Person Info:'+sex);
 88InBlock.gifend;
 89InBlock.gif
 90InBlock.gifprocedure WritePerson.Say;
 91InBlock.gifbegin
 92InBlock.gif  inherited;
 93InBlock.gif      WriteLn('I am a Write Baby');
 94InBlock.gifend;
 95InBlock.gif
 96InBlock.gifconstructor WritePerson.WritePerson;
 97InBlock.gifbegin
 98InBlock.gif            sex:='Man';
 99InBlock.gif            race:='Write';
100InBlock.gifend;
101InBlock.gif
102InBlock.gif{ BlackPerson }
103InBlock.gif
104InBlock.gifconstructor BlackPerson.BlackPerson;
105InBlock.gifbegin
106InBlock.gif            sex:='Man';
107InBlock.gif            race:='Black';
108InBlock.gifend;
109InBlock.gif
110InBlock.gifprocedure BlackPerson.GetInfo;
111InBlock.gifbegin
112InBlock.gif  inherited;
113InBlock.gif      WriteLn('the '+race+' Person Info:'+sex);
114InBlock.gifend;
115InBlock.gif
116InBlock.gifprocedure BlackPerson.Say;
117InBlock.gifbegin
118InBlock.gif  inherited;
119InBlock.gif     WriteLn('I am a Black Baby');
120InBlock.gifend;
121InBlock.gif
122InBlock.gif{ EvolutionToBlack }
123InBlock.gif
124ExpandedSubBlockStart.gifContractedSubBlock.gifFunction EvolutionToBlack()Function EvolutionToBlack()Function EvolutionToBlack()function EvolutionToBlack.evolution: Person;
125InBlock.gifbegin
126InBlock.gif    result:=BlackPerson.BlackPerson;
127InBlock.gifend;
128InBlock.gif
129InBlock.gifconstructor EvolutionToBlack.EvolutionToBlack;
130InBlock.gifbegin
131InBlock.gif
132InBlock.gifend;
133InBlock.gif
134InBlock.gif{ EvolutionToWrite }
135InBlock.gif
136ExpandedSubBlockStart.gifContractedSubBlock.gifFunction EvolutionToWrite()Function EvolutionToWrite()Function EvolutionToWrite()function EvolutionToWrite.evolution: Person;
137InBlock.gifbegin
138InBlock.gif   result:=WritePerson.WritePerson;
139InBlock.gifend;
140InBlock.gif
141InBlock.gifconstructor EvolutionToWrite.EvolutionToWrite;
142InBlock.gifbegin
143InBlock.gif
144InBlock.gifend;
145InBlock.gif
146InBlock.gif{ EvolutionToYellow }
147InBlock.gif
148ExpandedSubBlockStart.gifContractedSubBlock.gifFunction EvolutionToYellow()Function EvolutionToYellow()Function EvolutionToYellow()function EvolutionToYellow.evolution: Person;
149InBlock.gifbegin
150InBlock.gif     result:=YellowPerson.YellowPerson;
151InBlock.gifend;
152InBlock.gif
153InBlock.gif
154InBlock.gif
155InBlock.gifconstructor EvolutionToYellow.EvolutionToYellow;
156InBlock.gifbegin
157InBlock.gif
158InBlock.gifend;
159InBlock.gifvar
160InBlock.gifyev,wev,bev:Evolution;
161InBlock.gifyp,wp,bp:Person;
162InBlock.gifbegin
163InBlock.gif    yev:=EvolutionToYellow.EvolutionToYellow;
164InBlock.gif    yp:=yev.evolution;
165InBlock.gif    wev:=EvolutionToWrite.EvolutionToWrite;
166InBlock.gif    wp:=wev.evolution;
167InBlock.gif    bev:=EvolutionToBlack.EvolutionToBlack;
168InBlock.gif    bp:=bev.evolution;
169InBlock.gif    yp.GetInfo;
170InBlock.gif    yp.say;
171InBlock.gif     WriteLn('..');
172InBlock.gif    wp.GetInfo;
173InBlock.gif    wp.say;
174InBlock.gif     WriteLn('..');
175InBlock.gif    bp.GetInfo;
176InBlock.gif    bp.say;
177InBlock.gif
178InBlock.gifend.
179InBlock.gif

何时使用工厂方法:
1.一个类无法预测它要创建的对象属于哪个类。
2.一个类用他的子类来指定所创建的对象。
3.把要创建哪个类的信息局部化的时候。
对于实现工厂模式需要注意的问题:
1.基类是一个抽象类或一个接口,模式必须返回一个完整的可工作的类。
2.基类包含默认方法,除非默认方法不能胜任,才会调用这些方法。
3.可以将参数传递给工厂,告诉工厂返回哪个类型的类。这种情况下,类可以共享相同的方法名,但完成的工作可以不同。
我们甚至可以将上面的代码变得更复杂一点:

代码如下:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif
  4 None.gif namespace  FactoryMethodPlus2
  5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
  7InBlock.gif    ///============== Program Description==============
  8InBlock.gif    ///Name:FactoryMethodPlus2.cs
  9InBlock.gif    ///Objective:FactoryMethodPlus2
 10InBlock.gif    ///Date:2006-04-23 
 11InBlock.gif    ///Written By coffee.liu
 12InBlock.gif    ///================================================
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    class Class1
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**//**//// <summary>
 17InBlock.gif        /// 应用程序的主入口点。
 18ExpandedSubBlockEnd.gif        /// </summary>

 19InBlock.gif        [STAThread]
 20InBlock.gif        static void Main(string[] args)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            Evolution[] evs=new Evolution[5];
 23InBlock.gif            evs[0]=new EvolutionToYellow();
 24InBlock.gif            evs[1]=new EvolutionToBlack();
 25InBlock.gif            evs[2]=new EvolutionToWrite();
 26InBlock.gif            evs[3]=new EolutionToAsia();
 27InBlock.gif            evs[4]=new EolutionToNorthAmerica();
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            foreach(Evolution ev in evs )dot.gif{
 29InBlock.gif            Console.WriteLine("..");
 30ExpandedSubBlockStart.gifContractedSubBlock.gif                foreach(Person ps in ev.Persons)dot.gif{
 31InBlock.gif                //ps.GetInfo();
 32InBlock.gif                    ps.Say();
 33ExpandedSubBlockEnd.gif                }

 34ExpandedSubBlockEnd.gif            }

 35InBlock.gif
 36ExpandedSubBlockEnd.gif        }

 37ExpandedSubBlockEnd.gif    }

 38InBlock.gif    public abstract class Person
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 40InBlock.gif        protected string sex,race;
 41InBlock.gif        public string GetSex()
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            return sex;
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif        public string GetRace()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            return race;
 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif        public abstract void GetInfo();
 50InBlock.gif        public abstract void Say();
 51ExpandedSubBlockEnd.gif    }

 52InBlock.gif
 53InBlock.gif    public class YellowPerson:Person
 54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 55InBlock.gif        public YellowPerson()
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57InBlock.gif            sex="Man";
 58InBlock.gif            race="Yellow";
 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif        public YellowPerson(string Ysex)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            sex=Ysex;
 63InBlock.gif            race="Yellow";
 64ExpandedSubBlockEnd.gif        }

 65InBlock.gif        public override void GetInfo()
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif        public override void Say()
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            Console.WriteLine("I am a Yellow Baby");
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74ExpandedSubBlockEnd.gif    }

 75ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
 76InBlock.gif    /// 黄种人分布
 77ExpandedSubBlockEnd.gif    /// </summary>

 78InBlock.gif    public class AsiaYellowPerson:YellowPerson
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 80InBlock.gif        private string state="Asia";
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Statedot.gif{
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        getdot.gif{return state;}
 83ExpandedSubBlockEnd.gif        }

 84InBlock.gif        public AsiaYellowPerson():base()
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 86InBlock.gif        public AsiaYellowPerson(string Ysex):base(Ysex)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 88InBlock.gif        public override void Say()
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            Console.WriteLine("I am a Yellow Baby ,My state in Asia");
 91ExpandedSubBlockEnd.gif        }

 92ExpandedSubBlockEnd.gif    }

 93InBlock.gif    
 94InBlock.gif    public class NorthAmericaYellowPerson:YellowPerson
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif        private string state="NorthAmerica";
 97InBlock.gif        public string State
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
100ExpandedSubBlockEnd.gif        }

101InBlock.gif            public NorthAmericaYellowPerson():base()
102ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
103InBlock.gif        public NorthAmericaYellowPerson(string Ysex):base(Ysex)
104ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
105InBlock.gif        public override void Say()
106ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
107InBlock.gif            Console.WriteLine("I am a Yellow Baby ,My state in NorthAmerica");
108ExpandedSubBlockEnd.gif        }

109ExpandedSubBlockEnd.gif        }

110InBlock.gif    public class SouthAmericaYellowPerson:YellowPerson
111ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
112InBlock.gif        private string state="SouthAmerica";
113InBlock.gif        public string State
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
116ExpandedSubBlockEnd.gif        }

117InBlock.gif            public SouthAmericaYellowPerson():base()
118ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
119InBlock.gif        public SouthAmericaYellowPerson(string Ysex):base(Ysex)
120ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
121InBlock.gif        public override void Say()
122ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
123InBlock.gif            Console.WriteLine("I am a Yellow Baby ,My state in SouthAmerica");
124ExpandedSubBlockEnd.gif        }

125ExpandedSubBlockEnd.gif        }

126InBlock.gif    public class AustrialiaYellowPerson:YellowPerson
127ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
128InBlock.gif        private string state="Austrialia";
129InBlock.gif        public string State
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
132ExpandedSubBlockEnd.gif        }

133InBlock.gif            public AustrialiaYellowPerson():base()
134ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
135InBlock.gif        public AustrialiaYellowPerson(string Ysex):base(Ysex)
136ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
137InBlock.gif        public override void Say()
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            Console.WriteLine("I am a Yellow Baby ,My state in Austrialia");
140ExpandedSubBlockEnd.gif        }

141ExpandedSubBlockEnd.gif        }

142InBlock.gif    
143InBlock.gif    public class BlackPerson:Person
144ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
145InBlock.gif        public BlackPerson()
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            sex="Man";
148InBlock.gif            race="Black";
149ExpandedSubBlockEnd.gif        }

150InBlock.gif        public BlackPerson(string Bsex)
151ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
152InBlock.gif            sex=Bsex;
153InBlock.gif            race="Black";
154ExpandedSubBlockEnd.gif        }

155InBlock.gif        public override void GetInfo()
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
158ExpandedSubBlockEnd.gif        }

159InBlock.gif        public override void Say()
160ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
161InBlock.gif            Console.WriteLine("I am a Black Baby");
162ExpandedSubBlockEnd.gif        }

163ExpandedSubBlockEnd.gif    }

164ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
165InBlock.gif    /// 黑人分布
166ExpandedSubBlockEnd.gif    /// </summary>

167InBlock.gif    public class EuropeBlackPerson:BlackPerson
168ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
169InBlock.gif            private string state="Europe";
170InBlock.gif        public string State
171ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
172ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
173ExpandedSubBlockEnd.gif        }

174InBlock.gif            public EuropeBlackPerson():base()
175ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
176InBlock.gif        public EuropeBlackPerson(string Ysex):base(Ysex)
177ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
178InBlock.gif        public override void Say()
179ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
180InBlock.gif            Console.WriteLine("I am a Black Baby ,My state in Europe");
181ExpandedSubBlockEnd.gif        }

182ExpandedSubBlockEnd.gif        }

183InBlock.gif    public class AfricaBlackPerson:BlackPerson
184ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
185InBlock.gif            private string state="Africa";
186InBlock.gif        public string State
187ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
188ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
189ExpandedSubBlockEnd.gif        }

190InBlock.gif            public AfricaBlackPerson():base()
191ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
192InBlock.gif        public AfricaBlackPerson(string Ysex):base(Ysex)
193ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
194InBlock.gif        public override void Say()
195ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
196InBlock.gif            Console.WriteLine("I am a Black Baby ,My state in Africa");
197ExpandedSubBlockEnd.gif        }

198ExpandedSubBlockEnd.gif        }

199InBlock.gif    
200InBlock.gif    
201InBlock.gif    public class WritePerson:Person
202ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
203InBlock.gif        public WritePerson()
204ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
205InBlock.gif            sex="Man";
206InBlock.gif            race="Write";
207ExpandedSubBlockEnd.gif        }

208InBlock.gif        public WritePerson(string Wsex)
209ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
210InBlock.gif            sex=Wsex;
211InBlock.gif            race="Write";
212ExpandedSubBlockEnd.gif        }

213InBlock.gif        public override void GetInfo()
214ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
215InBlock.gif            Console.WriteLine("the "+race+" Person Info:"+sex); 
216ExpandedSubBlockEnd.gif        }

217InBlock.gif        public override void Say()
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219InBlock.gif            Console.WriteLine("I am a Write Baby");
220ExpandedSubBlockEnd.gif        }

221ExpandedSubBlockEnd.gif    }

222ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
223InBlock.gif    ///白人分布
224ExpandedSubBlockEnd.gif    /// </summary>

225InBlock.gif
226InBlock.gif    public class EuropeWritePerson:WritePerson
227ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
228InBlock.gif            private string state="Europe";
229InBlock.gif        public string State
230ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
231ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
232ExpandedSubBlockEnd.gif        }

233InBlock.gif            public EuropeWritePerson():base()
234ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
235InBlock.gif        public EuropeWritePerson(string Ysex):base(Ysex)
236ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
237InBlock.gif        public override void Say()
238ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
239InBlock.gif            Console.WriteLine("I am a Write Baby ,My state in Europe");
240ExpandedSubBlockEnd.gif        }

241ExpandedSubBlockEnd.gif        }

242InBlock.gif    public class NorthAmericaWritePerson:WritePerson
243ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
244InBlock.gif            private string state="NorthAmerica";
245InBlock.gif        public string State
246ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
247ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
248ExpandedSubBlockEnd.gif        }

249InBlock.gif            public NorthAmericaWritePerson():base()
250ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
251InBlock.gif        public NorthAmericaWritePerson(string Ysex):base(Ysex)
252ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
253InBlock.gif        public override void Say()
254ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
255InBlock.gif            Console.WriteLine("I am a Write Baby ,My state in NorthAmerica");
256ExpandedSubBlockEnd.gif        }

257ExpandedSubBlockEnd.gif        }

258InBlock.gif    public class SouthAmericaWritePerson:WritePerson
259ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
260InBlock.gif            private string state="SouthAmerica";
261InBlock.gif        public string State
262ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
263ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
264ExpandedSubBlockEnd.gif        }

265InBlock.gif            public SouthAmericaWritePerson():base()
266ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
267InBlock.gif        public SouthAmericaWritePerson(string Ysex):base(Ysex)
268ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
269InBlock.gif        public override void Say()
270ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
271InBlock.gif            Console.WriteLine("I am a Write Baby ,My state in SouthAmerica");
272ExpandedSubBlockEnd.gif        }

273ExpandedSubBlockEnd.gif        }

274InBlock.gif    public class OrangutanWritePerson:WritePerson
275ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
276InBlock.gif        private string state="Orangutan";
277InBlock.gif        public string State
278ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
279ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return state;}
280ExpandedSubBlockEnd.gif        }

281InBlock.gif            public OrangutanWritePerson():base()
282ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
283InBlock.gif        public OrangutanWritePerson(string Ysex):base(Ysex)
284ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{}
285InBlock.gif        public override void Say()
286ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
287InBlock.gif            Console.WriteLine("I am a . Baby ,I am a Orangutan,I like banananananana..");
288ExpandedSubBlockEnd.gif        }

289ExpandedSubBlockEnd.gif        }

290InBlock.gif
291ExpandedSubBlockStart.gifContractedSubBlock.gif/**//**//**//// <summary>
292InBlock.gif/// 进化
293ExpandedSubBlockEnd.gif/// </summary>

294InBlock.gif    public abstract class Evolution
295ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
296ExpandedSubBlockStart.gifContractedSubBlock.gif        public Evolution()dot.gif{
297InBlock.gif         this.evolution();
298ExpandedSubBlockEnd.gif        }

299InBlock.gif        protected ArrayList persons=new ArrayList();
300InBlock.gif        public abstract void evolution();
301InBlock.gif        public ArrayList Persons
302ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
303ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return persons;}
304ExpandedSubBlockEnd.gif        }

305ExpandedSubBlockEnd.gif    }

306InBlock.gif    public class EvolutionToYellow:Evolution
307ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
308InBlock.gif        
309InBlock.gif        public override void evolution()
310ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
311InBlock.gif            persons.Add(new AsiaYellowPerson());
312InBlock.gif            persons.Add(new NorthAmericaYellowPerson());
313InBlock.gif            persons.Add(new SouthAmericaYellowPerson());
314InBlock.gif            persons.Add(new AustrialiaYellowPerson());
315ExpandedSubBlockEnd.gif        }

316ExpandedSubBlockEnd.gif    }

317InBlock.gif    public class EvolutionToBlack:Evolution
318ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
319InBlock.gif        
320InBlock.gif        public override void evolution()
321ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
322InBlock.gif            persons.Add(new EuropeBlackPerson());
323InBlock.gif            persons.Add(new AfricaBlackPerson());
324ExpandedSubBlockEnd.gif        }
                
325ExpandedSubBlockEnd.gif    }

326InBlock.gif    public class EvolutionToWrite:Evolution
327ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
328InBlock.gif        public override void evolution()
329ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
330InBlock.gif            persons.Add(new EuropeWritePerson());
331InBlock.gif            persons.Add(new NorthAmericaWritePerson());
332InBlock.gif            persons.Add(new SouthAmericaWritePerson());
333InBlock.gif            persons.Add(new OrangutanWritePerson());
334ExpandedSubBlockEnd.gif        }
        
335ExpandedSubBlockEnd.gif    }

336ExpandedSubBlockStart.gifContractedSubBlock.gif    public class EolutionToAsia:Evolutiondot.gif{
337ExpandedSubBlockStart.gifContractedSubBlock.gif        public override void evolution()dot.gif{
338InBlock.gif           persons.Add(new AsiaYellowPerson()); 
339ExpandedSubBlockEnd.gif        }

340ExpandedSubBlockEnd.gif    }

341InBlock.gif    public class EolutionToNorthAmerica:Evolution
342ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
343InBlock.gif        public override void evolution()
344ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
345InBlock.gif            persons.Add(new NorthAmericaYellowPerson());
346InBlock.gif            persons.Add(new NorthAmericaWritePerson());
347ExpandedSubBlockEnd.gif        }

348ExpandedSubBlockEnd.gif    }

349ExpandedBlockEnd.gif}

350 None.gif

程序代码(Pascal):
  1 None.gif program FactoryMethodPlus1;
  2 None.gif      //==============  Program Description ==============
  3 None.gif     // Name:FactoryMethodPlus1.dpr
  4 None.gif     // Objective:FactoryMethodPlus1
  5 None.gif     // Date : 2006 - 04 - 23
  6 None.gif     // Written By coffee.liu
  7 None.gif     //================================================
  8 None.gif{$APPTYPE CONSOLE}
  9 None.gif
 10 None.gifuses
 11 None.gif  SysUtils;
 12 ExpandedBlockStart.gifContractedBlock.gif  type Person = Class protectedclass Class protectedclass
 13InBlock.gif       protected
 14InBlock.gif        sex:string;
 15InBlock.gif        race:string;
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function GetSex()Function GetSex()Function GetSex()function GetSex():string;
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        public Function GetRace()Function GetRace()Function GetRace()function GetRace():string;
 18InBlock.gif        public procedure GetInfo;virtual;abstract;
 19InBlock.gif        public procedure say;virtual;abstract;
 20InBlock.gif
 21InBlock.gif   end;
 22InBlock.gif   /yellow///
 23ExpandedSubBlockStart.gifContractedSubBlock.gif   type YellowPerson=Class (Class (Class (class(Person)
 24InBlock.gif       constructor YellowPerson();
 25InBlock.gif        public procedure GetInfo;override;
 26InBlock.gif        public procedure Say;override;
 27InBlock.gif     end;
 28ExpandedSubBlockStart.gifContractedSubBlock.gif   type AsiaYellowPerson=Class (Class (Class (class(YellowPerson)
 29InBlock.gif        constructor create;
 30InBlock.gif        public procedure Say;override;
 31InBlock.gif     end;
 32ExpandedSubBlockStart.gifContractedSubBlock.gif    type NorthAmericaYellowPerson=Class (Class (Class (class(YellowPerson)
 33InBlock.gif        constructor create;
 34InBlock.gif        public procedure Say;override;
 35InBlock.gif     end;
 36InBlock.gif
 37ExpandedSubBlockStart.gifContractedSubBlock.gif     type SouthAmericaYellowPerson=Class (Class (Class (class(YellowPerson)
 38InBlock.gif        constructor create;
 39InBlock.gif        public procedure Say;override;
 40InBlock.gif     end;
 41InBlock.gif
 42ExpandedSubBlockStart.gifContractedSubBlock.gif     type AustrialiaYellowPerson=Class (Class (Class (class(YellowPerson)
 43InBlock.gif        constructor create;
 44InBlock.gif        public procedure Say;override;
 45InBlock.gif     end;
 46InBlock.gif   //black//
 47ExpandedSubBlockStart.gifContractedSubBlock.gif    type BlackPerson=Class (Class (Class (class(Person)
 48InBlock.gif       constructor BlackPerson();
 49InBlock.gif        public procedure GetInfo;override;
 50InBlock.gif        public procedure Say;override;
 51InBlock.gif     end;
 52ExpandedSubBlockStart.gifContractedSubBlock.gif      type EuropeBlackPerson=Class (Class (Class (class(BlackPerson)
 53InBlock.gif        constructor create;
 54InBlock.gif        public procedure Say;override;
 55InBlock.gif     end;
 56ExpandedSubBlockStart.gifContractedSubBlock.gif      type AfricaBlackPerson=Class (Class (Class (class(BlackPerson)
 57InBlock.gif        constructor create;
 58InBlock.gif        public procedure Say;override;
 59InBlock.gif     end;
 60InBlock.gif   write/
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    type WritePerson=Class (Class (Class (class(Person)
 62InBlock.gif       constructor WritePerson();
 63InBlock.gif        public procedure GetInfo;override;
 64InBlock.gif        public procedure Say;override;
 65InBlock.gif     end;
 66ExpandedSubBlockStart.gifContractedSubBlock.gif      type EuropeWritePerson=Class (Class (Class (class(WritePerson)
 67InBlock.gif        constructor create;
 68InBlock.gif        public procedure Say;override;
 69InBlock.gif     end;
 70ExpandedSubBlockStart.gifContractedSubBlock.gif      type NorthAmericaWritePerson=Class (Class (Class (class(WritePerson)
 71InBlock.gif        constructor create;
 72InBlock.gif        public procedure Say;override;
 73InBlock.gif     end;
 74ExpandedSubBlockStart.gifContractedSubBlock.gif      type SouthAmericaWritePerson=Class (Class (Class (class(WritePerson)
 75InBlock.gif        constructor create;
 76InBlock.gif        public procedure Say;override;
 77InBlock.gif     end;
 78ExpandedSubBlockStart.gifContractedSubBlock.gif      type OrangutanWritePerson=Class (Class (Class (class(WritePerson)
 79InBlock.gif        constructor create;
 80InBlock.gif        public procedure Say;override;
 81InBlock.gif     end;
 82InBlock.gif   Evolution
 83InBlock.gif   type ArrPersons=array of Person;
 84ExpandedSubBlockStart.gifContractedSubBlock.gif    type Evolution=Class publicclassClass publicclass
 85InBlock.gif        public
 86InBlock.gif        Persons: ArrPersons;
 87InBlock.gif        public procedure  evolution();virtual;abstract;
 88InBlock.gif        constructor create;
 89InBlock.gif       // published
 90ExpandedSubBlockStart.gifContractedSubBlock.gif       // Property GetPerson()Property GetPerson()Property GetPerson()property GetPerson:ArrPersons read Persons;
 91InBlock.gif      end;
 92ExpandedSubBlockStart.gifContractedSubBlock.gif    type EvolutionToYellow=Class (Class (Class (class(Evolution)
 93InBlock.gif        constructor  EvolutionToYellow();
 94InBlock.gif        public procedure evolution();override;
 95InBlock.gif     end;
 96ExpandedSubBlockStart.gifContractedSubBlock.gif     type EvolutionToBlack=Class (Class (Class (class(Evolution)
 97InBlock.gif        constructor EvolutionToBlack();
 98InBlock.gif        public procedure evolution();override;
 99InBlock.gif     end;
100ExpandedSubBlockStart.gifContractedSubBlock.gif     type EvolutionToWrite=Class (Class (Class (class(Evolution)
101InBlock.gif        constructor EvolutionToWrite();
102InBlock.gif        public procedure evolution();override;
103InBlock.gif     end;
104InBlock.gif{ Person }
105InBlock.gif
106ExpandedSubBlockStart.gifContractedSubBlock.gifFunction Person()Function Person()Function Person()function Person.GetRace: string;
107InBlock.gifbegin
108InBlock.gif result:=race;
109InBlock.gifend;
110InBlock.gif
111ExpandedSubBlockStart.gifContractedSubBlock.gifFunction Person()Function Person()Function Person()function Person.GetSex: string;
112InBlock.gifbegin
113InBlock.gif  result:=sex;
114InBlock.gifend;
115InBlock.gif
116InBlock.gif{ YellowPerson }
117InBlock.gif
118InBlock.gifconstructor YellowPerson.YellowPerson;
119InBlock.gifbegin
120InBlock.gifinherited  ;
121InBlock.gif            sex:='Man';
122InBlock.gif            race:='Yellow';
123InBlock.gifend;
124InBlock.gifprocedure YellowPerson.GetInfo;
125InBlock.gif   begin
126InBlock.gif    inherited;
127InBlock.gif      WriteLn('the '+race+' Person Info:'+sex);
128InBlock.gif   end;
129InBlock.gifprocedure YellowPerson.Say;
130InBlock.gifbegin
131InBlock.gif  inherited;
132InBlock.gif      WriteLn('I am a Yellow Baby');
133InBlock.gifend;
134InBlock.gif
135InBlock.gif{ WritePerson }
136InBlock.gif
137InBlock.gifprocedure WritePerson.GetInfo;
138InBlock.gifbegin
139InBlock.gif  inherited;
140InBlock.gif     WriteLn('the '+race+' Person Info:'+sex);
141InBlock.gifend;
142InBlock.gif
143InBlock.gifprocedure WritePerson.Say;
144InBlock.gifbegin
145InBlock.gif  inherited;
146InBlock.gif      WriteLn('I am a Write Baby');
147InBlock.gifend;
148InBlock.gif
149InBlock.gifconstructor WritePerson.WritePerson;
150InBlock.gifbegin
151InBlock.gifinherited;
152InBlock.gif            sex:='Man';
153InBlock.gif            race:='Write';
154InBlock.gifend;
155InBlock.gif
156InBlock.gif{ BlackPerson }
157InBlock.gif
158InBlock.gifconstructor BlackPerson.BlackPerson;
159InBlock.gifbegin
160InBlock.gif       inherited;
161InBlock.gif            sex:='Man';
162InBlock.gif            race:='Black';
163InBlock.gifend;
164InBlock.gif
165InBlock.gifprocedure BlackPerson.GetInfo;
166InBlock.gifbegin
167InBlock.gif  inherited;
168InBlock.gif      WriteLn('the '+race+' Person Info:'+sex);
169InBlock.gifend;
170InBlock.gif
171InBlock.gifprocedure BlackPerson.Say;
172InBlock.gifbegin
173InBlock.gif  inherited;
174InBlock.gif     WriteLn('I am a Black Baby');
175InBlock.gifend;
176InBlock.gif
177InBlock.gif{ EvolutionToBlack }
178InBlock.gif
179InBlock.gifprocedure EvolutionToBlack.evolution;
180InBlock.gifvar
181InBlock.gifeb:EuropeBlackPerson;
182InBlock.gifab:AfricaBlackPerson;
183InBlock.gifbegin
184InBlock.gif            setlength(Persons,2);
185InBlock.gif             eb:=EuropeBlackPerson.create;
186InBlock.gif             ab:=AfricaBlackPerson.create;
187InBlock.gif            persons[0]:= person(eb);
188InBlock.gif            persons[1]:= person(ab);
189InBlock.gif
190InBlock.gif   // result:=BlackPerson.BlackPerson;
191InBlock.gifend;
192InBlock.gif
193InBlock.gifconstructor EvolutionToBlack.EvolutionToBlack;
194InBlock.gifbegin
195InBlock.gif inherited create;
196InBlock.gifend;
197InBlock.gif
198InBlock.gif{ EvolutionToWrite }
199InBlock.gif
200InBlock.gifprocedure EvolutionToWrite.evolution;
201InBlock.gifvar
202InBlock.gif ew:EuropeWritePerson;
203InBlock.gif nw:NorthAmericaWritePerson;
204InBlock.gif sw:SouthAmericaWritePerson;
205InBlock.gif ow:OrangutanWritePerson;
206InBlock.gifbegin
207InBlock.gif setlength(Persons,4);
208InBlock.gif           ew:=EuropeWritePerson.create;
209InBlock.gif           nw:=NorthAmericaWritePerson.create;
210InBlock.gif           sw:=SouthAmericaWritePerson.create;
211InBlock.gif           ow:=OrangutanWritePerson.create;
212InBlock.gif           persons[0]:= person(ew);
213InBlock.gif           persons[1]:= person(nw);
214InBlock.gif           persons[2]:= person(sw);
215InBlock.gif           persons[3]:= person(ow);
216InBlock.gifend;
217InBlock.gif
218InBlock.gifconstructor EvolutionToWrite.EvolutionToWrite;
219InBlock.gifbegin
220InBlock.gif inherited create;
221InBlock.gifend;
222InBlock.gif
223InBlock.gif{ EvolutionToYellow }
224InBlock.gif
225InBlock.gifprocedure EvolutionToYellow.evolution;
226InBlock.gifvar
227InBlock.gifay:AsiaYellowPerson;
228InBlock.gifasy:AustrialiaYellowPerson;
229InBlock.gifny:NorthAmericaYellowPerson;
230InBlock.gifsy:SouthAmericaYellowPerson;
231InBlock.gifbegin
232InBlock.gif setlength(Persons,4);
233InBlock.gif           ay:=AsiaYellowPerson.create;
234InBlock.gif           asy:=AustrialiaYellowPerson.create;
235InBlock.gif           ny:=NorthAmericaYellowPerson.create;
236InBlock.gif           sy:=SouthAmericaYellowPerson.create;
237InBlock.gif           persons[0]:= person(ay);
238InBlock.gif           persons[1]:= person(asy);
239InBlock.gif           persons[2]:= person(ny);
240InBlock.gif           persons[3]:= person(sy);
241InBlock.gifend;
242InBlock.gif
243InBlock.gif
244InBlock.gif
245InBlock.gifconstructor EvolutionToYellow.EvolutionToYellow;
246InBlock.gifbegin
247InBlock.gif   inherited create;
248InBlock.gifend;
249InBlock.gif
250InBlock.gif{ AsiaYellowPerson }
251InBlock.gif
252InBlock.gifconstructor AsiaYellowPerson.create;
253InBlock.gifbegin
254InBlock.gif  inherited YellowPerson;
255InBlock.gifend;
256InBlock.gif
257InBlock.gifprocedure AsiaYellowPerson.Say;
258InBlock.gifbegin
259InBlock.gif // inherited;
260InBlock.gif    WriteLn(' am a Yellow Baby ,My state in Asia');
261InBlock.gifend;
262InBlock.gif
263InBlock.gif{ AustrialiaYellowPerson }
264InBlock.gif
265InBlock.gifconstructor AustrialiaYellowPerson.create;
266InBlock.gifbegin
267InBlock.gif   inherited YellowPerson;
268InBlock.gifend;
269InBlock.gif
270InBlock.gifprocedure AustrialiaYellowPerson.Say;
271InBlock.gifbegin
272InBlock.gif  //inherited;
273InBlock.gif    WriteLn(' am a Yellow Baby ,My state in Austrialia');
274InBlock.gifend;
275InBlock.gif
276InBlock.gif{ NorthAmericaYellowPerson }
277InBlock.gif
278InBlock.gifconstructor NorthAmericaYellowPerson.create;
279InBlock.gifbegin
280InBlock.gif    inherited YellowPerson;
281InBlock.gifend;
282InBlock.gif
283InBlock.gifprocedure NorthAmericaYellowPerson.Say;
284InBlock.gifbegin
285InBlock.gif // inherited;
286InBlock.gif   WriteLn(' am a Yellow Baby ,My state in NorthAmerica');
287InBlock.gifend;
288InBlock.gif
289InBlock.gif{ SouthAmericaYellowPerson }
290InBlock.gif
291InBlock.gifconstructor SouthAmericaYellowPerson.create;
292InBlock.gifbegin
293InBlock.gif    inherited YellowPerson;
294InBlock.gifend;
295InBlock.gif
296InBlock.gifprocedure SouthAmericaYellowPerson.Say;
297InBlock.gifbegin
298InBlock.gif // inherited;
299InBlock.gif   WriteLn(' am a Yellow Baby ,My state in SouthAmerica');
300InBlock.gifend;
301InBlock.gif
302InBlock.gif{ AfricaBlackPerson }
303InBlock.gif
304InBlock.gifconstructor AfricaBlackPerson.create;
305InBlock.gifbegin
306InBlock.gif    inherited BlackPerson;
307InBlock.gifend;
308InBlock.gif
309InBlock.gifprocedure AfricaBlackPerson.Say;
310InBlock.gifbegin
311InBlock.gif // inherited;
312InBlock.gif  WriteLn('I am a Black Baby ,My state in Africa');
313InBlock.gif
314InBlock.gifend;
315InBlock.gif
316InBlock.gif{ EuropeBlackPerson }
317InBlock.gif
318InBlock.gifconstructor EuropeBlackPerson.create;
319InBlock.gifbegin
320InBlock.gif     inherited BlackPerson;
321InBlock.gifend;
322InBlock.gif
323InBlock.gifprocedure EuropeBlackPerson.Say;
324InBlock.gifbegin
325InBlock.gif // inherited;
326InBlock.gif  WriteLn('I am a Black Baby ,My state in Europe');
327InBlock.gifend;
328InBlock.gif
329InBlock.gif{ OrangutanWritePerson }
330InBlock.gif
331InBlock.gifconstructor OrangutanWritePerson.create;
332InBlock.gifbegin
333InBlock.gif   inherited WritePerson;
334InBlock.gifend;
335InBlock.gif
336InBlock.gifprocedure OrangutanWritePerson.Say;
337InBlock.gifbegin
338InBlock.gif//  inherited;
339InBlock.gif    WriteLn('I am a  Baby ,I am a Orangutan,I like banananananana..');
340InBlock.gifend;
341InBlock.gif
342InBlock.gif{ EuropeWritePerson }
343InBlock.gif
344InBlock.gifconstructor EuropeWritePerson.create;
345InBlock.gifbegin
346InBlock.gif    inherited WritePerson;
347InBlock.gifend;
348InBlock.gif
349InBlock.gifprocedure EuropeWritePerson.Say;
350InBlock.gifbegin
351InBlock.gif // inherited;
352InBlock.gif    WriteLn('I am a Write Baby ,My state in Europe');
353InBlock.gifend;
354InBlock.gif
355InBlock.gif{ NorthAmericaWritePerson }
356InBlock.gif
357InBlock.gifconstructor NorthAmericaWritePerson.create;
358InBlock.gifbegin
359InBlock.gif      inherited WritePerson;
360InBlock.gifend;
361InBlock.gif
362InBlock.gifprocedure NorthAmericaWritePerson.Say;
363InBlock.gifbegin
364InBlock.gif  //inherited;
365InBlock.gif    WriteLn('I am a Write Baby ,My state in NorthAmerica');
366InBlock.gifend;
367InBlock.gif
368InBlock.gif{ SouthAmericaWritePerson }
369InBlock.gif
370InBlock.gifconstructor SouthAmericaWritePerson.create;
371InBlock.gifbegin
372InBlock.gif       inherited WritePerson;
373InBlock.gifend;
374InBlock.gif
375InBlock.gifprocedure SouthAmericaWritePerson.Say;
376InBlock.gifbegin
377InBlock.gif // inherited;
378InBlock.gif    WriteLn('I am a Write Baby ,My state in SouthAmerica');
379InBlock.gifend;
380InBlock.gif
381InBlock.gif{ Evolution }
382InBlock.gif
383InBlock.gifconstructor Evolution.create;
384InBlock.gifbegin
385InBlock.gif
386InBlock.gif     evolution;
387InBlock.gifend;
388InBlock.gif
389InBlock.gif
390InBlock.gifvar
391InBlock.gif//yev,wev,bev:Evolution;
392InBlock.gif//yp,wp,bp:Person;
393InBlock.gifevs:array [0..2] of Evolution;
394InBlock.gifi,j:integer;
395InBlock.gifbegin
396InBlock.gif
397InBlock.gif    evs[0]:=EvolutionToYellow.EvolutionToYellow;
398InBlock.gif    evs[1]:=EvolutionToBlack.EvolutionToBlack;
399InBlock.gif    evs[2]:=EvolutionToWrite.EvolutionToWrite;
400InBlock.gif    for j:=0 to 2 do
401InBlock.gif      begin
402InBlock.gif    for i:=0 to length(evs[j].Persons)-1 do
403InBlock.gif       begin
404InBlock.gif          evs[j].Persons[i].say;
405InBlock.gif       end;
406InBlock.gif       end;
407InBlock.gif
408InBlock.gifend.

转载于:https://www.cnblogs.com/coffeeliu/archive/2006/04/22/382356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值