|
he next level: Class behavior
As you've seen by now, AS3 classes look pretty much like Java classes. You haven't seen them yet, but AS3 interfaces also look eerily similar to their Java counterparts. But looks aren't everything (except with supermodels and wax fruit) and there are a few important distinctions in behavior that are worth investigating.
Constructors and permissive behavior
Java allows the same access permission specifiers (public, protected, private, and package-private) on constructors that are allowed on classes, fields, and methods:
public class FooObject {
private FooObject() {}
}
FooObject foo = new FooObject();
// Compiler error
In AS3, constructors are always public:
public class FooObject {
private function FooObject() {}
}
// Compiler error
Making a constructor private (in a language that supports it, like Java) is not a typical pattern, although it is helpful in some situations, like creating singletons. If you really want only one of something, then it's a good idea to prevent anyone but the class itself from creating it. A workaround used in AS3 involves throwing exceptions from the constructor when called from outside of your singleton accessor, but it is not quite the same thing.
Interfaces and properties
Java allows properties to be declared on interfaces:
public interface IFoo {
public int blah = 5;
public final int VAL = 7;
}
*Both of these properties are implicitly static and final, even though they lack those keywords. Try it, you'll see.
AS3 does not allow properties on interfaces. Only functions can be declared on interfaces. Note, however, that you can declare properties with set/get functions; just not properties as fields. For example, this works:
public interface IProperties
{
function set blah(value:Number):void;
function get blah():Number;
}
If the get/set example here makes no sense, don't worry. You'll learn more about properties and these functions in the second half of this article. |
|