Chapter 4. Object Oriented Programming

Programming using classes is essential to take advantage of the full power of ActionScript 3.0. By using classes, you participate in object oriented programming, a versatile way to think about and structure code. Objects are high-level building blocks, and classes are the constructs that define them. In this chapter you learn what constitutes a class, how classes relate to objects and other language constructs, and how to write a class in ActionScript 3.0.

Understanding Classes

To program using classes, you must understand what classes are. Classes perform multiple roles in object oriented programming, and there are many ways to think about them. This section introduces these fundamental ideas.

Classes Can Model the Real World

Classes often work best and are easiest to think about when they represent objects in the real world. You can, for example, create a class to represent a bicycle. That bicycle can have a color, a size, and two tires. You can pedal it, brake it, switch gears, and ring the bell. The bicycle has relationships with other real-world objects: your feet, the road surface, and occasionally a bike pump. If you model the bicycle as a class, you can represent all of these and carefully design it to interact with the other objects in the right ways. Let's call this class Bicycle. I will come back to the Bicycle class to describe concepts in class design throughout this chapter.

You can use classes throughout your program to model tangible ...

Get ActionScript 3.0 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.