Oddity with Inheritance

42 views
Skip to first unread message

Marco Salamone

unread,
Feb 14, 2013, 8:36:02 AM2/14/13
to haxe...@googlegroups.com


var ret:Item; //Base class for Ring, Weapon, and Armor
switch(type)
{
case 1: ret = new Ring(iType,Blessing);
case 2: ret = new Weapon(iType, Blessing);
case 3: ret = new Armor(iType, Blessing);
default: ret = new Item(iType, Blessing);
}
}while (!MenuPrompter.IsCorrect(ret.name()));



Item also has an internal field- static var Types = ["My","Name","Types" /*etc*/], which is redefined for each extending class.

Whenever I call ret.name(), it uses Item.Types to look-up the output string instead of, say, Ring.Types.

Any syntactical recommendation to solve this? If possible, I'd like to avoid overriding the function.
Thanks.

Alexander Kuzmenko

unread,
Feb 14, 2013, 10:05:39 AM2/14/13
to haxe...@googlegroups.com
As far as i know, static vars and methods are not inherited on extending classes.

четверг, 14 февраля 2013 г., 17:36:02 UTC+4 пользователь Marco Salamone написал:

Jason O'Neil

unread,
Feb 14, 2013, 11:45:27 PM2/14/13
to haxe...@googlegroups.com
Yeah statics are not inherited, and variables and inline methods can't be overridden. 

Here is an example of what I know to do: http://try.haxe.org/#2868e

Essentially, you have two options that I know of:

 * Use a method that you override in your sub classes
 * Use a property (which really just points to a method that you override in your sub classes).

Does anyone know of a better way?  I am curious...



--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Greg Dove

unread,
Feb 15, 2013, 12:40:16 AM2/15/13
to haxe...@googlegroups.com
Maybe through the constructor? Not sure if this makes sense or not, but it does mean the subclasses are a bit leaner.


Greg Dove
Dove Software Development Ltd
http://greg-dove.com

Jason O'Neil

unread,
Feb 15, 2013, 12:44:33 AM2/15/13
to haxe...@googlegroups.com

Oh right, that's much more sensible. Thanks for pointing out my obvious omission :)

Reply all
Reply to author
Forward
0 new messages