在类中达到类型成员

时间:2013-03-18 08:54:34

标签: c# class

我有两种类型,如下所示:

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变量。我怎么能这样做?

3 个答案:

答案 0 :(得分: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 
    }
}

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

实现的一些方法

1-从T1继承T2,

2-通过T1作为方法参数