Of course there are easier ways to do the same thing... :-)
(G)2008 xChas */
-def_mem(Actions)
+def_interface(Animal,Actions)
{
- str method(talk) (anonymous self);
+ str method(talk) (Animal self);
};
-def_mem(Animal)
+def_obj(Animal)
{
interface(Actions);
char *name;
};
-construct(Animal) (Animal self, str name)
+construct(Animal,Actions) (Animal self, str name)
{
self->name = name;
return self;
}
-str cat_talk(anonymous self)
+/* implementation of methods */
+
+str catTalk(Animal self)
{
return "Meow!";
}
-str dog_talk(anonymous self)
+str dogTalk(Animal self)
{
return "Arf! Arf!";
}
-Actions cat_actions(void)
+/* registration of methods to object interface */
+
+Actions catActions(void)
{
- Get_mem(this,Actions);
+ Get_mem(this, Actions);
- this->talk = cat_talk;
+ this->talk = catTalk;
return this;
}
-Actions dog_actions(void)
+Actions dogActions(void)
{
- Get_mem(this,Actions);
+ Get_mem(this, Actions);
- this->talk = dog_talk;
+ this->talk = dogTalk;
return this;
}
+/* usage of objects inside C<<1 program */
+
program
{
int i;
Animal animal[3];
- Actions cat = cat_actions();
- Actions dog = dog_actions();
+ Actions cat = catActions();
+ Actions dog = dogActions();
- animal[0] = get_obj(Animal,cat,"Missy");
- animal[1] = get_obj(Animal,cat,"Mr. Bojangles");
- animal[2] = get_obj(Animal,dog,"Lassie");
+ animal[0] = get_obj(Animal, cat, "Missy");
+ animal[1] = get_obj(Animal, cat, "Mr. Bojangles");
+ animal[2] = get_obj(Animal, dog, "Lassie");
for_range(i, 0, 2)
{
- print(animal[i]->name,":",_(talk,animal[i]));
+ print(animal[i]->name, ":", _(talk, animal[i]));
}
+
}