Prev | Current Page 226 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


To improve the BlogEntry class, a variable containing the creation date is added and its value is
automatically generated at the instantiation time.
class BlogEntry
{
public var title : String;
public var content : String;
public var createdOn : Date;
public function new ()
{
createdOn = Date.now();
}
}
The constructor, as any other function, can have arguments that in the Blog examples can be used to set
some constraints on the BlogEntry declaration and to reduce the line of codes required to create a
usable instance of the class.
class BlogEntry
{
public var title : String;
public var content : String;
public var createdOn : Date;
??‘
??‘
??‘
109
Chapter 5: Delving Into Object-Oriented Programming
public function new (title : String, content : String)
{
this.title = title;
this.content = content;
createdOn = Date.now();
}
}
The instance is now created this way:
class Main
{
static function main()
{
var entry : BlogEntry = new BlogEntry(???My First Blog Entry???, ???...???);
trace(entry.


Pages:
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238