This needs a different approach
    Gide Nwawudu 
    gide at btinternet.com
       
    Wed Apr 30 06:46:29 PDT 2008
    
    
  
On Tue, 29 Apr 2008 23:38:52 +0200, "Saaa" <empty at needmail.com> wrote:
>
>> >
>>> IFruit[] fruits = [APPLE, PLUM, APPLE ... PEAR, PLUM];
>>>
>>> Since all fruits inherit from IFruit, they can all be represented as 
>>> IFruit references.  So, if you have an array of IFruit, you can put all 
>>> different kinds of fruits in it.
>>
>> Calling 'fruit[2].eat();' would call eat() from that specific APPLE, 
>> right?
>
>Changing fruit[2] to PLUM would go how?
Just assign as normal, fruits[2] = PLUM.
[CODE]
import std.stdio;
interface IFruit
{
     void eat();
}
IFruit APPLE;
IFruit PEAR;
IFruit PLUM;
static this()
{
     APPLE = new class() IFruit {
         void eat() {
             writefln("Eat APPLE");
         }
     };
     PEAR = new class() IFruit {
         void eat() {
             writefln("Eat PEAR");
         }
     };
     PLUM = new class() IFruit {
         void eat() {
             writefln("Eat PLUM");
         }
     };
} 
int main(string[] args) {
	IFruit[] fruits = [APPLE, PEAR, APPLE, APPLE, PEAR, PLUM];
	
	writefln("Fruits");
	foreach( f; fruits) {
		f.eat();
	}
	
	writefln("\nNew Fruits");
	fruits[2] = PLUM;
	foreach( f; fruits) {
		f.eat();
	}
	
	return 0;
}
[/CODE]
Gide
    
    
More information about the Digitalmars-d-learn
mailing list