简体   繁体   English

不包含带有 3 个参数的构造函数

[英]Does not contain a constructor that takes 3 arguments when it does

I'm having trouble getting the main to read my constructor.我无法让 main 读取我的构造函数。 It keeps saying my class doesn't contain a constructor that takes on 3 arguments.它一直说我的类不包含接受 3 个参数的构造函数。 It's public.它是公开的。 I don't know what I'm doing wrong, I've done this before successfully, this is a practice problem I'm using to study for my test.我不知道我做错了什么,我之前成功地完成了这个,这是我用来学习考试的练习题。

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);
        }

I think your Actor class isn't compiling because of this line:我认为由于这一行,您的 Actor 类没有编译:

     set { name = Dustin Hoffman; }

and the JIT error is just confused because just how broken your code is as a result.并且 JIT 错误只是令人困惑,因为结果是您的代码有多么破碎。

Also:还:

  Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards" );

Cannot simply sit out in no-man's land, it must be inside some kind of method or constructor.不能简单地坐在无人区,它必须在某种方法或构造函数中。

Per comment below: you declare name as both a field and Property with the same capitalization, which is invalid.根据下面的评论:您将 name 声明为具有相同大小写的字段和属性,这是无效的。 And it's declared SAGMember but referenced SAGMEMBER.它被声明为 SAGMember,但引用了 SAGMEMBER。

You do realize there is an error window in Visual Studio, right?您确实意识到 Visual Studio 中有一个错误窗口,对吗?

You actually have multiple compile errors.您实际上有多个编译错误。 I suggest starting at the top error and working down.我建议从最严重的错误开始,然后逐步减少。 In this case, as @JasonLind suggested, the error message with the lack of a constructor is probably incorrect - occasionally you'll see cases like this where a previous compile error confuses the compiler (which is why you usually want to start with the first compile error - sometimes fixing one compile error will fix several others as well).在这种情况下,正如@JasonLind 所建议的那样,缺少构造函数的错误消息可能是不正确的 - 有时您会看到这样的情况,其中先前的编译错误使编译器感到困惑(这就是为什么您通常希望从第一个开始编译错误 - 有时修复一个编译错误也会修复其他几个错误)。

For example:例如:

 Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards" );

You're missing a +.你缺少一个+。 Also, as indicated, that has to be inside a method.此外,正如所指出的,这必须在方法内部。

Also, as indicated:此外,如所示:

 set { name = Dustin Hoffman; }

needs quotes.需要报价。 You shouldn't do that anyway because you can't set the property to anything but "Dustin Hoffman".无论如何您都不应该这样做,因为您不能将属性设置为“Dustin Hoffman”以外的任何内容。

Also, name is declared twice.此外, name被声明了两次。 Make the property and field have different names.使属性和字段具有不同的名称。

Also, in your constructor:此外,在您的构造函数中:

this.awardsNum = AwardsNum ++;
    this.SAGMember = false;

Why the ++?为什么是++? Also, you always throw away the value that was passed into the constructor because SAGMember is hardcoded to false .此外,您总是丢弃传递给构造函数的值,因为SAGMember被硬编码为false

For the ToString method,对于ToString方法,

    return string.Format ("Actor: " + name + "\n" + " Number of Awards: " + awardsNum + "\n"+ "SAG Member: " + SAGMember);

You're not using a format string here, just concatenation.您在这里没有使用格式字符串,只是连接。 Also, why is \\n a separate string?另外,为什么\\n是一个单独的字符串?

The following comment:以下评论:

//attributes

is incorrect.是不正确的。 These are fields, not attributes - they're very different.这些是字段,而不是属性 - 它们非常不同。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM