58
Chapter 2: Creating Custom Classes with ActionScript 2.0
To reference a class that resides in a package directory, you can either specify its fully qualified
class name or import the package by using the
import
statement (see the following section).
Creating and using interfaces
An interface in object-oriented programming is like a class whose methods have been declared,
but otherwise don’t “do” anything. That is, an interface consists of “empty” methods.
One use of interfaces is to enforce a protocol between otherwise unrelated classes. For example,
suppose you’re part of a team of programmers, each of whom is working on a different part—that
is, a different class—of a large application. Most of these classes are unrelated, but you still need a
way for the different classes to communicate. You need to define an interface, or communication
protocol, to which all the classes must adhere.
One way to do this would be to create a class that defines all these methods, and then have each
class
extend
, or inherit from, this superclass. But because the application consists of classes that are
unrelated, it doesn’t make sense to put them all into a common class hierarchy. A better solution is
to create an interface that declares the methods these classes will use to communicate, and then
have each class implement (provide its own definitions for) those methods.
You can usually program successfully without using interfaces. When used appropriately,
however, interfaces can make the design of your applications more elegant, scalable,
and maintainable.
For more information on creating and using interfaces, see the following topics:
•
“Creating an interface” on page 58
•
“Interfaces as data types” on page 59
Creating an interface
The process for creating an interface is the same as for creating a class. As with classes, you can
define interfaces only in external AS files. You declare an interface using the
interface
keyword,
followed by the interface name, and then left and right curly braces ({}), which define the body of
the interface, as shown in the following example:
interface
interfaceName
{
// interface method declarations
}
An interface can contain only method (function) declarations, including parameters, parameter
types, and function return types.
For example, the following code declares an interface named MyInterface that contains two
methods,
method_1()
and
method_2()
. The first method,
method_1()
, has no parameters and
specifies a return type of
Void
(meaning it does not return a value). The second method,
method_2()
, has a single parameter of type String, and specifies a return type of Boolean.
interface MyInterface {
function method_1():Void;
function method_2(param:String):Boolean;
}
Содержание FLEX-FLEX ACTIONSCRIPT LANGUAGE
Страница 1: ...Flex ActionScript Language Reference...
Страница 8: ......
Страница 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Страница 76: ......
Страница 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Страница 135: ...case 135 See also break default strict equality switch...
Страница 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Страница 229: ...while 229 i 3 The following result is written to the log file 0 3 6 9 12 15 18 See also do while continue for for in...
Страница 808: ...808 Chapter 7 ActionScript for Flash...
Страница 810: ...810 Appendix A Deprecated Flash 4 operators...
Страница 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Страница 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Страница 822: ...822 Index...