Object Oriented Language

来源:互联网 发布:决战武林弩炮进阶数据 编辑:程序博客网 时间:2024/06/10 05:35

 Object Oriented Language

As we have previously mentioned, Flex 3 makes use of ActionScript 3.0, a powerful object-oriented programming language. Before we dive into the specific syntax used in ActionScript, let's take a look at what makes a language object-oriented in the first place, and the benefits to using and object-oriented language versus a procedural language.

One of the benefits of object-oriented programming is the manner used to describe the elements that make up an application. We can analogize describing elements of an application to the way a human would describe the elements that make up a physical room.

If you were asked to describe your immediate surroundings as thoroughly as possible, you'd likely start by listing the various objects you see in front of you. If you're in a room with some furniture that includes a few chairs and a table, for example, you could talk about how many chairs there are, what color they are, and whether they have cushions. If there's a door, you could report whether it's closed or open. If there's a clock, you may indicate what time it is. By touching on the various elements that make up your surroundings, and by describing the specific state that they're in, we could get a clear picture of what you're seeing. This is true because even though we have never seen your particular chair, or door, we have a abstract notion on what chairs and doors are. Your chair is brown, it's got a cushion, and your door is closed? No problem, I can dig that. It would be a problem if you reported that your chair was closed and your door had a cushion because that wouldn't jive with my expectations of chairs and doors.

If you were to use this approach of using objects and their behaviors to write computer programs, you'd be applying the object-oriented programming paradigm. Central to object-oriented programming is the use of objects which are specific representations of classes. In the above example, the door that we refer to is an object that has been derived from the class "door". A class can be thought of as a template from which objects are created. The class defines the various behaviors (methods), and qualities (properties), that an object will have. In object-oriented terms, an object's methods and properties are known as members. An object should almost always contain both properties and methods.

The above example describes a room with furniture as it exists in the real world. Let's explore the way an object-oriented program would describe the same scene. First of all, we have an object called Room. Room can be thought of as the parent object in our example. In object-oriented programming, the process of creating an object that contains several other objects is known as composition. So, our Room object is composed of many other objects (Clock, Chair, Door, etc). The language used to describe these relationships is "Room has a Clock", because Clock is a property of Room.

Room
Clock
Chair
Door
Enter the Room
Leave the Room


As you can see, our Room has a Clock, Chair, and a Door. There are also two methods available as part of Room: "Enter the Room" and "Leave the Room". These are both things that you'd like to "do" to a Room. Now let's take a look at the rest of the items that our Room has, modeled as objects.

Clock
Hours
Minutes
Seconds
AM or PM
Position in Room
Set the Time
Move Clock in Room
Move Clock to new Room


Chair
Cushion
Number or Legs
Height to Seat
Total Height
Position in Room
Sit in Chair
Stand up from Chair
Move Chair in Room
Move Chair to new Room


Door
Height
Can be Locked
Is Locked
Wall Positioned On
Open Door
Close Door
Lock Door
Unlock Door


A fundamental aspect to object-oriented programming is the notion that classes are relatively self-contained pieces of code. What this means is that the functional information contained within a class is separated from the objects that act on it. This aspect of object-oriented programming provides protection for a class so that objects that aren't relevant to the class can't access its resources. In the above example, a chair cannot be open or closed because that's not part of "chair behavior". A class protects its contents through encapsulation. Encapsulation dictates the level of access a program may have on the methods and properties of a given class.

A programmer controls the type of access an application can have to its objects by using access modifiers. In ActionScript, parts of a class may be declared public, private, or protected. A method or property of a class that's determined to be public may be accessed by all objects in a program. A method or property that's private may only be accessed by members of the same class. Finally, a method or property that's protected can be accessed by object of its own class type and any class type that's derived from that class. This leads us to another major part of object-oriented programming -- inheritance.

A class in an object-oriented program may give rise to many related sub-classes. To use a real-world example, a chair could be thought of as a relative of the furniture class. Chairs, after all, have characteristics in common with all furniture, even though they have their own unique qualities as well. You may extend the chair class even further and introduce a rocking chair class, or a barstool class. Both rocking chairs and barstools are types of chairs, and therefore also furniture. Rocking chairs and barstools are said to inherit the methods and properties of furniture and chairs. They are both movable structures that are designed to support the human body (or however you may define furniture), and they are specifically designed for sitting. What inheritance does for object-oriented programming is allow for scalability without having to completely rewrite code for every single object. Inherited objects have an "is-a" relationship with the classes they inherit from. A rocking chair "is-a" chair, and a chair "is-a" piece of furniture.

So returning to our real world "room with furniture" example, suppose we'd like to add another chair to our room. We found a really sweet barstool on eBay using the eBay Desktop application (because we're hip to RIAs), and we decided to add it to our collection. It's safe to say that when it arrives we won't have to completely relearn how to use it. We've got a lot of experience with chairs of all kinds after all, and sitting on this one shouldn't be much of a stretch. This may sound obvious, but when it comes to programming such a scenario, unless we do some tricky stuff we would actually have to re-code part of our application every time we wanted to add a new component. This is because unless we explicitly state how a program should interact with an object, it will have no way of knowing what to do. We solve this potential headache by using what's known as an interface. An interface is simply a collection of methods that governs how new instances of a class should behave. An interface for the chair class would outline behavior that every chair, regardless of type, would demonstrate. It wouldn't include rocking, for example, because barstools probably don't do this. It may include "supporting a human" though, because all chairs regardless of type do this. Using an interface goes a long way in making a program scalable. Because the original code doesn't need to be modified any time an addition is made, programmers can quickly add to an application.

The development of the object-oriented paradigm has given programmers a powerful model for creating applications that mimic real world scenarios. In addition to providing a real world perspective, object-oriented programs are by nature scalable considering they are composed of self-contained units of code known as objects. Although a thorough exploration into object-oriented programming goes beyond the scope of this article, a sound understanding of its basic principles is important for anyone learning Flex. In our next post we'll discuss more specific syntax as it applies to the object-oriented language of Flex -- ActionScript 3.0.

Current Glossary of Terms

Access Modifier A keyword that declares what part of a program may access the methods and properties of an object. Commonly used access modifiers are public, private, and protected.

Class In object-oriented programming, a class is used as a template to create objects. The class contains the behaviors and attributes of the objects that are created from it.

Composition The process of creating an object that contains one or more other objects.

Encapsulation The protection of an object's methods and properties through the use of access modifiers.ᅠ

Inheritance The relationship of classes with a shared origin. A specific rocking chair class is inherited from a base chair class.

Interface A collection of method and/or property descriptions that are used to define how a program interacts with members of a class.

Member A method and property of a class.

Method Behaviors associated with a class.

Object An instantiation of a class

Object Oriented Programming A programming paradigm that is centralized on the use of classes and objects to abstract the functional element of a piece of software.

Private An access modifier that declares that only the same class may access an object's methods and properties.

Property A characteristic of an object. Also known as the object's data.

Protected An access modifier that declares that only members of the same class and its derived classes may access an object's methods and properties.

Public An access modifier that declares that an objects methods and properties are accessible throughout the entire program.ᅠ

原创粉丝点击