162
Chapter 9: Creating Classes with ActionScript 2.0
Similarly, any function declared within a class is considered a method of the class. In the Person
class example, you created a single method called
showInfo()
.
class Person {
var age:Number;
var name:String;
function showInfo() {
// showInfo() method definition
}
}
Initializing properties inline
You can initialize properties
inline
—that is, when you declare them—with default values, as
shown here:
class Person {
var age:Number = 50;
var name:String = "John Doe";
}
When you initialize properties inline the expression on the right side of an assignment must be a
compile-time constant
. That is, the expression cannot refer to anything that is set or defined at
runtime. Compile-time constants include string literals, numbers, Boolean values,
null
, and
undefined
, as well as constructor functions for the following built-in classes: Array, Boolean,
Number, Object, and String.
For example, the following class definition initializes several properties inline:
class CompileTimeTest {
var foo:String = "my foo"; // OK
var bar:Number = 5; // OK
var bool:Boolean = true; // OK
var name:String = new String("Jane"); // OK
var who:String = foo; // OK, because 'foo' is a constant
var whee:String = myFunc(); // error! not compile-time constant expression
var lala:Number = whee; // error! not compile-time constant expression
var star:Number = bar + 25; // OK, both 'bar' and '25' are constants
function myFunc() {
return "Hello world";
}
}
This rule only applies to instance variables (variables that are copied into each instance of a class),
not class variables (variables that belong to the class itself ). For more information about these
kinds of variables, see
“Instance and class members” on page 165
.
Creating subclasses
In object-oriented programming, a subclass can inherit the properties and methods of another
class, called the superclass. To create this kind of relationship between two classes, you use the
class
statement’s
extends
clause. To specify a superclass, use the following syntax:
class
SubClass
extends
SuperClass
{}
Содержание FLASH MX 2004 - ACTIONSCRIPT
Страница 1: ...ActionScript Reference Guide...
Страница 8: ...8 Contents...
Страница 12: ......
Страница 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Страница 54: ...54 Chapter 2 ActionScript Basics...
Страница 80: ...80 Chapter 3 Writing and Debugging Scripts...
Страница 82: ......
Страница 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Страница 112: ......
Страница 120: ...120 Chapter 6 Using the Built In Classes...
Страница 176: ......
Страница 192: ...192 Chapter 10 Working with External Data...
Страница 202: ...202 Chapter 11 Working with External Media...
Страница 204: ......
Страница 782: ...782 Chapter 12 ActionScript Dictionary...
Страница 793: ...Other keys 793 221 222 Key Key code...
Страница 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Страница 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Страница 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Страница 816: ...816 Index...