方法

方法的分类:方法分为两大类,静态方法和非静态方法。那么静态方法和非静态放在在声明和使用过程中有哪些不同呢?

·静态方法在声明的过程中有关键字static,而非静态方法没有。

·在调用静态方法时需要由类名直接调用,而非静态方法则是通过类创建的对象来调用。

·调用非静态方法的时候可以通过this来调用,但是静态方法不可以。

在方法的撰写过程中,我们经常会对方法进行重载,方法的重载是指调用同一方法名,但各方法中参数的数据类型、个数或顺序不同。

namespace ConsoleApplication3
{
class Program
{
public static int Add(int x, int y)//使用静态修饰词static 需要用类名.出来
{
return x + y;
}
public double Add(int x, double y)
{
return x + y;
}
public int Add(int x, int y,int z)
{
return x + y + z;
}
static void Main(string[] args)
{
Program program = new Program();//实例化Program类对象
int x = 3;
int y = 5;
int z = 7;
double y1 = 5.5;
Console.WriteLine(x + "+" + y + "=" + Program.Add(x, y));
Console.WriteLine(x + "+" + y1 + "=" + program.Add(x, y1));
Console.WriteLine(x + "+" + y + "+" + z + "=" + program.Add(x, y, z));
Console.ReadLine();

}
}
}

原文地址:https://www.cnblogs.com/mywangpingan/p/7017445.html