| ||||||||
BackgroundObject Oriented Programming (OOP) is based on the packaging together the behavior (methods and events) of an entity with its identity and state information (properties). The package is called an object. Here are some things to know about objects and object-oriented programming:
Let's look at an example of how objects can be used to describe a "real-world" object:
Okay, so I can describe a Chevy TrailBlazer for a computer - now what? The description of the "TrailBlazer" object can be used to...
Object oriented technology is the foundation of programming tools such as Visual Basic (VB). VB depend on the implementation of windows, buttons, and other objects as "objects" to define and build a user interface. In addition, developers can create new objects to extend the environment and build complex applications. Now that we have covered the requisite techno-babble of object technology, let's look at how it is used in a programming environment such as Visual Basic.
Objects and ProgrammingThe windows environment itself is a key aspect of object-oriented programming systems. Windows-based programs operate by displaying objects such as windows, buttons, and text boxes on the screen for collecting and presenting data and information. These objects are the heart of programming development and delivery systems such as Visual Basic and JavaScript. Object-orientated design and development using tools such as C++, Java, and even Visual Basic has become the de facto standard approach for developing and delivering windows applications (not too mention enterprise and web applications as well). It is based on developing software programs using and manipulating objects that often represent "real-world" objects. For example, a double-column bookkeeping ledger can be presented as a grid in a program (a spreadsheet), which can contain data. The data, in turn, can be manipulated in much the same way as the paper ledger itself. Advantages of object-oriented programs are:
Object oriented software development is a complex and exciting aspect of computer science. It is much more than the brief discussion we are providing here, however, there are some aspects you need to understand if you want to develop windows-based software applications using tools such as Visual Basic and JavaScript. Programming with Objects (Object-Based Programming)Please note: much of the following discussion is oriented towards Visual Basic. Object based programming is technically an "application" of object oriented programming. Programming tools such as Visual Basic and JavaScript take advantage of "windows" program objects that support the window interface paradigm (forms, buttons, text boxes, etc.). These languages may also go further by allowing the creation and use of new objects in object-oriented programs. Program objects are components that can be used to build an application. They include:
The following form is from Visual Basic and illustrates some of the objects that can be used to create an application...
Just about everything on a windows-based screen is an object. Some objects, such as windows or forms, contain other objects. Objects are usually identified by unique names. For example:
When referring to objects within objects, a dot-notation is used. For example: "myform.gobutton" refers to the button in the "myform" window. There are some basic features of window-based programming objects that are important to understand:
PropertiesObjects are described with "properties" (data) which tell you what they look like, where they go, how they will act, and what they contain. Properties often describe physical properties of the object (color or size), but they can also describe behavior, and the data contained by an object. Examples of properties include:
Property descriptions such as "height" or "value" are usually referenced by dot-notation. For example:
EventsThe secret to how most windows-based programs lies in behaviors called "events". Events are triggered when windows are "loaded," buttons are "clicked," or text boxes are "changed." These events drive the program. Objects can respond
to stimuli or things that are done to them.
For example:
The following example
from Visual Basic shows the event list for the push button...
In many programming systems such as Visual Basic and JavaScript, objects are associated with behavior by appending a descriptive behavioral term to the name of the object. For example: "gobutton_click" indicating that the button has been clicked. In some cases, certain events such as "onclick" are explicitly associated with the object, a button. From a programming perspective, events are where the work is done. Code is attached to events to drive a program. For example: Sub command1_click () Text1.text = "Hello
World" End Sub In this example:
Code written for
events may be used to call other procedures, change properties, or invoke
methods. MethodsObjects usually have many things that they can do or that can be done to them. These are predefined procedures that often modify properties of the object such as its location. For example:
In many cases, methods can apply to different objects and perform approximately the same function on all of them. In the following example, the text1.move will cause the text box to move and be resized when the button is clicked...
In many cases,
values are passed to methods that change property values. For example, the "move" method in Visual Basic is
given values for the left and top position as well as height and width. The
properties for left, top, width, and height are changed by the "move"
method. Methods are usually referenced by the following notation: "mybox.move 10,10,200,200" which invokes the move method for "mybox" to the left and top positions of 10 and a width and height of 200. CollectionsAnother feature of some objects is the ability to contain other objects. For example, a database table (an object) consists of a collection of records (also objects) or a form includes a collection of subordinate objects. Just remember:
Referencing Objects in ApplicationsEvery object has a unique name, which can be linked to properties and methods. For example:
In some cases, objects may be contained within other objects. For example, a form, named myform, contains cmdbutton. If cmdbutton is referenced from outside of myform, it is referred to as myform.cmdbutton.property. This strategy is used extensively in JavaScript to refer to objects on web forms, for example: document.webform.tbox.value This name refers to the value found in the text box in a form in a browser document. Events, in the Visual Basic environment, are linked to objects with the "_". For example, in Visual Basic, the cmdbutton click event would look like this: cmdbutton_click. This technique will vary in other environments such as JavaScript where the "onclick" event is explicitly described when the object is described. For example: <input type="button" VALUE="Click to add ="
NAME="button1" OnClick="addnum()"> The button has an "OnClick" event that invokes the "addnum" function. Why Is Object-based Programming So PowerfulThe primary advantage of object-based programming systems is the speed and ease in which you can design and build an application. The benefit of windows-based programming is the consistency that is found in how programs look, feel, and behave. In some cases, a technique called rapid prototyping is applied in which programs are constructed very quickly to understand what needs to be done. These programs are usually discarded when the final program is constructed. In fact, you can quickly design an application by "drawing" it. This allows you to think about the program before you actually sit down to create it. There are various other techniques for describing how a program works such as flow charts that you may already have heard of. The user interface is built by placing objects on the screen that the end user will interact with to accomplish the task of the program. These might include buttons, text boxes, and list boxes. Once the user interface is built, code is attached to the objects to enable the program to perform its work. As the programmer, you focus on what the program needs to do and not on how buttons or boxes are drawn on the screen during program execution. The programming tool and the delivery environment (operating system) take care of how the buttons work during program execution (when you click them).
Key Things to Remember about Objects and Windows ProgrammingNote: the following comments refer directly to tools such as Visual Basic and are not intended as a comprehensive discussion on object oriented programming.
|
|
Copyright © 1999
- 2005 |