Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.

Similar presentations


Presentation on theme: "What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex."— Presentation transcript:

1 What is an object?

2 What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex. The file object An object has state (it has various properties, which might change). Properties are often referred to as attributes. An object has behavior (it can do things and can have things done to it).

3 Software Objects Software objects will have identity, state, and behavior just as do real world objects. Objects (real world and software) have identity, state, and behavior. Software objects have identity because each is a separate chunk of memory (just like a yellow tennis ball, a software object is a distinct individual even if it looks nearly the same as other objects.) Software objects have state. Some of the memory that makes a software object is used for variables which contain values. Software objects have behavior. Some of the memory that makes a software object is used to contain programs (called methods/functions) that enable the object to "do things." The object does something when one of its method runs.

4 An Object

5 Class When a ACTIONSCRIPT application is being run, objects are created and their methods are invoked (are run.) To create an object, there needs to be a description of it. A class is a description of a kind of object. A programmer may define a class using ACTIONSCRIPT, or may use predefined classes that come in class libraries.

6 Example Here is a tiny application that instantiates (creates) an object by following the plan in the class String: var str1:String; // str1 is a variable that refers to an object, but the object does not exist yet. var len:Number; // len is a primitive variable of type Number (all data types are objects in AS3 even Number) str1 = "Random Jottings"; // create an object of type String len = str1.length; // invoke the object's property length Alert.show("The string is " + len + " characters long");

7 Notes The class String is defined at the Top Level and therefore does not need to be included into a program Class reference: http://help.adobe.com/en_US/FlashPlatform/ reference/actionscript/3/String.html http://help.adobe.com/en_US/FlashPlatform/ reference/actionscript/3/String.html

8 Objects and Names for Objects A variable that can refer to an object does not always have an object to refer to. For example, in our program the variable str1 refers to an object only after assigning the String to it. var str1:String; // str1 is a variable that refers to an object, // but the object does not exist yet var len:Number; // len is a primitive variable of type str1 = "Random Jottings"; // create an object of type String len = str1.length; // invoke the object's property length Alert.show("The string is " + len + " characters long"); Before (str1 = "Random Jottings"; ), str1 was a "place holder" that did not actually refer to any object.

9 Using a Reference to an Object Once the object has been created (with the new operator or in the case of a String, by just assigning it), the variable str1 refers to an existing object. That object has several properties, one of them the length property. A String object's Length properties stores the number of characters in the string. Reference: http://help.adobe.com/en_US/FlashPlatform/ref erence/actionscript/3/String.html http://help.adobe.com/en_US/FlashPlatform/ref erence/actionscript/3/String.html

10 Using an Object's Properties Remember that an object consists of both variables (state information) and methods (recipies for behavior.) Both of these are called "members" of the object. ACTIONSCRIPT uses "dot notation" for both: referenceToAnObject.memberOfObject For example, to invoke the Length property of the object named str1 the following is used: len = str1.length; Method names have "()" at their end.

11 Using a Class to Make Many Objects A class is like a cookie cutter that can be used many times to make many cookies. There is only one cookie cutter, but can be used to make many cookies. Cookies are objects and each one has its own individuality because each one is made out of a different section of dough. Different cookies may have different characteristics, even though they follow the same basic pattern. For example, different cookies can have different candy sprinkles or frosting, or be backed for different times. Cookies can be created. And cookies can be destroyed (just ask Cookie Monster). But destroying cookies does not affect the cookie cutter. It can be used again if there is dough left.

12 Static In the ACTIONSCRIPT language, a characteristic of a class definition that is not shared by its objects is called static. There is only one class definition for a given class, so when a program is running, if something is static then there is only one of it. You can think of the word static as meaning "no matter how many objects are made, there will be only one of these." For example: public static var specialX:Number;  this means that specialX is available to all objects derived from the class in which this variable is defined but is only accessible through the class itself! An example is the Math class and its list of static properties and functions Reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/M ath.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/M ath.html

13 Static variables Static variables are declared using a combination of the static keyword and either the var or const statement. Static variables, which are attached to a class rather than an instance of a class, are useful for storing and sharing information that applies to an entire class of objects. For example, a static variable is appropriate if you want to keep a tally of the number of times a class is instantiated or if you want to store the maximum number of class instances that are allowed. The following example creates a totalCount variable to track the number of class instantiations and a MAX_NUM constant to store the maximum number of instantiations. The totalCount and MAX_NUM variables are static, because they contain values that apply to the class as a whole rather than to a particular instance. class StaticVars { public static var totalCount:int = 0; public static const MAX_NUM:uint = 16; } Code that is external to the StaticVars class and any of its subclasses can reference the totalCount and MAX_NUM properties only through the class itself. For example, the following code works: trace(StaticVars.totalCount); // output: 0 trace(StaticVars.MAX_NUM); // output: 16 You cannot access static variables through an instance of the class, so the following code returns errors: var myStaticVars:StaticVars = new StaticVars(); trace(myStaticVars.totalCount); // error trace(myStaticVars.MAX_NUM); // errorVariables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant, as the StaticVars class does for MAX_NUM. You cannot assign a value to MAX_NUM inside the constructor or an instance method. The following code will generate an error, because it is not a valid way to initialize a static constant: // !! Error to initialize static constant this way class StaticVars2 { public static const UNIQUESORT:uint; function initializeStatic():void { UNIQUESORT = 16; } } Instance variables Instance variables include properties declared with the var and const keywords, but without the static keyword. Instance variables, which are attached to class instances rather than to an entire class, are useful for storing values that are specific to an instance. For example, the Array class has an instance property named length, which stores the number of array elements that a particular instance of the Array class holds.

14 Constructors One way in which objects are created is when the new operator is used with a constructor var str1:Info; // str1 is a reference to an object.(we’ll assume from now on the Info class is related to a String class var len:Number; str1 = new Info(“Random Jottings");//create an object of type Info len = str1.length; // invoke the object's method length() Alert.show("The string is " + String(len) + " characters long"); A constructor has the same name as the class Constructors often are used with values (called parameters) There are usually several different constructors in a class, each with different parameters (in AS3 there is only ONE constructor with multiple optional parameters). Sometimes one is more convenient to use than another, depending on how the new object's data is to be initialized.

15 The String Constructor Documentation Constructor Detail String () Constructor public function String(val:String)String Language Version: ActionScript 3.0 Runtime Versions: Flash Player 9, AIR 1.0, Flash Lite 4 Creates a new String object initialized to the specified string. Note: Because string literals use less overhead than String objects and are generally easier to use, you should use string literals instead of the String class unless you have a good reason to use a String object rather than a string literal. Parameters val:String — The initial value of the new String objectString Reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3 /String.html

16 Dot Notation After an object has been constructed it can (usually) be changed by using its own methods (not its constructor.) Some objects are designed so that once they have been constructed they cannot be changed at all (concept of immutability).

17 Practice Program Fill in the blanks to: Create two Info objects. The first Info will get the characters "Green eggs" The second Info will get the characters " and ham." The program will compute the length of each Info, and then Write out the combined length of both Info (the sum of the individual lengths.) var str1:Info; // str1 is a reference to a Info object. var str2:Into; // str2 is a reference to a second Info object. var len1, len2 :Number; // the length of str1 and the length of str2 str1 = new Info(“Green eggs”) ; // create the first Info str2 = new Info(“ and ham.”) ; // create the second Info len1 = str1.length(); // get the length of the first string len2 = str2.length(); // get the length of the second string Alert.show("The combined length of both strings is " + (len1 + len2) + " characters" );

18 Quiz 1. What attributes do all real world objects have? a. Objects have identity, state, and behavior. b. Objects have state and behavior. c. Objects have size and weight. d. Objects have existence. 2. What attributes do all Software objects have? a. Software objects have RAM, ROM, and processors. b. Software objects have identity, state, and behavior. c. Software objects have variables and storage. d. Software objects are made of computer components.

19 3. What is a class? a. A class is a section of computer memory containing objects. b. A class is a section of the hard disk reserved for object oriented programs. c. A class is the part of an object that contains the variables. d. A class is a description of a kind of object. 4. What is another name for creating an object? a. instantiation b. insubordination c. initialization d. inheritance

20 5. What are the static variables and methods of a class? a. Variables and methods that form the foundation of each object of that class. b. Variables and methods that belong to all objects in the computer system. c. Variables and methods that belong only the objects of that class. d. Variables and methods that are part of the class definition, but not of its objects. 6. Which of the following invokes the method length() of the object str and stores the result in val? a. val = str.length() ; b. val = length.str() ; c. val = length().str ; d. val = length( str ) ;

21 7. How many objects of a given class may be constructed in an application? a. Only one per constructor. b. As many as the application asks for. c. Only one per class. d. One object per variable. 8. Which of the following is correct? a. String alpha("Hello Quiz!") ; b. String = "Hello Quiz!" ; c. String alpha = new "Hello Quiz!" ; d. var alpha:String = "Hello Quiz!" ;


Download ppt "What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex."

Similar presentations


Ads by Google