Introduction to Classes |
|
|
Class Variable Initialization |
To initialize a class, you can access each member and assign it an appropriate value. Here is an example: |
class Book { var Title : String; var Author : String; var Pages : int; var CoverType : char; } var BrandNew : Book; BrandNew = new Book; BrandNew.Title = "Webber Databases With JScript .NET"; BrandNew.Author = "Catherine Mamfourh"; BrandNew.Pages = 1225; BrandNew.CoverType = 'H'; print("Book Characteristics"); print("Title: ", BrandNew.Title); print("Author: ", BrandNew.Author); print("Pages: ", BrandNew.Pages); print("Cover: ", BrandNew.CoverType); |
Classes and Member Functions |
Introduction |
The primary motivation of using classes in a program is to create objects as complete as possible. An object must be able to handle its own business so that the other objects of the program or of another program would only need to know which object can take care of a particular need they have. A regular variable, as a member of an object, cannot handle assignments; this job is handled by particular functions declared as members of a class. A function as a member of a class is also called a Method. In our lessons, the words “method” and “function”, when associated with a class, will refer to the same thing: a member function of the class. |
Creating Member Functions |
To create a member function of a class, type the function keyword followed by a name, the necessary parentheses, and the curly brackets that delimit the body of a function. Here is an example: |
class FRectangle { var Length : double; var Height : double; function Perimeter() { } } |
To implement a method, use the same techniques we used to define regular functions. When a method is a class' member, it has access to the member variables of the same class; this means that you don't need to pass the variables as arguments. You can just use any of them as if it were supplied. Here is an example: |
class FRectangle { var Length : double; var Height : double; function Perimeter() : double { return (Length + Height) * 2; } function Area() : double { return Length * Height; } } |
Class Members Interactions |
Regardless of how the member methods of a class are implemented, any method can call another without using an access operator. This allows a class' methods to exchange information among themselves easily. Furthermore, unlike regular functions where a function must be declared prior to another function calling it, the method members of a class don't abide by that rule: one method can call another method before or after the other has been implemented, as long as it is defined somewhere. Here is an example: |
class FRectangle { var Length : double; var Height : double; function Perimeter() : double { return (Length + Height) * 2; } function Area() : double { return Length * Height; } function ShowCharacteristics() { print("Rectangle Characteristics"); print("Length: ", Length); print("Height: ", Height); print("Perimeter: ", Perimeter()); print("Area: ", Area()); } } var Recto : FRectangle = new FRectangle; Recto.Length = 24.55; Recto.Height = 20.75; Recto.ShowCharacteristics(); |
Once an object is defined and behaves as complete as possible, the other function or objects of the program can make the appropriate calls trusting that the called object can handle its assignments efficiently. This ability allows you to (tremendously) reduce the work overload of the other components of a program. |
|
||
Previous | Copyright © 2004 FunctionX, Inc. | Next |
|