2009年4月20日
將myeclipse"eclipse"links下的com.genuitec.eclipse.MyEclipse.link里的內容由具體的磁盤地址改為相對地址即可:
path=../myeclipse
如果你想在myeclipse目錄下就啟動它,那么加一個bat文件,內容:
start eclipse/eclipse.exe -vm jre/bin/javaw.exe
2009年3月18日
dojo之面向對象編程
Dojo作為一個強大的javascript工具箱,有它自己面向對象的開發方式,用declare解決了對象的創建和繼承的問題,文檔中的例子:
 dojo.declare("my.classes.bar", my.classes.foo, {
// properties to be added to the class prototype
someValue: 2,
// initialization function
 constructor: function() {
this.myComplicatedObject = new ReallyComplicatedObject();
},
// other functions
 someMethod: function() {
doStuff();
}
}
);
declare的第一個參數是對象名稱,最后一個參數指定在這個對象里要添加的內容,包括函數和屬性,寫個例子
 dojo.declare("Apple", null, {
price: 5,
 constructor: function(weight) {
this.total = weight * this.price;
},
 print: function() {
alert("The total price is " + this.total);
}
}
);
var myapple = new Apple(10);
myapple.print(); //輸出結果:"The total price is 50"
上例通過declare創建了一個Apple對象,javascript本身沒有類的概念,可以使用對象本身來創建新的對象myapple,通過構造函數的參數值計算蘋果的總價,print函數輸出結果,非常形象的構建了一個Apple“類”,非常容易理解。要注意的是,這里如果聲明默認構造函數, "new Apple(10)"將直接執行默認構造函數,帶參數的構造函數就被忽略了,并非C++中順序執行。
注意 dojo.declare第二個參數,如果創建一個獨立的新對象,可以設為null,當需要從其他一個或多個對象繼承時,則為對象名稱,這樣就方便的實現了對象繼承。多個對象繼承,declare第二個參數為一數組,第一個元素為原型父對象,其他的為mixin對象,通過代碼來理解。
 <script>
 dojo.declare("Apple", null, {
price : 5,
 constructor : function(weight) {
this.total = weight * this.price;
},
// constructor : function() {
// alert("Create Apple !");
// },
 print : function() {
alert("The total price is " + this.total);
}
}
);
 dojo.declare("AppleTree", null, {
 constructor : function() {
alert("Create AppleTree !");
},
 print : function() {
alert("This is an apple tree");
},
 additional : function() {
alert("This is a mixin class");
}
}
);
 dojo.declare("GreenApple", [Apple, AppleTree], {
 constructor : function() {
alert("Getting a green apple");
}
}
);
</script>
創建一個GreenApple對象,測試alert執行順序!mixin對象的方法將覆蓋之前對象中的同名函數,除非子對象也聲明了同名函數print。
//輸出
//"The height of the tree is #ff0000"
//"Getting a green apple"
var gapple = new GreenApple();
//輸出,覆蓋了Apple對象的print
//"This is an apple tree"
gapple.print();
//"This is a mixin class"
gapple.additional();
dojo/_base/_loader/bootstrap.js有專門的mixin函數,用于對象的拷貝,將一個創建好的對象拷貝到新的對象中
 var copy = dojo.mixin( {}, new Apple(2));
copy.print();
print輸出結果是"The total price is 10",mixin參數一定是創建好的對象實例,否則出錯! dojo.extend則可以將一個或多個對象的屬性、方法拷貝到一個原型上,通過prototype實現繼承,這是繼承的另外一種方式。
通過declare、mixin、extend, dojo給我們提供了一種方便的對象創建與擴展機制,一般情況下夠用了,感覺還是比較方便,使用時也存在一些限制,翻翻源代碼就能理解。這里主要是要知道 dojo是如何面向對象的,方便我們更好的理解 dojo基礎功能,及dijit和dojox, dojo最為強大還是它的widgets。本文涉及的js源碼:
mixin: dojo/_base/_loader/bootstrap.js
extend: dojo/_base/lang.js
declare: dojo/_base/declare.js
ExtJS之面向對象編程
1:支持命名空間
<script type="text/javascript">
// 定義一個命名空間
Ext.namespace("Ext.wentao");
// 在命名空間上定義一個類
Ext.wentao.helloworld = Ext.emptyFn;
// 創建一個類的實例
new Ext.wentao.helloworld();
</script>
其中
Ext.wentao.helloworld = Ext.emptyFn;
等價于
Ext.wentao.helloworld = function(){};
2:支持類實例屬性
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 為自定義的類 增加一個 name 屬性,并賦值
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤"
});
var _person = new Ext.wentao.Person();// 實例化 自定義類
alert(_person.name);
_person.name = "張三";// 修改類name屬性
alert(_person.name);
</script>
3:支持類實例方法
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 演示類實例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤",
sex : "男",
print : function() {
alert(String.format("姓名:{0},性別:{1}", this.name, this.sex));
}
});
var _person = new Ext.wentao.Person();// 實例化 自定義類
_person.print();
</script>
4:支持類靜態方法
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 演示類實例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤",
sex : "男",
print : function() {
alert(String.format("姓名:{0},性別:{1}", this.name, this.sex));
}
});
// 演示 類靜態方法
Ext.wentao.Person.print = function(_name, _sex) {
var _person = new Ext.wentao.Person();
_person.name = _name;
_person.sex = _sex;
_person.print(); // 此處調用類 實例方法,上面print是類 靜態方法
};
Ext.wentao.Person.print("張三", "女"); // 調用類 靜態方法
</script>
5:支持構造方法
<script type="text/javascript">
Ext.namespace("Ext.wentao"); //自定義一個命名空間

//構造方法
Ext.wentao.Person = function(_cfg){
Ext.apply(this,_cfg);
};

//演示類實例方法
Ext.apply(Ext.wentao.Person.prototype, {
print:function(){
alert(String.format("姓名:{0},性別:{1}",this.name,this.sex));
}
});
//演示 類靜態方法
Ext.wentao.Person.print = function(_name,_sex){
var _person = new Ext.wentao.Person({name:_name,sex:_sex});
_person.print(); //此處調用類 實例方法,上面print是類 靜態方法
};

Ext.wentao.Person.print("張三","女"); //調用類 靜態方法
</script>
6:支持類繼承
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
// *******************父類*********************
// 構造方法
Ext.wentao.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實例方法
Ext.apply(Ext.wentao.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Ext.wentao.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
Ext.extend(Ext.wentao.Student, Ext.wentao.Person, {
job : "學生"
});
var _student = new Ext.wentao.Student({
name : "張三",
sex : "女"
});
_student.print(); // 調用 父類方法
</script>
7:支持類實例方法重寫
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
// *******************父類*********************
// 構造方法
Ext.wentao.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實例方法
Ext.apply(Ext.wentao.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Ext.wentao.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
// 重寫父類的 實例 方法
Ext.extend(Ext.wentao.Student, Ext.wentao.Person, {
job : "學生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new Ext.wentao.Student({
name : "張三",
sex : "女"
});
_student.print(); // 調用 父類方法
</script>
8:支持命名空間別名
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Wt = Ext.wentao; // 命名空間的別名
// *******************父類*********************
// 構造方法
Wt.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實例方法
Ext.apply(Wt.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Wt.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
// 重寫父類的 實例 方法
Ext.extend(Wt.Student, Ext.wentao.Person, {
job : "學生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new Wt.Student({
name : "張q三",
sex : "女"
});
_student.print(); // 調用 父類方法
</script>
9:支持類別名
<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Wt = Ext.wentao; // 命名空間的別名
// *******************父類*********************
// 構造方法
Wt.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
PN = Wt.Person; // 類別名
// 演示類實例方法
Ext.apply(PN.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Wt.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
ST = Wt.Student;
// 重寫父類的 實例 方法
Ext.extend(ST, PN, {
job : "學生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new ST({
name : "張q三",
sex : "女"
});
_student.print(); // 調用 父類方法
</script>
看看firefox支持不.
firefox中tab不能用
字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數字數
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 |
|
公告
本博客中未注原創的文章均為轉載,對轉載內容可能做了些修改和增加圖片注釋,如果侵犯了您的版權,或沒有注明原作者,請諒解。
這里作為分享知識經驗之用,歡迎各位讀者提出寶貴意見。
常用鏈接
留言簿(2)
隨筆分類
隨筆檔案
文章檔案
相冊
搜索
最新評論

閱讀排行榜
評論排行榜
|
|