Local Variables
Local variables are used to track information temporarily within the physical confines
of a constructor method, an instance method, a static method, or a function.
We haven’t studied instance methods, static methods, or functions yet so for now
we’ll focus on local variables in constructor methods.
To create a local variable within a constructor method, we use a variable definition,
as shown in the following generalized code. Notice that the definition starts with the
keyword var and, by convention, ends in a semicolon, as do all directives that do not
include a block statement. The semicolon indicates the end of the directive, much
like the period at the end of a sentence in a natural language.
class SomeClass {
public function SomeClass ( ) {
var identifier = value;
}
}
In the preceding code, identifier is the local variable’s name, and value is the value
associated with that variable. Together, the equals sign and the value are known as
the variable initializer because they determine the initial value of the variable.
20 | Chapter 1: Core Concepts
Associating a variable with a value is known as assigning, setting, or
writing the variable’s value.
When the variable initializer is omitted, ActionScript automatically assigns the variable
a default value. Default values for variables are discussed in Chapter 8.
A local variable can be used within the method or function that contains its definition
only. When the method or function finishes executing, the local variable expires
and can no longer be used by the program.
Let’s create a local variable to refer to the VirtualPet object we created earlier in the
VirtualZoo constructor. We’ll name the local variable pet, and we’ll use an initializer
to associate it with the VirtualPet object. Here’s the code:
package zoo {
public class VirtualZoo {
public function VirtualZoo ( ) {
var pet = new VirtualPet;
}
}
}
Having associated the local variable pet with a VirtualPet object, we can now use
that variable to refer to, and therefore control, that object. However, currently our
VirtualPet object can’t actually do anything because we haven’t programmed its
functionality. We’ll start to rectify that shortcoming in the next section by giving pets
the ability to have nicknames.
Instance Variables
Earlier we learned that a class describes the characteristics and behavior of a particular
type of object. In object-oriented programming terms, a “characteristic” is a specific
piece of information (i.e., value) that describes some aspect of an object—such
as its width, speed, or color. To keep track of an object’s characteristics, we use
instance variables.
An instance variable is a variable attached to a particular object. Typically, each
instance variable describes a characteristic of the object to which it is attached. For
example, an instance variable might be the identifier width associated with the value
150, describing the size of the button object in an interface. Or, an instance variable
might be the identifier shippingAddress associated with the value “34 Somewhere
St,” describing the destination of a product-order object.
Instance variables are created using variable definitions directly within class definitions,
as shown in the following generalized code
Local Variables
Local variables are used to track information temporarily within the physical confines
of a constructor method, an instance method, a static method, or a function.
We haven’t studied instance methods, static methods, or functions yet so for now
we’ll focus on local variables in constructor methods.
To create a local variable within a constructor method, we use a variable definition,
as shown in the following generalized code. Notice that the definition starts with the
keyword var and, by convention, ends in a semicolon, as do all directives that do not
include a block statement. The semicolon indicates the end of the directive, much
like the period at the end of a sentence in a natural language.
class SomeClass {
public function SomeClass ( ) {
var identifier = value;
}
}
In the preceding code, identifier is the local variable’s name, and value is the value
associated with that variable. Together, the equals sign and the value are known as
the variable initializer because they determine the initial value of the variable.
20 | Chapter 1: Core Concepts
Associating a variable with a value is known as assigning, setting, or
writing the variable’s value.
When the variable initializer is omitted, ActionScript automatically assigns the variable
a default value. Default values for variables are discussed in Chapter 8.
A local variable can be used within the method or function that contains its definition
only. When the method or function finishes executing, the local variable expires
and can no longer be used by the program.
Let’s create a local variable to refer to the VirtualPet object we created earlier in the
VirtualZoo constructor. We’ll name the local variable pet, and we’ll use an initializer
to associate it with the VirtualPet object. Here’s the code:
package zoo {
public class VirtualZoo {
public function VirtualZoo ( ) {
var pet = new VirtualPet;
}
}
}
Having associated the local variable pet with a VirtualPet object, we can now use
that variable to refer to, and therefore control, that object. However, currently our
VirtualPet object can’t actually do anything because we haven’t programmed its
functionality. We’ll start to rectify that shortcoming in the next section by giving pets
the ability to have nicknames.
Instance Variables
Earlier we learned that a class describes the characteristics and behavior of a particular
type of object. In object-oriented programming terms, a “characteristic” is a specific
piece of information (i.e., value) that describes some aspect of an object—such
as its width, speed, or color. To keep track of an object’s characteristics, we use
instance variables.
An instance variable is a variable attached to a particular object. Typically, each
instance variable describes a characteristic of the object to which it is attached. For
example, an instance variable might be the identifier width associated with the value
150, describing the size of the button object in an interface. Or, an instance variable
might be the identifier shippingAddress associated with the value “34 Somewhere
St,” describing the destination of a product-order object.
Instance variables are created using variable definitions directly within class definitions,
as shown in the following generalized code
การแปล กรุณารอสักครู่..
