April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Fraser | > Here's how I would solve it: > > interface IFruit > { > void eat(); > } I understand the interface, its a bit like a (partial) set of rules a class needs to implement > > IFruit APPLE; > IFruit PEAR; > //... > IFruit PLUM; Different instances of the interface. > > static this() I use this on a module level, it gets run before main. Is it the same here? > { > APPLE = new class() IFruit > { > void eat() > { > tasteTheJuicyGoodness(); : D > } > } > > // ... > } How would you make the array of fruits? Something I can iterate over. like: fruits[ APPLE,PLUM,APPLE ... PEAR,PLUM]; > > You could initialize them in different modules, if you wanted, too. I want that : D |
April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | "Saaa" <empty@needmail.com> wrote in message news:fv7ht6$2d6c$1@digitalmars.com... > How would you make the array of fruits? > Something I can iterate over. like: > fruits[ APPLE,PLUM,APPLE ... PEAR,PLUM]; 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. |
April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | > I confused two arrays yesterday. The fruits don't change as often as I said (more like once every few seconds for only a small percentage of all fruits). ok that sounds better ... until now > As to how the flow is; that loop is called 30 times / sec. And eating a fruit may change it to another fruit. and why does eating a fruit change it to another? is that the way your current algorithm work - or why does fruits change if eaten? more story details, please... |
April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | >> I confused two arrays yesterday. The fruits don't change as often as I said (more like once every few seconds for only a small percentage of all fruits).
>
> ok that sounds better ... until now
>
>> As to how the flow is; that loop is called 30 times / sec. And eating a fruit may change it to another fruit.
>
> and why does eating a fruit change it to another?
> is that the way your current algorithm work - or why does fruits change if
> eaten? more story details, please...
Does this really matter?
In oop style this might sound a bit strange, but in my setup it is the same
as changing the type number.
A different type means a different way of being eaten.
|
April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >
> 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?
|
April 29, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa |
> >
>> 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?
I would like the PLUM to retain data from that specific APPLE.
That data is of the same type over all fruits.
|
April 30, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | >> and why does eating a fruit change it to another? >> is that the way your current algorithm work - or why does fruits change if eaten? more story details, please... > Does this really matter? > In oop style this might sound a bit strange, but in my setup it is the same as changing the type number. desgins in oop become strange if the idea of what your program should do is an procedural codesnippet of how it should work :-) > A different type means a different way of being eaten. thats fully ok - but why does an eaten fruit morph into another one? sometimes people want it all in just one array maybe you need another one |
April 30, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Tue, 29 Apr 2008 23:38:52 +0200, "Saaa" <empty@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
|
April 30, 2008 Re: This needs a different approach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gide Nwawudu |
>
>>
>>> >
>>>> 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
Thanks Gide (and everybody else), I think I will implement it alike your
code.
Should be interesting enough so expect more questions in the future :)
|
Copyright © 1999-2021 by the D Language Foundation