重构 | 修改签名

CtrlF6

修改签名重构将几种不同的修改应用于函数签名。您可以使用此重构来:

  • 更改函数名称和返回类型
  • 添加、删除和重新排序参数

在更改函数签名时,CLion 会搜索函数的所有用法,并更新可以安全修改以反映更改的函数的所有调用、实现和重写替换。

修改函数签名

  1. 将光标置于要重构的函数上。
  2. 按下 CtrlF6 或从主菜单或上下文菜单中选择“Refactor | Change Signature”。
  3. 在“Change Signature”对话框中,对函数签名进行必要的更改:

    • 在“Name”字段中更改函数名称。
    • 在“Return type”字段中更改函数返回类型。
    • 使用表格和 Add/Remove/Move up/Move down 按钮管理函数参数。

    当添加参数时,在相应的行中指定新参数的属性:类型、名称和默认值。

    “Default value”字段设置要用作参数传递给用法的类型值。如果将此字段保留为空,CLion 将使用默认的类型值(例如,数值的 0 和指针的 nullptr)。

    1.png

    在“Type”和“Default value”字段中都可以使用代码补全。

    • 您还可以使用相应的复选框将函数设置为 constconstexprnoexcept

示例

C/C++

之前之后
//Initial function signature int exFunction2(int a, int b) { int x = a+b; return x; }; int main() { int a = 0, b = 1; int x = exFunction2(a, b); return 0; }//Name changed, the third parameter introduced int exFunction3(int a, int b, int c) { int x = a+b; return x; }; int main() { int a = 0, b = 1; int x = exFunction3(a, b, 0); //default value '0' added return 0; }

Objective-C/C++

之前之后
//This function will be turned into a block float multiply (int x, int y) { return x * y; } int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = multiply( 10, 20 ); } return 0; }int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = //a block was generated ^float(int x, int y) { return x * y; }(10, 20); } return 0; }

Python

之前之后
# This function will be renamed def fibonacci( n ): a, b = 0, 1 while b < n: print( b ) a, b = b, a+b n = int(input("n = ")) fibonacci( n )# Function with the new name def fibonacci_numbers( n ): a, b = 0, 1 while b < n: print( b ) a, b = b, a+b n = int(input("n = ")) fibonacci_numbers( n )

JavaScript

之前之后
//This function will be renamed //and a default parameter will be added function result() { } function show_result() { alert('Result: ' + result()); }//Function with a new name and default parameter function generate_result(input = 100) { } function show_result() { alert('Result: ' + generate_result()); }

标签: clion破解, clion破解教程, clion教程, clion基础教程, clion高级教程, clion核心教程, clion免费激活码, clion免费注册码, clion注册码在线生成, clion激活