您的位置:首页 > 其它

方法的语法(未总结完)

2015-12-17 12:11 246 查看
定义方法的语法:

//Public  []括号表示可选,可用可不用

[访问修饰符][static]返回值类型 方法名([参数])

{

  方法体

}

注意:

方法一般要定义在类中

如果方法没有返回值,则返回值类型写void

如果方法没有参数,()不能省略

方法的调用:如果是静态方法(由static修饰的)则使用  类名.方法名();

在类中调用本类的方法,可以只写  方法名();

例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
class Class1
{
private static int member;
//实例方法访问静态字段
public void Way(int num)
{
member = num;
}
//静态方法访问静态字段
public static void Show()
{
Console.WriteLine("Value of member:" + member);
}

}

class Program1
{
static void CeShi()
{
Class1 test = new Class1();
//对象调用实例方法
test.Way(40);
Class1.Show();//如果在类中调用本类的方法,可以只写 方法名(); 即 Show();
}
}
}


参考:http://jingyan.baidu.com/article/f0062228c2e998fbd3f0c8a9.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: