解析C#面向对象编程中方法(method)的使用
方法是包含一系列语句的代码块。程序通过调用该方法并指定任何所需的方法参数使语句得以执行。在 C# 中,每个执行的指令均在方法的上下文中执行。Main 方法是每个 C# 应用程序的入口点,并在启动程序时由公共语言运行时 (CLR) 调用。 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(); } 方法访问 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; } 按引用传递与按值传递 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 作为输出。 返回值 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 存储值是可选的。此步骤可以帮助提高代码的可读性,或者如果需要存储该方法整个范围内参数的原始值,则此步骤可能很有必要。 异步方法 int result = await delayTask 。 // 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 访问器关键字。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |