/* Adding a class method using ::METHOD */
a = .point~new(1,1) /* Create some point instances */
say "Created point instance" a
b = .point~new(2,2) /* create another point instance */
say "Created point instance" b
c = .point~new(3,3) /* create another point instance */
say "Created point instance" c
/* ask the point class how many */
/* instances it has created */
say "The point class has created" .point~instances "instances."
::class point public /* create Point class */
::method init class
expose instanceCount
instanceCount = 0 /* Initialize instanceCount */
forward class (super) /* Forward INIT to superclass */
::method new class
expose instanceCount /* Creating a new instance */
instanceCount = instanceCount + 1 /* Bump the count */
forward class (super) /* Forward NEW to superclass */
::method instances class
expose instanceCount /* Return the instance count */
return instanceCount
::method init
expose xVal yVal /* Set object variables */
use arg xVal, yVal /* as passed on NEW */
::method string
expose xVal yVal /* Use object variables */
return "("xVal","yVal")" /* to return string value */
/* Adding a class method using a metaclass */
a = .point~new(1,1) /* Create some point instances */
say "Created point instance" a
b = .point~new(2,2)
say "Created point instance" b
c = .point~new(3,3)
say "Created point instance" c
/* ask the point class how many */
/* instances it has created */
say "The point class has created" .point~instances "instances."
::class InstanceCounter subclass class /* Create a new metaclass that */
/* will count its instances */
::method init
expose instanceCount
instanceCount = 0 /* Initialize instanceCount */
forward class (super) /* Forward INIT to superclass */
::method new
expose instanceCount /* Creating a new instance */
instanceCount = instanceCount + 1 /* Bump the count */
forward class (super) /* Forward NEW to superclass */
::method instances
expose instanceCount /* Return the instance count */
return instanceCount
::class point public metaclass InstanceCounter /* Create Point class */
/* using InstanceCounter metaclass */
::method init
expose xVal yVal /* Set object variables */
use arg xVal, yVal /* as passed on NEW */
::method string
expose xVal yVal /* Use object variables */
return "("xVal","yVal")" /* to return string value */