Classes (does this make sense?)
Saaa
empty at needmail.com
Fri May 30 18:34:03 PDT 2008
Best read after 'this needs a different approach' :)
Still trying to get some grip on those classes...
please comment on my try.
---------------------------
module main;
import std.stdio;
public import FruitClass
int main(string[] args) {
writefln("Fruits");
foreach( f; fruits) {
f.eat();
}
writefln("\nNew Fruits");
fruits[2] = PLUM;
foreach( f; fruits) {
f.eat();
}
return 0;
}
----------------------------
module FruitClass;
import Apple;
static this()
{
Fruit[] fruits=[APPLE,APPLE];
//with more fruits it would become something like
//Fruit[] fruits = [APPLE, PEAR, APPLE, APPLE, PEAR, PLUM];
}
abstract class Fruit
{
int color;
void eat();
//the actual class has more functions
}
----------------------------
module Apple;
import FruitClass;
class APPLE : Fruit
{
bool eaten=false;
void trowAway()
{
//how do you delete yourself?
}
void eat()
{
eaten=true;
}
this (uint color)
{
.color=color;
}
}
----------------------------
More information about the Digitalmars-d-learn
mailing list