import com.adobe.serialization.json.JSON; /* Achtung: In AS3 gilt die Prototype-Benutzung als veraltet! */ trace("AS3 (Flash CS5): OO 01: Classes\n============================"); trace ( "\n---------"+ "\nObjekt o1"+ "\n---------" ); var Point1 = function(x, y) { this.x = x; this.y = y; }; //oder //function Point1(x,y) //{ // this.x = x; // this.y = y; //} Point1.prototype = { move: function(dx, dy) {this.x += dx; this.y += dy;} }; var o1 = new Point1(5,11); trace("o1: " + JSON.encode(o1)); o1.move(7,4); trace("o1: " + JSON.encode(o1)); trace("o1.move: " + o1.move); o1.x = 7; trace("o1.x: " + o1.x); trace ( "\n---------"+ "\nObjekt o2"+ "\n---------" ); // function Point2(p_x, p_y) // Fehler: Methode kann nicht als Konstruktor verwendet werden var Point2 = function Point2(p_x, p_y) { var x=p_x; var y=p_y; this.move = function(p_dx, p_dy) { x += p_dx; y += p_dy; }; this.getX = function() { return x; }; this.getY = function() { return y; }; } var o2 = new Point2(2,2); trace("o2: " + o2.getX() + " " + o2.getY()); o2.move(-1,2); trace("o2: " + o2.getX() + " " + o2.getY()); o2.x = 7; trace("o2.x: " + o2.x + ", o2.getX(): " + o2.getX()); trace("o2: " + JSON.encode(o2)); trace ( "\n---------"+ "\nObjekt o3"+ "\n---------" ); // function Point3(p_x, p_y) // Fehler: Methode kann nicht als Konstruktor verwendet werden var Point3 = function(p_x,p_y) { this.v_x = p_x; this.v_y = p_y; } Point3.prototype = { move: function(p_dx, p_dy) { this.v_x += p_dx; this.v_y += p_dy; }, getX: function() { return this.v_x; }, getY: function() { return this.v_y; } }; var o3 = new Point3(5,11); trace("o3: " + o3.getX() + " " + o3.getY()); o3.move(7,4); trace("o3: " + o3.getX() + " " + o3.getY()); trace("o3: " + JSON.encode(o3)); /* trace ( "\n---------"+ "\nObjekt o4"+ "\n---------" ); function Point4(p_x,p_y) { this.v_x = p_x; this.v_y = p_y; } Point4.prototype = { move: function(p_dx, p_dy) { this.v_x += p_dx; this.v_y += p_dy; }, get x() { return this.v_x; }, // funtioniert in AS3 nicht get y() { return this.v_y; }, // funtioniert in AS3 nicht set x(p_x) { this.v_x = p_x; } }; var o4 = new Point4(20,-17); trace("o4: " + o4.x + " " + o4.y); o4.move(7,4); trace("o4: " + o4.x + " " + o4.y); trace("o4: " + JSON.encode(o4)); o4.x = 7; trace("o4.x: " + o4.x); o4.y = 7; // Fehler trace("o4.y: " + o4.y); */ /* */