请耐心等待我,因为我正在学习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);
}
}
Run Code Online (Sandbox Code Playgroud)
在最后一行,编译器告诉我statuseffect不包含一个带3个参数的构造函数.据我所知,它确实包含3个参数.这里有什么我想念的吗?
作为旁注.如果你们对我的剧本有任何意见或建议,那也会有所帮助.
你的构造函数private对于类本身之外的代码是"不可见的".尝试internal在构造函数之前添加关键字.或者,如果需要从其他项目中看到它,请添加public.
另一个问题:在你的类中statuseffect,你在构造函数中声明了三个局部变量.这些变量的唯一范围是构造函数.您必须将其声明移出构造函数(然后它们将成为该类的实例字段).构造函数仍然可以分配给它们.
| 归档时间: |
|
| 查看次数: |
12830 次 |
| 最近记录: |