Continuing from last time, this article begins covering features of C# classes that aren’t in AS3. We’ll begin with abstract classes and functions, which AS3 required workaround code to enforce even at run-time. Today’s article shows you how to use C# to cleanly enforce these at compile-time.
First off, I forgot one feature of AS3′s classes in the previous three articles. Let’s briefly discuss static static initializers, also known as class initializers, class constructors, or static constructors. This is a function that is called automatically when the class’ static fields need to be initialized prior to the class being used. Here’s how it looks in AS3:
Static initializers aren’t commonly used because you can declare and initialize your fields at the same time (
private static var NEXT_ID:int = 1;
), but can be useful if the initialization requires more complex logic. In any case, here’s the equivalent in C#:
Static initializers in C# are called a “static constructor”. It’s like a constructor for the class rather than a constructor for an instance of the class. It’s syntax is just like that of an instance constructor, except that it has the
static
keyword first, it never has an access modifier, and it doesn’t take any parameters.
Now let’s talk about abstract classes. These are classes that can’t be instantiated directly. To create one, you need to extend the abstract class with a non-abstract class and instantiate the extending class. AS3 lacks this feature at compile-time, but there is a common run-time workaround:
With this workaround you can still write code that creates an
ExtrudedShape
directly and it will compile just fine:
At runtime, the first argument will be checked against the
private const
Object
that anyone outside of the class and its derivatives couldn’t possibly have access to. The ArgumentError
will then be thrown and the caller will not get an instance of the ExtrudedShape
. A derivative class, on the other hand, has access to HIDDEN_KEY
and can pass it to the constructor:
This is an effective strategy for implementing abstract classes at run-time, but C# offers a way to enforce this at compile-time:
Note the use of the
abstract
keyword when declaring the class. This tells the compiler not to let anyone directly instantiate the class. There’s no need for any of the runtime workaround that AS3 requires. Derivative classes don’t need any HIDDEN_KEY
and look just like classes extending any other non-abstract classes:
Similar to abstract classes are abstract functions. These are used when you want your abstract class to not define a function but instead mandate that classes extending it must implement the function. There is, again, no way to do this in AS3 at compile-time. Instead, it can be worked around at runtime:
Here the
ExtrudedShape
class doesn’t want to define the get area
function since it really doesn’t know anything about that. So its version ofget area
immediately throws an exception. This makes the function effectively abstract at run-time, but there is again no compile-time checking. The following derivative class compiles just fine without implementing the get area
function:
In C#, we simply use the
abstract
keyword:
The same keyword would be used for non-getter functions, too:
That wraps up abstract classes and functions for today, as well as static initializers. To summarize, here’s a comparison between C# and AS3 covering everything in this article:
Next time we’ll continue with C# class features that aren’t available in AS3. Stay tuned!
Source: habrahabr.ru
TRANSLATION
No comments:
Post a Comment