问题从这考试:
楼主问了:
没写过控制台应用程序,请各位高手别笑话我菜鸟。
小弟写了个控制台应用程序,大概是这样:
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
}
private string aa()
{
return "aa";
}
}
}
但是在Main函数里写代码的时候似乎智能感知找不到aa()这个函数,请问是不是需要把aa()函数设置成public的或者static的?感谢各位
我的回复1:你可以把这个函数写成静态的,也就是加上static修饰符,也可以创建个类测对象来调用它。(这写了错别字类错写成测了)。
回复2:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
string str = "";
str=p.aa();
Console.WriteLine(str);
Console.ReadLine();
}
private string aa()
{
return "aa";
}
}
}
回复3:
也可以这样:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "";
str = Program.aa();
Console.WriteLine(str);
Console.ReadLine();
}
private static string aa()
{
return "aa";
}
}
}
引用了别人的回复回复4:
引用 2 楼 hulihui 的回复:
Main()是静态static函数,其调用的本类函数必须是static
我的回复:
Main函数是程序的入口点,本身是静态的,没说它调用本类函数必须是static的啊。
在这我理解他的“其调用的”是在Main函数中调用的本类函数必须static,我认为不是这样的我就回复了,而且我的回复2里边也证实了main函数可以调用实例函数。
下边的高人给我回复了:
引用 10 楼 dahai99007 的回复:
引用 2 楼 hulihui 的回复:
Main()是静态static函数,其调用的本类函数必须是static
Main函数是程序的入口点,本身是静态的,没说它调用本类函数必须是static的啊。
linux_ch 的回复
這個答案很雷,你的基礎很差~~
号多繁体字,我猜了半天是这个意思:
这个答案很雷,你的基础很差
还好心提醒我:
linux_ch 2008年12月01日 15点21分16秒 说:
你的基礎很差,看到你的回復
linux_ch 2008年12月01日 15点22分05秒 说:
Main函数是程序的入口点,本身是静态的,没说它调用本类函数必须是static的啊。
好好看看基礎的吧
dahai99007 2008年12月01日 15点39分04秒 说:
是啊,还请高人们多指点!Thanks!
所以发个帖子请高人给我补补基础,Thanks!
本想发200分呢,可提示说我无权发这么多分 :)