当前位置:首页 > Windows程序 > 正文

C#入门学习之属性和方法

2021-05-25 Windows程序

1 方法分为静态方法和非静态方法,静态方法不对特定的实例进行操作,,且不允许引用this,非静态方法是对类的某个特定的实例进行操作,且可以用this来访改方法

例子一 静态方法

public static int add(int x,int y)

{

return x+y;

}

static void Main(string[] args)

{

Console.WriteLine(Program.add(3,4));

}

例子二 非静态方法

public int add(int x,int y)

{

return x+y;

}

static void Main(string[] args)

{

Program program=new Program();   //实例化对象

Console.WriteLine(Program.add(3,4));

}

2 params关键字可以指定采用数目可变的参数的方法参数

注:一个函数中只能一个参数带params关键字;
·带params关键字的参数必须是最后一个参数;
·带params关键字的参数类型必须是一维数组;
 

二  实例

1 求数组中最大元素值的方法

static void Main(string[] args)

{

int[] myArray = {1,5,3,7,8,22,21,33,35,99,26 };

int maxVal = MaxValue(myArray);

Console.WriteLine("The maximum value in myArray is {0}",maxVal);

Console.ReadKey();

}

static int MaxValue(int[] intArray)

{

int maxVal =intArray[0];

for (int i = 1; i < intArray.Length; i++)

{

if (intArray[i] > maxVal)

{

maxVal=intArray[i];

}          

}

return maxVal;

}

2 求数组元素和的方法

static int SumVals(params int[] vals)  //关键字params

{

int sum = 0;

foreach (int val in vals)

{

sum += val;

}

return sum;

}

static void Main(string[] args)

{

int sum = SumVals(1, 2, 3, 4, 5);

Console.WriteLine("The sum={0}", sum);

Console.ReadKey();

}    }

3 面向对象和方法运用的小程序

using System;

class Address

{

public string name;

public string address;

}

class Methodparams

{

public static void Main()

{

string myChoice;

Methodparams mp = new Methodparams();

do

{

myChoice = mp.getchoice();

mp.makeDecision(myChoice);

Console.Write("Press any key to continue...");

Console.ReadLine();

Console.WriteLine();

} while (myChoice != "Q" && myChoice != "q");

}

string getchoice()

{

string mychoice;

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,Q)");

mychoice = Console.ReadLine();

return mychoice;

}

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.viewAddress("Cheryl","Joe","Matt","Robert");

break;

case "Q":

case "q":

Console.WriteLine("Bye");

break;

}

}

void addAddress(ref Address addr)

{

Console.WriteLine("Name:{0},Address:{1} added.",addr.name,addr.address);

}

void deleteAddress(string name)

{

Console.WriteLine("You wish to delete {0}‘s address.",name);

}

void modifyAddress(out Address addr)

{

addr = new Address();

addr.name = "Joe";

addr.address = "C# Station";

}

void viewAddress(params string[] names)

{

foreach (string name in names)

{

Console.WriteLine("Name:{0}",name);

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70647.html