0

请多多包涵,因为我只是在学习 C#。只是在搞乱 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);
    }
}

在最后一行,编译器告诉我 statuseffect 不包含带有 3 个参数的构造函数。据我所知,它确实包含 3 个论点。我在这里缺少什么吗?

作为旁注。如果你们对我的脚本有任何意见或建议,那也会很有帮助。

4

5 回答 5

6

您的构造函数private因此对于类本身之外的代码是“不可见的”。尝试internal在构造函数之前添加关键字。或者,如果它也需要在其他项目中可见,请public改为添加。

另一个问题:在您的 classstatuseffect中,您在构造函数中声明了三个局部变量。这些变量的唯一作用域是构造函数。您必须将它们的声明移出构造函数(然后它们成为类的实例字段)。构造函数仍然可以分配给它们。

于 2012-08-17T07:45:34.173 回答
2

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

于 2012-08-17T07:45:29.093 回答
2

因为c#它特别是一种面向对象的语言,所以你在一个类中声明的每一件事都没有访问说明符,编译器认为它是私有的。所以声明你的构造函数public,这将解决这个问题

public statuseffect(string Effect,int Amount,int Duration)
    {
        string effect = Effect;
        int amount = Amount;
        int duration = Duration;
    }
于 2012-08-17T07:48:15.180 回答
0

公开您的构造函数。默认情况下,当您不指定任何访问说明符时,它会变为私有,这意味着它只能在您的类中使用。将其公开可以从外部访问。

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

此外,对这些变量做一些事情,它们是没有用的。

于 2012-08-17T07:48:46.083 回答
-1
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);
    }
}
于 2015-02-20T00:52:49.643 回答