当前位置: 移动技术网 > IT编程>开发语言>c# > 解析C#面向对象编程中方法(method)的使用

解析C#面向对象编程中方法(method)的使用

2019年07月18日  | 移动技术网IT编程  | 我要评论
方法是包含一系列语句的代码块。程序通过调用该方法并指定任何所需的方法参数使语句得以执行。在 c# 中,每个执行的指令均在方法的上下文中执行。main 方法是每个 c# 应用

方法是包含一系列语句的代码块。程序通过调用该方法并指定任何所需的方法参数使语句得以执行。在 c# 中,每个执行的指令均在方法的上下文中执行。main 方法是每个 c# 应用程序的入口点,并在启动程序时由公共语言运行时 (clr) 调用。
方法签名
通过指定访问级别(如 public 或 private)、可选修饰符(如 abstract 或 sealed)、返回值、方法的名称以及任何方法参数,在类或结构中声明方法。这些部件一起构成方法的签名。
注意
出于方法重载的目的,方法的返回类型不是方法签名的一部分。但是在确定委托和它所指向的方法之间的兼容性时,它是方法签名的一部分。
方法参数在括号内,并且用逗号分隔。空括号指示方法不需要任何参数。此类包含三种方法:

abstract class motorcycle
{
 // anyone can call this.
 public void startengine() {/* method statements here */ }

 // only derived classes can call this.
 protected void addgas(int gallons) { /* method statements here */ }

 // derived classes can override the base class implementation.
 public virtual int drive(int miles, int speed) { /* method statements here */ return 1; }

 // derived classes must implement this.
 public abstract double gettopspeed(); 
}

方法访问
调用对象上的方法就像访问字段。在对象名之后添加一个句点、方法名和括号。参数列在括号里,并且用逗号分隔。因此,可在以下示例中调用 motorcycle 类的方法:

class testmotorcycle : motorcycle
{

 public override double gettopspeed()
 {
  return 108.4;
 }

 static void main()
 {

  testmotorcycle moto = new testmotorcycle();

  moto.startengine();
  moto.addgas(15);
  moto.drive(5, 20);
  double speed = moto.gettopspeed();
  console.writeline("my top speed is {0}", speed);   
 }
}

方法参数与参数
该方法定义指定任何所需参数的名称和类型。调用代码调用该方法时,它为每个参数提供了称为参数的具体值。参数必须与参数类型兼容,但调用代码中使用的参数名(如果有)不需要与方法中定义的参数名相同。例如:

public void caller()
{
 int numa = 4;
 // call with an int variable.
 int producta = square(numa);

 int numb = 32;
 // call with another int variable.
 int productb = square(numb);

 // call with an integer literal.
 int productc = square(12);

 // call with an expression that evaulates to int.
 productc = square(producta * 3);
}

int square(int i)
{
 // store input argument in a local variable.
 int input = i;
 return input * input;
}

按引用传递与按值传递
默认情况下,值类型传递给方法时,传递的是副本而不是对象本身。因此,对参数的更改不会影响调用方法中的原始副本。可以使用 ref 关键字按引用传递值类型。
引用类型的对象传递到方法中时,将传递对对象的引用。也就是说,该方法接收的不是对象本身,而是指示该对象位置的参数。如果通过使用此引用更改对象的成员,即使是按值传递该对象,此更改也会反映在调用方法的参数中。
通过使用 class 关键字创建引用类型,如以下示例所示。

public class samplereftype
{
 public int value;
}

现在,如果将基于此类型的对象传递到方法,则将传递对对象的引用。下面的示例将 samplereftype 类型的对象传递到 modifyobject 方法。

public static void testreftype()
{
 samplereftype rt = new samplereftype();
 rt.value = 44;
 modifyobject(rt);
 console.writeline(rt.value);
}
static void modifyobject(samplereftype obj)
{
 obj.value = 33;
}

该示例执行的内容实质上与先前示例相同,均按值将参数传递到方法。但是因为使用了引用类型,结果有所不同。 modifyobject 中所做的对形参 obj 的 value 字段的修改,也会更改 testreftype 方法中实参 rt 的 value 字段。 testreftype 方法显示 33 作为输出。

返回值
方法可以将值返回到调用方。如果列在方法名之前的返回类型不是 void,则该方法可通过使用 return 关键字返回值。带 return 关键字,后跟与返回类型匹配的值的语句将该值返回到方法调用方。 return 关键字还会停止执行该方法。如果返回类型为 void,没有值的 return 语句仍可用于停止执行该方法。没有 return 关键字,当方法到达代码块结尾时,将停止执行。具有非空的返回类型的方法都需要使用 return 关键字来返回值。例如,这两种方法都使用 return 关键字来返回整数:

class simplemath
{
 public int addtwonumbers(int number1, int number2)
 {
  return number1 + number2;
 }

 public int squareanumber(int number)
 {
  return number * number;
 }
}

若要使用从方法返回的值,调用方法可以在相同类型的值足够的地方使用该方法调用本身。也可以将返回值分配给变量。例如,以下两个代码示例实现了相同的目标:

(1)

 int result = obj.addtwonumbers(1, 2);
result = obj.squareanumber(result);
// the result is 9.
console.writeline(result);
(2)

result = obj.squareanumber(obj.addtwonumbers(1, 2));
// the result is 9.
console.writeline(result);

在这种情况下,使用本地变量 result 存储值是可选的。此步骤可以帮助提高代码的可读性,或者如果需要存储该方法整个范围内参数的原始值,则此步骤可能很有必要。

异步方法
通过使用异步功能,你可以调用异步方法而无需使用显式回调,也不需要跨多个方法或 lambda 表达式来手动拆分代码。visual studio 2012 中已引入异步功能。
如果用 async 修饰符标记方法,则可以使用该方法中的 await 运算符。当控件到达异步方法中的 await 表达式时,控件将返回到调用方,并在等待任务完成前,方法中进度将一直处于挂起状态。任务完成后,可以在方法中恢复执行。
注意
异步方法在遇到第一个尚未完成的 awaited 对象或到达异步方法的末尾时(以先发生者为准),将返回到调用方。
异步方法可以具有 task<tresult>、task 或 void 返回类型。void 返回类型主要用于定义需要 void 返回类型的事件处理程序。无法等待返回 void 的异步方法,并且返回 void 方法的调用方无法捕获该方法引发的异常。
在以下示例中,delayasync 是具有 task<tresult> 返回类型的异步方法。 delayasync 具有返回整数的 return 语句。因此,delayasync 的方法声明必须具有 task<int> 的返回类型。因为返回类型是 task<int>,dosomethingasync 中 await 表达式的计算如以下语句所示得出整数:

int result = await delaytask


startbutton_click 方法是具有 void 返回类型的异步方法的示例。因为 dosomethingasync 是异步方法,调用 dosomethingasync 的任务必须等待,如以下语句所示:await dosomethingasync();。 startbutton_click 方法必须使用 async 修饰符进行定义,因为该方法具有 await 表达式。

// using system.diagnostics;
// using system.threading.tasks;

// this click event is marked with the async modifier.
private async void startbutton_click(object sender, routedeventargs e)
{
 await dosomethingasync();
}

private async task dosomethingasync()
{
 task<int> delaytask = delayasync();
 int result = await delaytask;

 // the previous two statements may be combined into
 // the following statement.
 //int result = await delayasync();

 debug.writeline("result: " + result);
}

private async task<int> delayasync()
{
 await task.delay(100);
 return 5;
}

输出: 

 

result: 5

异步方法不能声明任何 ref 或 out 参数,但是可以调用具有这类参数的方法。


表达式主体定义
具有立即仅返回表达式结果,或单个语句作为方法主题的方法定义很常见。以下是使用 => 定义此类方法的语法快捷方式:

public point move(int dx, int dy) => new point(x + dx, y + dy); 
public void print() => console.writeline(first + " " + last);
// works with operators, properties, and indexers too.
public static complex operator +(complex a, complex b) => a.add(b);
public string name => first + " " + last; 
public customer this[long id] => store.lookupcustomer(id);

如果该方法返回 void 或是异步方法,则该方法的主体必须是语句表达式(与 lambda 相同)。对于属性和索引器,两者必须是只读,并且不使用 get 访问器关键字。
迭代器
迭代器对集合执行自定义迭代,如列表或数组。迭代器使用 yield return 语句返回元素,每次返回一个。当 yield return 语句到达时,将记住当前在代码中的位置。下次调用迭代器时,将从该位置重新开始执行。
通过使用 foreach 语句从客户端代码调用迭代器。
迭代器的返回类型可以是 ienumerable、ienumerable<t>、ienumerator 或 ienumerator<t>。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网