The following example demonstrates the use of interfaces. First we will declare four interfaces: Reader, Singer, Swimmer, and Talker, all in the animal namespace. A new SongBird class will implement Singer, the AquaticBird class will implement Swimmer, and the Human class will implement all four interfaces.
01: xclass.declareInterface("animal.Reader", [ "read" ]);
02: xclass.declareInterface("animal.Singer", [ "sing" ]);
03: xclass.declareInterface("animal.Swimmer", [ "swim" ]);
04: xclass.declareInterface("animal.Talker", [ "talk" ]);05:
06: xclass.declare("animal.Biped", XObject, {
07: constructor: function(name) {
08: console.log("constructing Biped " + name);
09: this.name = name;
10: },
11: walk: function() {
12: console.log("This " + this.name + " is walking!");
13: }
14: });
15:
16: xclass.declare("animal.bird.Bird", animal.Biped, {
17: constructor: function(name) {
18: console.log("constructing Bird " + name);
19: },
20: fly: function() {
21: console.log("This " + this.name + " is flying!");
22: }
23: });
24:25: xclass.declare("animal.bird.SongBird", [ animal.Biped, animal.Singer ], {26: constructor: function(name) {
27: console.log("constructing SongBird " + name);
28: },
29: fly: function() {
30: console.log("This " + this.name + " is flying!");
31: },
32: sing: function() {
33: console.log("This " + this.name + " is singing!");
34: }
35: });
36:37: xclass.declare("animal.bird.aquatic.AquaticBird",
38: [ animal.bird.Bird, animal.Swimmer ], {39: constructor: function(name) {
40: console.log("constructing AquaticBird " + name);
41: },
42: swim: function() {
43: console.log("This " + this.name + " is swimming!");
44: }
45: });
46:47: xclass.declare("animal.mammal.Human", [
48: animal.Biped, animal.Reader, animal.Singer,
49: animal.Swimmer, animal.Talker ], {50: constructor: function(name) {
51: console.log("constructing Human " + name);
52: },
53: read: function() {
54: console.log("This " + this.name + " is reading!");
55: },
56: sing: function() {
57: console.log("This " + this.name + " is singing!");
58: },
59: swim: function() {
60: console.log("This " + this.name + " is swimming!");
61: },
62: talk: function() {
63: console.log("This " + this.name + "is talking!");
64: }
65: });
66:
67: var ape = new animal.Biped("ape");
68: var owl = new animal.bird.Bird("owl");
69: var bluebird = new animal.bird.SongBird("bluebird");
70: bluebird.walk();
71: bluebird.fly();
72: bluebird.sing();
73: var duck = new animal.bird.aquatic.AquaticBird("duck");
74: duck.walk();
75: duck.fly();
76: duck.swim();
77: var human = new animal.mammal.Human("boy");
78: human.walk();
79: human.swim();
80: human.talk();
81: human.read();
82: human.sing();
83: implements("ape", "animal.Reader");
84: implements("human", "animal.Reader");
85: implements("owl", "animal.Singer");
86: implements("bluebird", "animal.Singer");
87: implements("owl", "animal.Swimmer");
88: implements("duck", "animal.Swimmer");
This example will produce the following output on the JavaScript console.
constructing Biped ape
This ape is walking!
constructing Biped owl
constructing Bird owl
This owl is walking!
This owl is flying!
constructing Biped bluebird
constructing SongBird bluebird
This bluebird is walking!
This bluebird is flying!
This bluebird is singing!
constructing Biped duck
constructing Bird duck
constructing AquaticBird duck
This duck is walking!
This duck is flying!
This duck is swimming!
constructing Biped American
constructing Human American
This boy is walking!
This boy is swimming!
This boy is talking!
This boy is reading!
This boy is singing!
ape does not implement Reader
human implements Reader
owl does not implement Singer
bluebird implements Singer
owl does not implement Swimmer
duck implements Swimmer