A friend of mine, just getting into more advanced programming topics in Macromedia Flash, asked me what classes were.
“That’s an excellent question!” I beamed, pleased at the opportunity to share some hard-won knowledge. “They’re useful for constructing object hierarchies–“
“What are objects? And why are there hierarchies of them?”
“Well, you can model a problem as a series of interconnected objects…”
“Huh? Why? Where do you put them? Are they files?”
There has to be a better way of explaining classes to people without going down the tired old “Classes allow you to build object-oriented programs that make it easier to model real-world problems” explanation. The entry in Wikipedia is a great example of a definition that makes absolutely no sense to a beginning programmer:
In, object-oriented programming, a class consists of a collection of types of encapsulated instance variables and types of methods, possibly with implementation of those types together with a constructor function that can be used to create objects of the class. A class is a cohesive package that consists of a particular kind of compile-time metadata.
It just gets worse the more you read. It’s all technically correct, but like a lot of technical writing it tends to describe rather than explain. Which is pretty useless when you’re trying to understand something new.
So I’m thinking next time some asks me about classes in Flash, I’m going to try explaining it first in non-OOP terms.
- Classes allow you to group functions and variables together. You do this by using the
classstatement, which contains all the functions and variables that you want to be related to each other. This can make it a lot easier to organize your program into groups of classes, as opposed to huge lists of functions.
- Once you define a class, you can treat it as a new kind of data type. You can create new instance variables of a class you’ve defined using the
newstatement. To use the functions and data that you have defined inside the class, you use dot notation on the instance variable.
- Class definitions can also be used to organize your global variables, if you use the
statickeyword. This is far more useful than using the
- A lot of Flash’s built-in features are packaged in the form of pre-defined classes, so learning how to use classes is essential to unlocking the advanced features of Flash’s script environment.
- There are two other big advantages to using classes in AS 2.0: the compiler will be able to find errors and tell you what is wrong, and you can put all your source code inside the Flash Project Window. No more hunting for script fragments inside of your buttons and movieclips…they’re all in one place, finally.
At this point, one has to start talking about how to set up classes syntax-wise, explaining classpaths and packages, and the “one class per file” rule. I think one could probably defer a discussion of object-oriented programming until after the syntax is understood.
However, I think a lot of Flash programmers come at AS2.0 from a “I want to do X” perspective, and they they just want to know what properties to twiddle so the right thing happens. Do you force them to understand classes and object hierarchies?
Sleepy, will reflect on this later.
UPDATE: I’m starting to dump some thoughts into the blog as a kind of “think aloud” exercise. The next part is here.