Properties in Methodica

new topic     » topic index » view thread      » older message » newer message

After carefully reviewing Derek's second post, I have tried my hand at creating
a good readable syntax for defining properties. I have also adopted his reasobale
suggestion that methods and properties both have private access by default.
No access specification is used for instance variables.

Derek's example class reworked again:

class Quad extends Entity

integer h,w

property integer height
public get	
	return this.h
public set
	if value<0 then
		throw Type_Error("Height must not be negative.")
	else
		this.h=value		
	end if
end property

property integer width 
public get
	return this.w		
public set
	if value<0 then
	        throw Type_Error.new("Width must not be negative.")
	else
		this.w=value
	end if
end property

property integer area
public get
	return this.h*this.w
private set	
	integer x
        x=Math.sqrt(area).to_integer()
	this.h=x
	this.w=x
end property
	
method shared public Quad new(integer height=0,integer width=-1)	
	Quad me
	if height<0 then height=0 end if
	if width<0 then
		height=Math.sqrt(height).to_integer()
		width=height
	end if
	me=super()
	me.height=height
	me.width=width
	return me
end method

end class

--used thus ...

class Application extends Entity

method shared public main()
	Quad q
	q=Quad.new(7,8)
	Console.puts(q.area) -- displays 56
  	q.width=5
    Console.puts(q.area) -- displays 35
   	q.width+=1 --shorthand for q.width=q.width+1 
   	Console.puts(q.area) -- displays 42
   	Console.puts(q.height) -- displays 7
   	Console.puts(q.width) -- displays 6
	q=Quad.new(81)
	Console.puts(q.area) -- displays 81
	Console.puts(q.height) -- displays 9
   	Console.puts(q.width) -- displays 9
    q.area=49  -- will throw Access_Denied, this property has a private setter.
end method
	
end class


new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu