Abstract - 抽象クラスの利用
スポンサードリンク
この機能を実行するにはprototype.jsが必要です
構文
Abstract
「抽象クラスの利用」サンプルコード
1 | <html> |
2 | |
3 | <head> |
4 | <title>Abstract - 抽象クラスの利用</title> |
5 | </head> |
6 | |
7 | |
8 | <!--抽象クラスの利用のサンプル--> |
9 | <script type="text/javascript" src="/js/prototype.js"></script> |
10 | <script type="text/javascript"> |
11 | <!-- |
12 | |
13 | Abstract.Creature = function() {} // 抽象クラスの作成 |
14 | Abstract.Creature.prototype = { |
15 | punch : function () { |
16 | alert('punch'); |
17 | }, |
18 | eat : function () {} |
19 | }; |
20 | |
21 | var Animal = Class.create(); |
22 | |
23 | Animal.prototype = Object.extend(new Abstract.Creature, { |
24 | initialize: function (name) { |
25 | this.name = name; |
26 | }, |
27 | kick : function () { |
28 | alert('kick'); |
29 | }, |
30 | eat : function () { |
31 | alert('uooooooon.'); |
32 | } |
33 | }); |
34 | |
35 | var Person = Class.create(); |
36 | Person.prototype = Object.extend(new Animal, { |
37 | initialize : function(name, message) { |
38 | this.name = name; |
39 | this.message = message; |
40 | }, |
41 | say : function() { |
42 | alert('hey, my name is '+this.name+' , '+this.message); |
43 | }, |
44 | kick : function () { |
45 | alert('i am person'); |
46 | Animal.prototype.kick.apply(this); // 親クラスの同名メソッド呼び出し |
47 | }, |
48 | eat : function () { |
49 | alert('delicious'); |
50 | } |
51 | }); |
52 | |
53 | function execute() { |
54 | var monkey = new Animal('monkey'); |
55 | monkey.kick(); |
56 | monkey.punch(); |
57 | monkey.eat(); |
58 | |
59 | var kenji = new Person('kenji', 'hello'); |
60 | kenji.punch(); |
61 | kenji.kick(); |
62 | kenji.say(); |
63 | kenji.eat(); |
64 | } |
65 | |
66 | //--> |
67 | </script> |
68 | <body> |
69 | <button onclick="execute()">サンプル開始</button> |
70 | </body> |
71 | |
72 | </html> |
ポップアップ | 印刷 | ←ポップアップでソースコードを開き、簡単にコピーできます | ? |
スポンサードリンク
「Abstractの動作デモ」
新しいウィンドウで実行 | スクリプトを編集&実行 - スクリプトをWEB上で編集&動作確認することが出来ます。
スポンサードリンク