通用类 [英] Generic class

查看:52
本文介绍了通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,
我想知道如何将我的物品"Person"放入在数组中考虑Person是泛型类的实例.当我尝试时,出现此错误对象引用未设置为对象的实例".
我的此类:


使用

Hi Guys,
I'd like to know how to put my objects "Person" in an array considering that Person is an instance of a generic class. when i try i get this error " Object reference not set to an instance of an object".
This my class:


using

系统;

使用

System.Collections.Generic;

使用

System.Linq;

使用

System.Text;

命名空间

genClassMatrix

{

genClassMatrix

{

class Program

{

公共 矩阵

public class Matrix<T>

{

T [,] Arr;

T[,] Arr;

int 大小;

int Size;

 

公共 矩阵( 行, int

public Matrix(int rows, int cols)

{

Arr =

T [rows,cols];

Size = Arr.Length;

new T[rows, cols];

Size = Arr.Length;

}

公共 Clear()

{

public void Clear()

{

( i = 0; i< 3; i ++)

{

for (int i = 0; i <3; i++)

{

( j = 0; j< 3; j ++)

{

for (int j = 0; j <3; j++)

{

Arr [i,j] =

Arr[i, j] =

默认 (T);

}

default(T);

}

}

}

公共 SetItem( int int col,T项)

{

public void SetItem(int row, int col, T Item)

{

( i = 0; i< Size; i ++)

{

for (int i = 0; i < Size; i++)

{

( j = 0; j< Size; j ++)

{

for (int j = 0; j < Size; j++)

{

如果 ((i ==行)&&(j == col))

{

if ((i == row) && (j == col))

{

如果 (Arr [row,col] .Equals( 默认 (T)))

{

if (Arr[row, col].Equals(default(T)))

{

Arr [row,col] =物品;

Arr[row, col] = Item;

}

}

}

}

}

公共 T GetItem( 行, int

public T GetItem(int row, int col)

{

( i = 0; i< Size; i ++)

{

for (int i = 0; i < Size; i++)

{

( j = 0; j< Size; j ++)

{

for (int j = 0; j < Size; j++)

{

如果 ((i == row)&&(j == col))

{

if ((i==row)&&(j==col))

{

如果 (Arr [i,j] .Equals(Arr [row,col]))

{

if (Arr[i, j].Equals(Arr[row, col]))

{

控制台 .WriteLine(Arr [i,j] .ToString());

}

Console.WriteLine(Arr[i, j].ToString());

}

}

}

}

返回 (T);

}

return default(T);

}

公共 存在(T物品)

{

public bool Exist(T Item)

{

( i = 0; i< 2; i ++)

{

for (int i = 0; i < 2; i++)

{

( j = 2; j> = 0; j--)

{

for (int j =2; j >=0; j--)

{

如果 (Equals(Arr [i,j],(Item)))

if (Equals(Arr[i, j],(Item)))

 

返回 ;

return true;

 

}

}

返回 ;

}

return false;

}

}

class Person

{

公共 id;

public int id;

 

公共 名称;

}

public string Name;

}

静态 Main( 字符串

static void Main(string[] args)

{

矩阵 < > MatInt = < int

Matrix<int> MatInt = new Matrix<int>(3,3);

 

矩阵 < > MatBool = < bool

Matrix<bool> MatBool = new Matrix<bool>(3,3);

 

矩阵 < > MatPersons = <

Matrix<Person> MatPersons = new Matrix<Person>(3,3);

 

p1 =

Person p1 = new Person();

 

p2 =

Person p2 = new Person();

 

p3 =

Person p3 = new Person();

 

//******* *******

p1.id = 345;

p1.id = 345;

p1.Name =

p1.Name =

"Patrick" ;

p2.id = 677;

"Patrick";

p2.id = 677;

p2.Name =

p2.Name =

"jonathan" ;

p3.id = 5555;

"jonathan";

p3.id = 5555;

p3.Name =

p3.Name =

"allan" ;

"allan";

 

//******* *******

MatPersons.SetItem(1,1,p1);

MatPersons.SetItem(1, 1, p1);

MatPersons.SetItem(2,2,p2);

MatPersons.SetItem(2, 2, p2);

MatPersons.SetItem(0,0,p3);

MatPersons.SetItem(0, 0, p3);

//******* *******

//Console.WriteLine(MatPersons.GetItem (1,2));

//******* *******

/* MatInt.SetItem(0,0 ,25);

MatInt.SetItem(1、1、15);

/*MatInt.SetItem(0, 0, 25);

MatInt.SetItem(1, 1, 15);

MatInt.SetItem(2,2,35);

MatInt.SetItem(2, 2, 35);

MatInt.GetItem(0,0);

MatInt.GetItem(0, 0);

MatInt.GetItem(2,2);

MatInt.GetItem(2, 2);

MatInt.GetItem(1,1);

MatInt.GetItem(1, 1);

Console.WriteLine();

Console.WriteLine();

如果(MatInt.Exist(13))

if (MatInt.Exist(13))

{

Console.WriteLine("True.");

Console.WriteLine("True.");

}

其他

{

Console.WriteLine("False.");

Console.WriteLine("False.");

}

Console.WriteLine();

Console.WriteLine();

MatInt.Clear();

MatInt.Clear();

MatInt.GetItem(0,0);

MatInt.GetItem(0, 0);

MatInt.GetItem(1,1);

MatInt.GetItem(1, 1);

MatInt.GetItem(2,2);

MatInt.GetItem(2, 2);

Console.WriteLine();

Console.WriteLine();

//**************

//**************

MatBool.SetItem(0,1,true);

MatBool.SetItem(0, 1, true);

MatBool.SetItem(1,1,假);

MatBool.SetItem(1, 1, false);

MatBool.SetItem(2,2,true);

MatBool.SetItem(2, 2, true);

MatBool.SetItem(0,2,true);

MatBool.SetItem(0, 2, true);

MatBool.GetItem(0,0);

MatBool.GetItem(0, 0);

MatBool.GetItem(1,1);

MatBool.GetItem(1, 1);

MatBool.GetItem(2,1);

MatBool.GetItem(2, 1);

MatBool.GetItem(0,2);

MatBool.GetItem(0, 2);

Console.WriteLine();

Console.WriteLine();

如果(MatBool.Exist(true))

if (MatBool.Exist(true))

{

Console.WriteLine("True.");

Console.WriteLine("True.");

}

其他

{

Console.WriteLine("False.");

Console.WriteLine("False.");

}

Console.WriteLine();

Console.WriteLine();

MatBool.Clear();

MatBool.Clear();

如果(MatBool.Exist(true))

if (MatBool.Exist(true))

{

Console.WriteLine("True.");

Console.WriteLine("True.");

}

其他

{

Console.WriteLine("False.");

Console.WriteLine("False.");

}

Console.WriteLine();

Console.WriteLine();

MatBool.GetItem(0,0);

MatBool.GetItem(0, 0);

MatBool.GetItem(1,1);

MatBool.GetItem(1, 1);

MatBool.GetItem(2,1);

MatBool.GetItem(2, 1);

MatBool.GetItem(0,2);

MatBool.GetItem(0, 2);

Console.Read(); */

Console.Read();*/

}

}

}

谢谢.

}

Thanks.

推荐答案



defult(T)不会创建实例.而且Arr [i,j]为null.

在您的SetItem方法中,更改if语句,如下所示.

Hi,

defult(T) does not create instance. And Arr[i,j] is null.

At your SetItem method change the if statement like below.

 if (Arr[row, col] ==null || Arr[row, col].Equals(default(T)))
                            {
                                Arr[row, col] = Item;

                            }


这篇关于通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆