I'm going through tutorials on dynamic classes. The concept is really exciting and interesting to me.
However this code isn't compiling correctly
dynamic class Person {
var name:String;
}
Person p= new Person();
p.name=”Joe”;
p.age=25;
p.printMe = function () {
trace开发者_C百科 (p.name, p.age);
}
p.printMe(); // Joe 25
I get a 1071 syntax error.
What gives?
Error in syntax; Person p = new Person();
is not valid AS3. It should be var p:Person = new Person();"
EDIT 1: Also, of course if you put your code as-is in the timeline it will not work. The class has to be in a .as file, and the other code must be in the timeline (or in a class function).
EDIT 2: This code works:
//Timeline:
var p:Person = new Person();
p.name="Joe";
p.age=25;
p.printMe = function () {
trace (p.name, p.age);
}
p.printMe(); // Joe 25`
Where Person.as looks like:
package {
public dynamic class Person {
var name:String;
}
}
I can't test it right now, but it looks like there are two errors in your code snippet. First, the variable declaration and second the string quotes (you used ”
instead of "
). The following code should work:
var p:Person = new Person();
p.name = "Joe";
p.age = 25;
p.printMe = function() {
trace (p.name, p.age);
}
p.printMe(); // Joe 25
精彩评论