lua-users home
lua-l archive

Re: AKClassHierarchy

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Feb 11, 2006, at 11:42 AM, PA wrote:
* Does not currently support any sort of 'super' mechanism for magically calling methods from an ancestor with the same name.
What about doing it explicitly instead of magically?
super.init( self )
In other words, you want the parent implementation (super.init) act on the current instance (self).
That's what I have now, but you have to explicitly specify the parent class:
	Rectangle = AKClass:new( )
	function Rectangle:initialize( inW, inH )
		self.w = inW or 0
		self.h = inH or 0
	end
	Square = AKClass:new( Rectangle )
	function Square:initialize( inLength )
		Rectangle.initialize( self, inLength, inLength )
	end
	UnitSquare = AKClass:new( Square )
	function UnitSquare:initialize( )
		Square.initialize( self, 1 )
	end
I tried what you suggest (albeit with "self.class.superclass", because I don't have a super property directly available to instances), but it fails when you have more than one level involved:
	function Square:initialize( inLength )
		self.class.superclass.initialize( self, inLength, inLength )
	end
	function UnitSquare( inLength )
		self.class.superclass.initialize( self, 1 )
	end
The problem is that when the UnitSquare instance is passed to Square's initializer, the self.class is still UnitSquare, which means that Square's initializer will infinitely call itself. For more info on this problem, see the second half of this post I made on the comp.lang.ruby group last night: http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/ c3b22bf2d53bd68b/89525293809a02e3#89525293809a02e3

AltStyle によって変換されたページ (->オリジナル) /