C# 类内的到达类型成员
我有两种类型,定义如下:C# 类内的到达类型成员,c#,class,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; } 公共类拓扑 { 公开名单T
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;
}
公共类拓扑
{
公开名单T1s;
公开名单T2s;
}
我想在T2类中创建一个方法,并想访问T1s变量。我该怎么做?在T2中创建一个公共方法,并在参数中传递T1的对象
public class T2
{
public int A2;
public int B2;
public void YourMethod(T1 t1)
{
string a1 = t1.A1;
string b1 = t1.B1;
}
}
在T2中创建一个公共方法,并在参数中传递T1的对象
public class T2
{
public int A2;
public int B2;
public void YourMethod(T1 t1)
{
string a1 = t1.A1;
string b1 = t1.B1;
}
}
通过构造函数将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
}
}
通过构造函数将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-从T1继承T2
2-将T1作为方法参数传递一些实现方法
1-从T1继承T2
2-将T1作为方法参数传递您希望从哪个T1
实例使用变量?您希望从哪个T1
实例使用变量?