Prev | Current Page 220 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

publicMethod()); // works and traces ???private method???
trace(sample.privateMethod()); // throws a compilation error
}
}
class RestrictAccessSample
{
public function new () { }
105
Chapter 5: Delving Into Object-Oriented Programming
public function publicMethod() : String
{
return privateMethod();
}
private function privateMethod() : String
{
return ???private method???;
}
}
In the example privateMethod can only be invoked in the context of SampleClass while
publicMethod is always accessible also inside other class declarations as long as it is associated to an
instance of SampleClass .
Having private fields is necessary to transmit messages between functions without exposing them in the
Application Programming Interface (API). This concept is known in OOP as encapsulation ; the
programmer that uses a certain class should not be aware of its internals.
Null Arguments
Assuming that a function argument should always provide a ready - to - use value is a common mistake.
The following example shows why.


Pages:
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232