属性 修饰符 返回值类型 方法名(参数) { 语句 } |
1.清单5-1. 一个简单的方法: OneMethod.cs |
using System; class OneMethod { public static void Main() { string myChoice; OneMethod om = new OneMethod(); do { myChoice = om.getChoice(); // Make a decision based on the user's choice switch(myChoice) { case "A": case "a": Console.WriteLine("You wish to add an address."); break; case "D": case "d": Console.WriteLine("You wish to delete an address."); break; case "M": case "m": Console.WriteLine("You wish to modify an address."); break; case "V": case "v": Console.WriteLine("You wish to view the address list."); break; case "Q": case "q": Console.WriteLine("Bye."); break; default: Console.WriteLine("{0} is not a valid choice", myChoice); } // Pause to allow the user to see the results Console.Write("Press any key to continue..."); Console.ReadLine(); Console.WriteLine(); } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit } string getChoice() { string myChoice; // Print A Menu Console.WriteLine("My Address Book\n"); Console.WriteLine("A - Add New Address"); Console.WriteLine("D - Delete Address"); Console.WriteLine("M - Modify Address"); Console.WriteLine("V - View Addresses"); Console.WriteLine("Q - Quit\n"); Console.WriteLine("Choice (A,D,M,V,or Q): "); // Retrieve the user's choice myChoice = Console.ReadLine(); return myChoice; } } |
说明 |
2.清单5-2. 方法参数:MethodParams.cs |
using System; class Address { public string name; public string address; } class MethodParams { public static void Main() { string myChoice; MethodParams mp = new MethodParams(); do { // show menu and get input from user myChoice = mp.getChoice(); // Make a decision based on the user's choice mp.makeDecision(myChoice); // Pause to allow the user to see the results Console.Write("Press any key to continue..."); Console.ReadLine(); Console.WriteLine(); } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit } // show menu and get user's choice string getChoice() { string myChoice; // Print A Menu Console.WriteLine("My Address Book\n"); Console.WriteLine("A - Add New Address"); Console.WriteLine("D - Delete Address"); Console.WriteLine("M - Modify Address"); Console.WriteLine("V - View Addresses"); Console.WriteLine("Q - Quit\n"); Console.WriteLine("Choice (A,D,M,V,or Q): "); // Retrieve the user's choice myChoice = Console.ReadLine(); return myChoice; } // make decision void makeDecision(string myChoice) { Address addr = new Address(); switch(myChoice) { case "A": case "a": addr.name = "Joe"; addr.address = "C# Station"; this.addAddress(ref addr); break; case "D": case "d": addr.name = "Robert"; this.deleteAddress(addr.name); break; case "M": case "m": addr.name = "Matt"; this.modifyAddress(out addr); Console.WriteLine("Name is now {0}.", addr.name); break; case "V": case "v": this.viewAddresses("Cheryl", "Joe", "Matt", "Robert"); break; case "Q": case "q": Console.WriteLine("Bye."); break; default: Console.WriteLine("{0} is not a valid choice", myChoice); } } // insert an address void addAddress(ref Address addr) { Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address); } // remove an address void deleteAddress(string name) { Console.WriteLine("You wish to delete {0}'s address.", name); } // change an address void modifyAddress(out Address addr) { //Console.WriteLine("Name: {0}.", addr.name); // causes error! addr = new Address(); addr.name = "Joe"; addr.address = "C# Station"; } // show addresses void viewAddresses(params string[] names) { foreach (string name in names) { Console.WriteLine("Name: {0}", name); } } } |
说明 |
相关视频
相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么
热门文章 去除winrar注册框方法
最新文章
比特币病毒怎么破解 比去除winrar注册框方法
华为无线路由器HG522-C破解教程(附超级密码JEB格式文件京东电子书下载和阅读限制破解教UltraISO注册码全集(最新)通过Access破解MSSQL获得数据
人气排行 华为无线路由器HG522-C破解教程(附超级密码JEB格式文件京东电子书下载和阅读限制破解教UltraISO注册码全集(最新)qq相册密码破解方法去除winrar注册框方法(适应任何版本)怎么用手机破解收费游戏华为无线猫HG522破解如何给软件脱壳基础教程
查看所有0条评论>>