简体   繁体   English

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

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

please bear with me as I'm just learning C#. 请耐心等待,因为我正在学习C#。 Just messing around with C# I decided to come up with an inventory system to test out but I have one problem in my script: 只是弄乱了C#,我决定想出一个清单系统进行测试,但是我的脚本中有一个问题:

using System;
using System.Collections.Generic;


public class Item
{
    public String name;
    public int pesos;

    public int getPesos()
    {
        return pesos;
    }
    public String getName()
    {
        return name;
    }
}
public class statuseffect
{
    statuseffect(string Effect,int Amount,int Duration)
    {
        string effect = Effect;
        int amount = Amount;
        int duration = Duration;
    }
}
public class Potion : Item 
{
    public int hpeffect;
    public int mpeffect;
    List<statuseffect> effects = new List<statuseffect>();


    public Potion(int hp,int mp)
    {
        hpeffect = hp;
        mpeffect = mp;
    }
    public void addEffect(statuseffect eff)
    {
        effects.Add(eff);
    }
}
class game
{
public static void Main()
    {   
        Potion healthPotion = new Potion(200,50);
        healthPotion.pesos = 23;
        Console.WriteLine(healthPotion.hpeffect);
        statuseffect slow = new statuseffect("slow",10,30);
    }
}

in the last line the compiler tells me that statuseffect does not contain a constructor that takes 3 arguments. 在最后一行中,编译器告诉我statuseffect不包含带有3个参数的构造函数。 From what I can tell, it does contain 3 arguments. 据我所知,它确实包含3个参数。 Is there something I am missing here? 我在这里缺少什么吗?

as a side note. 作为旁注。 if you guys have any comments or suggestions for my script, that would be helpful as well. 如果你们对我的脚本有任何意见或建议,那也将有所帮助。

Your constructor is private and therefore "invisible" to code outside the class itself. 您的构造函数是private ,因此对类本身之外的代码“不可见”。 Try adding the keyword internal before the constructor. 尝试在构造函数之前添加关键字internal Or, if it needs to be visible from other projects as well, add public instead. 或者,如果还需要从其他项目中看到它,则改为添加public

Another issue: In your class statuseffect , you declare three local variable inside your constructor. 另一个问题:在类statuseffect ,您在构造函数中声明了三个局部变量。 Those variable's only scope is the constructor. 这些变量的唯一作用域是构造函数。 You must move their declarations out of the constructor (then they become instance fields of the class). 您必须将它们的声明移出构造函数(然后它们将成为类的实例字段 )。 The constructor can still assign to them. 构造函数仍可以分配给它们。

您的构造函数需要标记为public

As c# is specifically an Objectt oriented language so every thing you declare in a class without an access specifier it is regarded as private by the compiler. 由于c#是专门面向对象的语言,因此您在没有访问说明符的类中声明的所有内容都被编译器视为私有的。 So declare your constructor public , that will solve the case 因此,将您的构造函数声明为public ,可以解决这种情况

public statuseffect(string Effect,int Amount,int Duration)
    {
        string effect = Effect;
        int amount = Amount;
        int duration = Duration;
    }

Make your constructor public. 公开您的构造函数。 When you dont specify any access-specifier by default it becomes private, means it can only be used inside your class. 默认情况下,如果您不指定任何访问说明符,它将变为私有的,这意味着它只能在您的类内部使用。 Making it public makes it accessible from outside. 公开发布可以从外部访问。

public class statuseffect
{
  public statuseffect(string Effect,int Amount,int Duration)
  {
    string effect = Effect;
    int amount = Amount;
    int duration = Duration;
  }
}

Moreover do something with those variables, they are of no use. 此外,对那些变量做一些事情,它们是没有用的。

public static void Populare_Books()
{
    Books[] bks = { new Books(1, "Shogun", "James Clavell"), new Books(2, "Pe aripile vantului", "Margaret Mitchell"), new Books(3, "MANDRIE SI PREJUDECATA", "Jane Austen"), new Books(4, "ANNA KARENINA", " Lev Tolstoi"), new Books(5, "Notre-Dame de Paris", "Victor Hugo"), new Books(6, "Marile speranțe", "Charles Dickens"), new Books(7, "Ion", "Liviu Rebreanu"), new Books(8, "Cărțile junglei", "Rudyard Kipling")};
    MySqlConnection con = new MySqlConnection("DataSource=localhost; UserID=root;database=" + nume);
    con.Open();
    MySqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "INSERT INTO Books (BookID, Title, ISBN, Number, NumberLeft) VALUES (@BookID, (@Title, (@ISBN, (@Number, (@NumberLeft)";
    MySqlTransaction tra = con.BeginTransaction();
    try
    {
        cmd.Transaction = tra;
        foreach (Books bo in bks)
        {
            cmd.Parameters.AddWithValue("@BookID", bo.BookID);
            cmd.Parameters.AddWithValue("@Title", bo.Title);
            cmd.Parameters.AddWithValue("@ISBN", bo.ISBN);
            cmd.Parameters.AddWithValue("@Number", bo.Number);
            cmd.Parameters.AddWithValue("@NumberLeft", bo.NumberLeft);
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
        }
        tra.Commit();
        Console.WriteLine("Tabelul Books a fost umplut");
        con.Close();
    }
    catch (Exception ex)
    {
        tra.Rollback();
        Console.WriteLine(ex.Message);
    }
}

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

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