W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
此功能僅在Ultimate版本中受支持。
本節(jié)介紹特定于ActionScript的重構(gòu),在本節(jié)中將介紹兩個(gè)主要的內(nèi)容,它們分別是:
有關(guān)可用于ActionScript的重構(gòu)的完整列表,請(qǐng)參閱ActionScript和Flex。
在 ActionScript 中,您可以使用 Change Method Signature 重構(gòu)來(lái)實(shí)現(xiàn)以下的目標(biāo):
更改方法簽名之前的代碼如下所示:
// The function paint() is declared in
// the IShape interface.
public interface IShape {
function paint(g: Graphics): void;
}
// This function is then called within the
// paint() function of the Canvas class.
public class Canvas {
private var shapes: Vector.<IShape>;
public function paint(g: Graphics): void {
for each (var shape: IShape in shapes) {
shape.paint(g);
}
}
}
// Now we are going to show an example of the
// Change Signature refactoring for the function
// paint() of the IShape interface.
更改方法簽名之后的代碼則為:
// In this refactoring example we have changed the name of the existing parameter
// and introduced two new parameters. Note that the first of the new parameters is
// a required parameter while the second is optional because the default value
// for it is specified in the function definition.
public interface IShape {
function paint(graphics:Graphics, wireframe:Boolean, offset:Point = null):void;
}
// When performing this refactoring, the new parameters were propagated to
// the paint() function of the Canvas class. As a result, the signature of
// Canvas.paint() has changed. Also note how IShape.paint() within
// Canvas.paint() is called now.
public class Canvas {
private var shapes: Vector.<IShape>;
public function paint(g:Graphics, wireframe:Boolean): void {
for each (var shape: IShape in shapes) {
shape.paint(g, wireframe);
}
}
}
// Other results for this refactoring are possible.
// For more information, see the discussion that follows.
對(duì)于添加到函數(shù)的每個(gè)新參數(shù),可以指定:
你還可以將已引入的參數(shù)傳播給調(diào)用正在更改其簽名的函數(shù)的函數(shù)。
重構(gòu)結(jié)果取決于您是否指定這些值并使用傳播。
傳播(Propagation)可以將新參數(shù)傳播到調(diào)用您正在更改其簽名的函數(shù)的任何函數(shù)。在這種情況下,調(diào)用函數(shù)的簽名通常會(huì)相應(yīng)地改變。但是,這些更改還取決于初始值設(shè)定項(xiàng)和新參數(shù)設(shè)置的默認(rèn)值的組合。
初始化程序(Initializer)。在初始化程序字段中指定的值將作為默認(rèn)參數(shù)值添加到函數(shù)定義中。這使相應(yīng)的參數(shù)成為可選參數(shù)。
如果沒有指定新參數(shù)的默認(rèn)值(在 Default value 字段中),則無(wú)論是否使用傳播,函數(shù)調(diào)用和調(diào)用函數(shù)的簽名都不會(huì)更改。
如果同時(shí)指定了初始值設(shè)定項(xiàng)和默認(rèn)值,則重構(gòu)結(jié)果取決于是否使用傳播:
默認(rèn)值(Default value)。通常,這是要添加到函數(shù)調(diào)用的值。
如果新參數(shù)未傳播給調(diào)用函數(shù),則此函數(shù)內(nèi)的函數(shù)調(diào)用也將使用此值。
如果使用傳播,則此值對(duì)于調(diào)用函數(shù)內(nèi)的函數(shù)調(diào)用無(wú)關(guān)緊要。
要查看上面討論的不同重構(gòu)設(shè)置如何影響重構(gòu)結(jié)果, 請(qǐng)讓我們考慮以下示例。
所有示例都是前面所示的重構(gòu)的簡(jiǎn)化版本。在所有情況下, 將類型布爾值的新參數(shù)線框添加到 IShape 接口中定義的函數(shù)上色 () 中。
在不同的示例中, 使用初始值設(shè)定項(xiàng)和默認(rèn)值的不同組合, 并將新參數(shù)傳播到畫布. 顏料 () (稱為 IShape. 顏料 ())。
要了解上面討論的不同重構(gòu)設(shè)置如何影響重構(gòu)結(jié)果,請(qǐng)讓我們考慮以下示例。
所有示例都是前面所示重構(gòu)的簡(jiǎn)化版本。在任何情況下,該 Boolean 類型的新參數(shù) wireframe 都會(huì)添加到 IShape 接口中定義的 paint() 函數(shù)中。
在不同的例子中,使用了初始值和默認(rèn)值的不同組合,并且新參數(shù)可以傳播到 Canvas.paint()(調(diào)用 IShape.paint())或者不傳播:
初始化 | 默認(rèn)值 | 是否使用傳播 | 結(jié)果 |
---|---|---|---|
false
|
是 |
|
|
false
|
否 |
|
|
true
|
是 |
|
|
true
|
否 |
|
|
true
|
false
|
是 |
|
true
|
false
|
否 |
|
您可以:
代碼完成可在此字段中使用,也可在包含函數(shù)參數(shù)的表的某些字段中使用。
添加參數(shù)時(shí),可能需要將這些參數(shù)傳播給調(diào)用當(dāng)前方法的方法。
(可能有一些方法會(huì)調(diào)用您正在更改簽名的方法,這些方法反過來(lái)可能會(huì)被其他方法調(diào)用,等等。您可以將正在進(jìn)行的更改傳播到當(dāng)前方法的參數(shù)中調(diào)用方法的層次結(jié)構(gòu),并指定哪些調(diào)用方法應(yīng)該受到影響,哪些不應(yīng)調(diào)用。)
傳播新參數(shù):
為了幫助您選擇必要的方法,調(diào)用方法和被調(diào)用方法的代碼顯示在對(duì)話框的右側(cè)部分(分別位于“調(diào)用方法”和“被調(diào)用方法” 窗格中)。
當(dāng)您在左側(cè)窗格中切換方法時(shí),右側(cè)部分的代碼會(huì)相應(yīng)更改。
要在實(shí)際執(zhí)行重構(gòu)之前查看預(yù)期的更改并進(jìn)行必要的調(diào)整,請(qǐng)單擊“預(yù)覽(Preview)”。
本部分討論ActionScript中的提取參數(shù)重構(gòu)。
在進(jìn)行提取操作之前的代碼如下所示:
// Two ways of extracting a parameter for the function
// formatPrice() will be shown.
public function foo():void {
formatPrice(0);
}
// The new parameter will be optional in the first
// of the examples and required in the second example.
public function formatPrice(value:int):String {
trace("currency: " + "$");
return "$" + value;
}
提取之后:
// The function formatPrice() may be called as before because
// the new parameter is introduced as an optional parameter.
public function foo():void {
formatPrice(0);
}
// The default value for the new parameter is specified
// in the function definition.
public function formatPrice(value:int, s:String = "$"):String {
trace("currency: " + s);
return s + value;
}
// The new parameter in this example is a required parameter.
// So two values must be passed to formatPrice() now.
public function foo():void {
formatPrice(0, "$");
}
// The new parameter is a required parameter because the default
// value for it is not specified in the function definition.
public function formatPrice(value:int, s:String):String {
trace("currency: " + s);
return s + value;
}
如果新參數(shù)將成為可選參數(shù),則將指定的值用作函數(shù)定義中的默認(rèn)參數(shù)值。
如果新參數(shù)作為必需參數(shù)引入,則將指定的值添加到函數(shù)調(diào)用中。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: