This is drectly from Dartlang.tour 27.9.2012, spec is just changing
dartlang.org check last changes!
Methods
Methods are functions that provide behavior for an object.
Instance methods
Instance methods on objects can access instance variables and
this
. The distanceTo() method in the following sample is an example of an instance method.#import('dart:math');
class Point {
num x, y;
Point(this.x, this.y);
num distanceTo(Point other) {
return sqrt(((x-other.x)*(x-other.x)) + ((y-other.y)*(y-other.y)));
}
}
Here’s how you invoke the distanceTo() method on an instance of Point:
var point = new Point(2, 2);
num distance = point.distanceTo(new Point(4,4));
assert(distance < 2.9 && distance > 2.8);
Ei kommentteja:
Lähetä kommentti