我无法让main读取我的构造函数。它一直说我的类不包含一个带有3个参数的构造函数。这是公开的。我不知道我做错了什么,我在成功之前做过这个,这是我用来学习测试的练习题。
public class Actor
{
//attributes
private string name;
private int awardsNum;
private bool SAGMember;
//property
private string name
{
get { return name; }
set { name = value; }
}
//constructor
public Actor(string Name, int AwardsNum, bool SAGMember)
{
this.name = Name;
this.awardsNum = AwardsNum ++;
this.SAGMember = false;
}
public Actor()
{
this.name = "Bob Smith";
this.awardsNum = 0 ;
this.SAGMember = false;
}
public override string ToString ()
{
return string.Format ("Actor: " + name + "\n" + " Number of Awards: " + awardsNum + "\n"+ "SAG Member: " + SAGMember);
}
}
public static void Main (string[] args)
{
Actor a1 = new Actor ("Dustin Hoffman", 0 , true);
Console.WriteLine (a1);
Actor a2 = new Actor ();
Console.WriteLine (a2);
}
答案 0 :(得分:7)
我认为你的Actor类因为这行而没有编译:
set { name = Dustin Hoffman; }
并且JIT错误很混乱,因为结果会导致代码损坏。
此外:
Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards" );
不能简单地坐在非人的土地上,它必须在某种方法或构造中。
以下评论:您将name声明为具有相同大小写的字段和Property,这是无效的。它已宣布为SAGMember,但引用了SAGMEMBER。
您确实意识到Visual Studio中存在错误窗口,对吧?
答案 1 :(得分:2)
您实际上有多个编译错误。我建议从顶部错误开始,然后继续工作。在这种情况下,正如@JasonLind所建议的那样,缺少构造函数的错误消息可能是错误的 - 有时您会看到这样的情况,其中先前的编译错误会混淆编译器(这就是为什么您通常想要从第一个开始编译错误 - 有时修复一个编译错误也会修复其他几个错误。
例如:
Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards" );
你错过了一个+。此外,如上所述,必须在方法内部。
另外,如所示:
set { name = Dustin Hoffman; }
需要报价。你不应该这样做,因为除了“Dustin Hoffman”之外,你不能将财产设置为任何东西。
此外,name
被声明两次。使属性和字段具有不同的名称。
另外,在你的构造函数中:
this.awardsNum = AwardsNum ++;
this.SAGMember = false;
为什么选择++?此外,您始终丢弃传递给构造函数的值,因为SAGMember
被硬编码到false
。
对于ToString
方法,
return string.Format ("Actor: " + name + "\n" + " Number of Awards: " + awardsNum + "\n"+ "SAG Member: " + SAGMember);
这里没有使用格式字符串,只是连接。另外,为什么\n
是一个单独的字符串?
以下评论:
//attributes
不正确。这些是字段,而不是属性 - 它们非常不同。
答案 2 :(得分:0)
public class Actor
{
//Fields
/*Write a class “Actor” that contains these attributes with the appropriate level of visibility explicitly defined:
“Name” which is a String,
“numberOfAwards” which is an integer
“SAGMember” which is a bool*/
private string name;
private bool SAGMember;
private int awardsNum;
//property
//4.Write a property (get/set) for the name attribute ONLY.
private string Name
{
get { return name; }
set { name = value; }
}
//constructor
// Write a parameterless constructor for this class that initializes the name to “Bob Smith”, the number of awards to 0, and the SAGMember to “false”.
public Actor()
{
this.name = "bob smith";
this.SAGMember = false;
this.awardsNum = 0;
}
public Actor(string name, bool SAGMember, int awardsNum)
{
this.name = Name;
this.SAGMember = true;
this.awardsNum = 4;
}
// over ride
public override string ToString()
{
return string.Format("Actor: " + name + "\n" + "\n" + "SAG Member: " + SAGMember + "Number of Awards: " + awardsNum );
}
}
----- ------ MAIN
public static void Main (string[] args)
{
Actor a1 = new Actor ( "Dustin Hoffman", true, 5);
Console.WriteLine (a1);
Actor a2 = new Actor ();
Console.WriteLine (a2);
}
}