特定于ActionScript的重構(gòu)

2018-03-05 10:26 更新

此功能僅在Ultimate版本中受支持。

本節(jié)介紹特定于ActionScript的重構(gòu),在本節(jié)中將介紹兩個(gè)主要的內(nèi)容,它們分別是:

  • 在 ActionScript 中更改方法簽名
  • 在 ActionScript 中提取參數(shù)

有關(guān)可用于ActionScript的重構(gòu)的完整列表,請(qǐng)參閱ActionScript和Flex。

在ActionScript中更改方法簽名

在 ActionScript 中,您可以使用 Change Method Signature 重構(gòu)來(lái)實(shí)現(xiàn)以下的目標(biāo):

  • 更改方法名稱和返回類型。
  • 添加新的參數(shù)并刪除現(xiàn)有的參數(shù)。請(qǐng)注意,您也可以使用專用的 Extract Parameter 重構(gòu)來(lái)添加參數(shù) 。
  • 重新排序參數(shù)。
  • 更改參數(shù)名稱和類型。
  • 通過方法調(diào)用層次結(jié)構(gòu)傳播新參數(shù)。

ActionScript中更改方法簽名示例

更改方法簽名之前的代碼如下所示:

// 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.

初始化器,默認(rèn)值以及新參數(shù)的傳播

對(duì)于添加到函數(shù)的每個(gè)新參數(shù),可以指定:

  • 用于初始化參數(shù)的值(或表達(dá)式)(IntelliJ IDEA 中的 Initializer 字段)。
  • 默認(rèn)值(或表達(dá)式)(默認(rèn)值字段)。

你還可以將已引入的參數(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é)果取決于是否使用傳播:

  • 如果未使用傳播,則初始化器值不會(huì)影響函數(shù)調(diào)用和調(diào)用函數(shù)的簽名。
  • 如果使用傳播,則將初始化值添加到調(diào)用函數(shù)的定義中作為相應(yīng)參數(shù)的默認(rèn)值(與您正在更改其簽名的函數(shù)中的方式相同)。

默認(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)示例

要查看上面討論的不同重構(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
public interface IShape {
int(g:Graphics, wireframe:Boolean):void;


 paint() in the Canvas class:

tion paint(g:Graphics,
           wireframe:Boolean): void {
h (var shape: IShape in shapes) {
pe.paint(g, wireframe);
false
public interface IShape {
int(g:Graphics,
    wireframe:Boolean):void;


 paint() in the Canvas class:

tion paint(g:Graphics): void {
h (var shape: IShape in shapes) {
pe.paint(g, false);
true
public interface IShape {
int(g:Graphics,
    wireframe:Boolean = true):void;


 paint() in the Canvas class:

tion paint(g:Graphics): void {
h (var shape: IShape in shapes) {
pe.paint(g);
true
public interface IShape {
int(g:Graphics,
    wireframe:Boolean = true):void;


 paint() in the Canvas class:

tion paint(g:Graphics): void {
h (var shape: IShape in shapes) {
pe.paint(g);
true false
public interface IShape {
int(g:Graphics,
    wireframe:Boolean = true):void;


 paint() in the Canvas class:

tion paint(g:Graphics,
           wireframe:Boolean = true): void {
h (var shape: IShape in shapes) {
pe.paint(g, wireframe);
true false
public interface IShape {
int(g:Graphics,
    wireframe:Boolean = true):void;


 paint() in the Canvas class:

tion paint(g:Graphics): void {
h (var shape: IShape in shapes) {
pe.paint(g, false);

更改方法簽名

  1. 在編輯器中,將光標(biāo)放在要更改其簽名的方法的名稱內(nèi)。
  2. 執(zhí)行以下操作之一:
    • 按 Ctrl+F6。
    • 在主菜單中選擇:重構(gòu)|更改簽名(Refactor | Change Signature)。
    • 從上下文菜單中選擇:重構(gòu)|更改簽名(Refactor | Change Signature)。
  3. 在“更改簽名(Change Signature)”對(duì)話框中,對(duì)方法簽名進(jìn)行必要的更改并指定需要進(jìn)行哪些其他相關(guān)更改。

    您可以:

    • 通過編輯返回類型字段的內(nèi)容來(lái)更改方法返回類型。

      代碼完成可在此字段中使用,也可在包含函數(shù)參數(shù)的表的某些字段中使用。

    • 更改方法名稱。為實(shí)現(xiàn)該目標(biāo),請(qǐng)編輯名稱字段中的文本。
    • 使用參數(shù)表和它右側(cè)的按鈕管理方法參數(shù):
      • 要添加新參數(shù),請(qǐng)單擊 (Alt+Insert)并在相應(yīng)字段中指定新參數(shù)的屬性。

        添加參數(shù)時(shí),可能需要將這些參數(shù)傳播給調(diào)用當(dāng)前方法的方法。

      • 要?jiǎng)h除參數(shù),請(qǐng)單擊相應(yīng)行中的任意單元格并單擊 (Alt+Delete)。
      • 要重新排序參數(shù),請(qǐng)使用 (Alt+Up)和 (Alt+Down)。例如,如果您想要將某個(gè)參數(shù)設(shè)置為列表中的第一個(gè)參數(shù),則可以單擊 與該參數(shù)對(duì)應(yīng)的行中的任意單元格,然后單擊所需的次數(shù)。
      • 要更改參數(shù)的名稱或類型,請(qǐng)?jiān)谙鄳?yīng)的表格單元格中進(jìn)行必要的編輯。
    • 沿著調(diào)用當(dāng)前方法的方法的層次結(jié)構(gòu)傳播新的方法參數(shù)(如果有的話)。

      (可能有一些方法會(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ù):

      1. 點(diǎn)擊 (Alt+G)。
      2. 在“選擇傳播新參數(shù)的方法(Select Methods to Propagate New Parameters)”對(duì)話框的左側(cè)窗格中 ,展開必要的節(jié)點(diǎn),并選擇希望傳播新參數(shù)的方法旁邊的復(fù)選框。

        為了幫助您選擇必要的方法,調(diào)用方法和被調(diào)用方法的代碼顯示在對(duì)話框的右側(cè)部分(分別位于“調(diào)用方法”和“被調(diào)用方法” 窗格中)。

        當(dāng)您在左側(cè)窗格中切換方法時(shí),右側(cè)部分的代碼會(huì)相應(yīng)更改。

      3. 點(diǎn)擊“確定”。
  4. 要立即執(zhí)行重構(gòu),請(qǐng)單擊“重構(gòu)(Refactor)”。

    要在實(shí)際執(zhí)行重構(gòu)之前查看預(yù)期的更改并進(jìn)行必要的調(diào)整,請(qǐng)單擊“預(yù)覽(Preview)”。

在ActionScript中提取參數(shù)

本部分討論ActionScript中的提取參數(shù)重構(gòu)。

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;
}

在ActionScript中提取參數(shù)

  1. 在編輯器中,將光標(biāo)放在表達(dá)式中以被參數(shù)替換。
  2. 執(zhí)行以下操作之一:
    • 按:Ctrl+Alt+P。
    • 在主菜單上選擇:重構(gòu)| 提取| 參數(shù)(Refactor | Extract | Parameter)。
    • 在上下文菜單中選擇:重構(gòu)| 提取| 參數(shù)(Refactor | Extract | Parameter)。
  3. 如果檢測(cè)到當(dāng)前光標(biāo)位置有多個(gè)表達(dá)式,則出現(xiàn)“表達(dá)式(Expressions)”列表。如果是這種情況,請(qǐng)選擇所需的表達(dá)式。為此,請(qǐng)單擊表達(dá)式?;蛘?,使用Up 和 Down 箭頭鍵導(dǎo)航到感興趣的表達(dá)式,然后按 Enter 來(lái)選擇它。
    在ActionScript中提取參數(shù)
  4. 在“提取參數(shù)(Extract Parameter)”對(duì)話框中:
    1. 通常,IntelliJ IDEA 會(huì)自行設(shè)置適當(dāng)?shù)膮?shù)類型。如有必要,您可以從類型列表中選擇其他適當(dāng)?shù)念愋汀?/li>
    2. 在 Name 字段中指定參數(shù)名稱。
    3. 在 Value 字段,最初,包含您所選擇的表達(dá)。通常情況下,你不需要改變它。

      如果新參數(shù)將成為可選參數(shù),則將指定的值用作函數(shù)定義中的默認(rèn)參數(shù)值。

      如果新參數(shù)作為必需參數(shù)引入,則將指定的值添加到函數(shù)調(diào)用中。

    4. 如果您希望新參數(shù)成為可選參數(shù),請(qǐng)選中“可選參數(shù)(Optional parameter)”復(fù)選框。
    5. 如果在函數(shù)體中發(fā)現(xiàn)了多于一次的表達(dá)式,則可以選擇僅替換所選事件或所有找到的事件與對(duì)新參數(shù)的引用。使用 Replace all occurrences 復(fù)選框指定您的意圖。
    6. 點(diǎn)擊“確定”。
    在ActionScript中提取參數(shù)
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)