0

我有两种类型,定义如下:

public class T1
{
    public int A1;
    public int B1;
}

public class T2
{
    public int A2;
    public int B2;
}

我有一个包含 T1 和 T2 列表的类:

public class Topology
{
    public List<T1> T1s;
    public List<T2> T2s;
}

我想在 T2 类中创建一个方法并想要访问 T1s 变量。我怎样才能做到这一点?

4

3 回答 3

2

通过构造函数将 T1 引用传递给 T2 怎么样?

public class T2
{
    private T1 _t1Reference;

    public int A2;
    public int B2;

    public T2(T1 t1Reference)
    {
       _t1Reference = t1Reference;    
    }

    public void T2Method()
    {
       //Access _t1Reference here 
    }
}

或者通过 T2 的方法参数传递对 T1 实例的引用?

public class T2
{
    public int A2;
    public int B2;

    public void T2Method(T1 t1Reference)
    {
       //Access t1Reference here 
    }
}
于 2013-03-18T08:57:42.320 回答
1

在 T2 中创建一个公共方法,并在参数中传递 T1 的对象。

public class T2
{
    public int A2;
    public int B2;

    public void YourMethod(T1 t1)
    {
       string a1 = t1.A1;
       string b1 = t1.B1;
    }
}
于 2013-03-18T08:56:27.377 回答
0

一些实现的方法

1- 从 T1 继承 T2,

2- 传递 T1 作为方法参数

于 2013-03-18T09:00:29.307 回答