AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > .NET技术

C#教程第五课:方法

51自学网 http://www.51zixue.net



2.在方法块中,我们首先定义了变量"myChoice"。

虽然它与 Main()方法中的"myChoice"变量同名同类型, 但它们是不同的两个变量,因为局部变量仅仅在其定义的块内可见。换句话说, getChoice()方法中的"myChoice" 同Main()方法中的"myChoice"变量没有丝毫联系。getChoice()方法打印出一个菜单到控制台,并读取用户的输入。"return" 语句把"myChoice"变量值返回给Main()方法中的调用者getChoice()。注意: "return"语句返回类型同该方法中定义的返回值类型相同,本例中,该返回值是个字符串。

3.在Main()方法中,在使用getChoice()之前,实例化了一个新的"OneMethod"对象。

这是因为:我们没有指明一个"静态"修饰符。(注意:Main()函数带有"静态"修饰符),getChoice()就成为一个实例的方法。 实例方法和静态方法的区别是:前者可以创建多个类的实例,每个实例有自己的单独的getChoice()方法。而一旦方法是静态的,就不存在方法的实例,你只能调用该静态方法的一个实现。

所以,正如前面所讲的,因为getChoice()并不是静态的,所以,我们必须实例化一个新对象来使用它。这是通过定义"OneMethod om = new OneMethod()"来进行的。在等号的左边,是对象引用"om",其类型是OneMethod。"om"是个对象的引用,这点很重要,"om"并不是对象自身,它是个引用OneMethod类型对象的变量。 在等号的右边,把新创建的OneMethod对象赋给引用"om"。 关键字"new"是个在堆上创建对象的新实例的C#运算符。此处完成的工作是: 在堆上创建一个新的OneMethod实例,并把它赋给om引用。一旦有了om引用的OneMethod对象实例,就可以通过om引用来对实例进行处理。

方法,域和其他类成员可以通过"." (点)运算符进行访问,标识和操纵。一旦需要调用方法getChoice(),就通过om引用,并使用点运算符"om.getChoice()"来进行。 getChoice() 块中的语句执行完毕之后即返回。为了捕捉到getChoice()的返回值,我们使用了赋值运算符"="。 返回串放到了Main()函数的局部变量 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);
}
}
}

说明


1.清单5-2是清单5-1的修改,主要是对程序进行了模块化,并添加了更多的实现,以便阐述参数传递的用法。

C#可以处理四种类型的参数:out(输出),ref(引用),params(数组)和value(值)。为了说明参数的用法,我们用两个字符串域创建地址类。

 
 

上一篇:C#教程第四课:循环控制语句  下一篇:C#学习第一天