我正在为学校中的作业创建自定义泛型类。我现在卡住了,我非常感谢以下错误的一些帮助。我正在给数组添加参数obj。问题是,obj永远不会改变,因此如果我是“蛋糕”的对象,数组中的所有索引都会说蛋糕,而不是其他字符串。自定义泛型类,预期方法名称
class minlist<T>
{
T[] storage = new T[3];
public void Add(T obj)
{
if (storage.Length != 3)
{
storage[0] = obj;
storage[1] = obj;
storage[2] = obj;
storage[3] = obj;
}
我觉得如果我把obj作为一个数组,就可以解决问题。因此,能够排序在不同的索引中的每个字符串,我会把蛋糕放在0和饼图1等
当我编辑公共无效添加(T obj)的参数,并使其公共无效添加(T [ ] obj)
我也有为obj添加索引。
storage[0] = obj[0];
storage[1] = obj[1];
storage[2] = obj[2];
storage[3] = obj[3];
在主,我想补充一点,以它通常看起来像这样的列表,而无需在add方法更改的参数。
minlist<authorinfo> aif = new minlist<authorinfo>();
aif.Add(new authorinfo("The Count of Monte Cristo","Alexandre", "Dumas", 1844));
当我将add参数更改为[] obj我必须在main中执行以下操作。
minlist<authorinfo> aif = new minlist<authorinfo>();
aif.Add(new authorinfo[0]("The Count of Monte Cristo","Alexandre", "Dumas", 1844));
这里的问题是[0]不起作用。我收到一个表示方法名称的错误。我不知道该怎么做。
对不起,如果这个问题有点混乱。
这是关于'authorinfo'的类定义。如果第一个工作,第二个不使用第一个,不是? – MoonKnight 2012-03-07 10:50:46