繁体   English   中英

C#错误-不包含接受1个参数的构造函数-我不明白我在做什么错?

[英]C#-Error-does not contain a constructor that takes 1 arguments-I don't get what I am doing wrong?

我一直在尝试使此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 ();

    }
}
}

构造函数没有返回类型。 所以代替

public void Test(int start)
{

    tally = start;

}

你应该有

 public Test(int start)
{

    tally = start;

}

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

public Test(int strat)
{
...
}

暂无
暂无

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

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