Prev | Current Page 232 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

now(); // fails
}
}
To demonstrate another use of the read - only access, change the example to introduce a new variable
excerpt , which will contain a small description of the blog entry using the first few words of the
content property. The value is calculated on each call so that the class doesn ??™ t require any support
variable.
public var excerpt(getExcerpt, null) : String;
private function getExcerpt() : String
{
return content.substr(0, 10) + ??? ...???;
}
The new code can be used like this:
class Main {
static function main()
{
var entry : BlogEntry = new BlogEntry(
???My First Entry???,
???Using the BlogEntry class is easy.???);
trace(entry.excerpt);
}
}
The excerpt variable will now output the first ten characters of the content variable followed by a
space and an ellipsis. In some cases it is better to provide an alternative text for the excerpt instead of
cropping blindly the content of the blog entry. To obtain this, a setter method and a new private variable
definedExcerpt are added to the class.


Pages:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244