要创建一个类的实例对象的前提是这个类必须有一个构造函数,否则是不可以的。构造函数根据你的需要,可以是:protected,internal,public,private等。你的类Test没有构造函数,Test a=new Test()当然编译不通过。修改为:using System;public class Test
{
public Text()
{
}
private delegate string GetAString(); public string ToString()
{
Console.WriteLine(" I love you !");
} public static void Main(string[] args)
{
Test a=new Test(); GetAString FirstStringMethod=new GetAString(a.ToString);
Console.WriteLine("Sring is"+FirstStringMethod());
}
}
{
public Text()
{
}
private delegate string GetAString(); public string ToString()
{
Console.WriteLine(" I love you !");
} public static void Main(string[] args)
{
Test a=new Test(); GetAString FirstStringMethod=new GetAString(a.ToString);
Console.WriteLine("Sring is"+FirstStringMethod());
}
}
还有 声明了返回类型 你没有返回制阿
{
public Test()
{
}
private delegate string GetAString(); public string ToString()
{
Console.WriteLine(" I love you !");
} public static void Main(string[] args)
{
Test a=new Test(); GetAString FirstStringMethod=new GetAString(a.ToString);
Console.WriteLine("Sring is"+FirstStringMethod());
}
}
函数ToString必须是override的,因为任何的Class都是默认继承自object的,而object已经有ToString函数了,所以必须有override限定(不用也行,只是会多一个警告,因为ToString不是Abstract类型的)2——
函数ToString的定义是返回一个string类型,那么在函数体中必须有明确的返回代码,否则编译也会报告错误的。
这一句 我还不是太理解啊
可以继续解释一下吗 我感觉以前的都挺好的了!