C#提取方法重構(gòu)
Jul 06, 2016 pm 01:30 PM引用:https://msdn.microsoft.com/zh-CN/library/0s21cwxk.aspx 提取方法 是一項(xiàng)重構(gòu)操作,提供了一種從現(xiàn)有成員中的代碼段創(chuàng)建新方法的便捷方式。 使用 提取方法 ,可以通過(guò)從現(xiàn)有成員的代碼塊中提取一組代碼來(lái)創(chuàng)建新方法。提取出的新方法包含所選代碼,而
引用:https://msdn.microsoft.com/zh-CN/library/0s21cwxk.aspx
“提取方法”是一項(xiàng)重構(gòu)操作,提供了一種從現(xiàn)有成員中的代碼段創(chuàng)建新方法的便捷方式。
使用“提取方法”,可以通過(guò)從現(xiàn)有成員的代碼塊中提取一組代碼來(lái)創(chuàng)建新方法。提取出的新方法包含所選代碼,而現(xiàn)有成員中的所選代碼被替換為對(duì)新方法的調(diào)用。 通過(guò)將代碼段變?yōu)槠渥约旱姆椒?,可以快速而?zhǔn)確地重新組織代碼,以獲得更好的重用性和可靠性。
“提取方法”有以下優(yōu)點(diǎn):
-
通過(guò)強(qiáng)調(diào)離散的可重用方法鼓勵(lì)最佳的編碼做法。
-
鼓勵(lì)通過(guò)較好的組織獲得自記錄代碼。
當(dāng)使用描述性名稱時(shí),高級(jí)別方法可以像讀取一系列注釋一樣進(jìn)行讀取。
-
鼓勵(lì)創(chuàng)建細(xì)化方法,以簡(jiǎn)化重載。
-
減少代碼重復(fù)。
1、創(chuàng)建名為 ExtractMethod 的控制臺(tái)應(yīng)用程序,然后使用以下代碼示例替換 PRogram。
<span style="color: #0000ff;">class</span><span style="color: #000000;"> A { </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> PI = <span style="color: #800080;">3.141592</span><span style="color: #000000;">; </span><span style="color: #0000ff;">double</span> CalculatePaintNeeded(<span style="color: #0000ff;">double</span> paintPerUnit, <span style="color: #0000ff;">double</span><span style="color: #000000;"> radius) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> Select any of the following: </span><span style="color: #008000;">//</span><span style="color: #008000;"> 1. The entire next line of code. </span><span style="color: #008000;">//</span><span style="color: #008000;"> 2. The right-hand side of the next line of code. </span><span style="color: #008000;">//</span><span style="color: #008000;"> 3. Just "PI *" of the right-hand side of the next line </span><span style="color: #008000;">//</span><span style="color: #008000;"> of code (to see the prompt for selection expansion). </span><span style="color: #008000;">//</span><span style="color: #008000;"> 4. All code within the method body. </span><span style="color: #008000;">//</span><span style="color: #008000;"> ...Then invoke Extract Method.</span> <span style="color: #0000ff;">double</span> area = PI * radius *<span style="color: #000000;"> radius; </span><span style="color: #0000ff;">return</span> area /<span style="color: #000000;"> paintPerUnit; } }</span>
2、選擇您要提取的代碼片段:
<span style="color: #0000ff;">double</span> area = PI * radius * radius;
3、在“重構(gòu)”菜單上,單擊“提取方法”。
出現(xiàn)“提取方法”對(duì)話框。
也可以鍵入鍵盤快捷鍵 Ctrl+R、Ctrl+M 來(lái)顯示“提取方法”對(duì)話框。
還可以右擊選定代碼,指向“重構(gòu)”,然后單擊“提取方法”來(lái)顯示“提取方法”對(duì)話框。
4、在“新方法名稱”框中指定新方法的名稱,如 CircleArea。
新方法簽名的預(yù)覽顯示在“預(yù)覽方法簽名”下。
5、單擊“確定”
備注:
使用“提取方法”命令時(shí),將在同一個(gè)類中的源成員之后插入新方法。
分部類型
如果類是分部類型,則“提取方法”將緊跟源成員之后生成新方法。 “提取方法”確定新方法的簽名,并在新方法中的代碼不引用實(shí)例數(shù)據(jù)時(shí)創(chuàng)建靜態(tài)方法。
泛型類型參數(shù)
當(dāng)提取的方法具有不受約束的泛型類型參數(shù)時(shí),除非已為該參數(shù)賦值,否則生成的代碼將不向該參數(shù)添加 ref 修飾符。 如果提取的方法將支持引用類型作為泛型類型實(shí)參,則應(yīng)該向方法簽名中的形參手動(dòng)添加 ref 修飾符。
匿名方法
如果嘗試提取匿名方法(該方法包括對(duì)在匿名方法之外聲明或引用的局部變量的引用)的一部分,則 Visual Studio 將警告您可能有語(yǔ)義更改。
當(dāng)匿名方法使用局部變量的值時(shí),將在執(zhí)行匿名方法時(shí)獲取該值。 將匿名方法提取到其他方法中時(shí),將在調(diào)用提取方法時(shí)獲取局部變量的值。
下面的示例闡釋這一語(yǔ)義更改。 如果執(zhí)行此代碼,則將向控制臺(tái)輸出 11。 如果使用“提取方法”將代碼注釋所標(biāo)記的代碼區(qū)域提取到其自己的方法中,然后執(zhí)行重構(gòu)后的代碼,則將向控制臺(tái)輸出 10。
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Program { </span><span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> D(); D d; </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) { Program p </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Program(); </span><span style="color: #0000ff;">int</span> i = <span style="color: #800080;">10</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">begin extraction</span><span style="color: #008000;">*/</span><span style="color: #000000;"> p.d </span>= <span style="color: #0000ff;">delegate</span> { Console.WriteLine(i++<span style="color: #000000;">); }; </span><span style="color: #008000;">/*</span><span style="color: #008000;">end extraction</span><span style="color: #008000;">*/</span><span style="color: #000000;"> i</span>++<span style="color: #000000;">; p.d(); } }</span>
若要解決此問(wèn)題,請(qǐng)使匿名方法中使用的局部變量成為類的字段。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Colorful motherboards enjoy high popularity and market share in the Chinese domestic market, but some users of Colorful motherboards still don’t know how to enter the bios for settings? In response to this situation, the editor has specially brought you two methods to enter the colorful motherboard bios. Come and try it! Method 1: Use the U disk startup shortcut key to directly enter the U disk installation system. The shortcut key for the Colorful motherboard to start the U disk with one click is ESC or F11. First, use Black Shark Installation Master to create a Black Shark U disk boot disk, and then turn on the computer. When you see the startup screen, continuously press the ESC or F11 key on the keyboard to enter a window for sequential selection of startup items. Move the cursor to the place where "USB" is displayed, and then

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

The difference between Go language methods and functions lies in their association with structures: methods are associated with structures and are used to operate structure data or methods; functions are independent of types and are used to perform general operations.
