【问题标题】:C#-Error-does not contain a constructor that takes 1 arguments-I don't get what I am doing wrong?C#-错误-不包含带 1 个参数的构造函数-我不明白我做错了什么?
【发布时间】:2014-03-15 20:28:23
【问题描述】:

我一直试图让这个 C# 程序工作,但我一直收到关于构造函数采用 1 个参数的错误。我不明白。我认为这与“Test myTest = new Test(3);”有关但我不知道如何处理它。

任何帮助或引导我朝着正确的方向前进将不胜感激。谢谢。

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

namespace ConsoleApplication1
{

public class Test
{

    private int tally;

    public void Test(int start)
    {

        tally = start;

    }

    public void AddFive()
    {

        tally += 5;

    }

    public void Display()
    {

        Console.WriteLine("The tally is {0}", tally);

    }

    public void Main(string[] args)
    {

        Test myTest = new Test(3);

        myTest.AddFive();

        myTest.Display ();

    }
}
}

【问题讨论】:

  • 天哪!!!您确实遇到了 OOP 问题:P

标签: c# constructor arguments


【解决方案1】:

构造函数没有返回类型。 所以不是

public void Test(int start)
{

    tally = start;

}

你应该有

 public Test(int start)
{

    tally = start;

}

【讨论】:

  • 我是个白痴。非常感谢。我从来没有想过如果类是无效的,就不会给出返回值。再次感谢。
【解决方案2】:

在构造函数定义中你说要返回 void。这不是必需的。你的构造函数应该是

public Test(int strat)
{
...
}

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多