我需要将矩形类型的变量传递给接口中的函数
i need to pass rectangle type of variable to function which is in interface
public class QuadTree<T> where T : IHasRect
{
private List<T> m_objects = null; // The objects in this
private Rectangle m_rect; // The area this
// Getters and Setters
public List<T> Objects { get { return m_objects; } }
public Rectangle Rect { get {return m_rect; } }
public QuadTree(Rectangle rect)
{
m_rect = rect;
}
public QuadTree(int x, int y, int width, int height)
{
m_rect = new Rectangle(x, y, width, height);
}
public void Insert(T item)
{
// If this quad doesn't intersect the items rectangle, do nothing
if (!m_rect.IntersectsWith(item.Rect))
return;
}
Main Class
static class MainProgram
{
static void Main() {
QuadTree<IHasRect> qt = new QuadTree<IHasRect>(3,6,6,6);
Rectangle rect = qt.Rect;
Console.WriteLine(rect);
// Console.WriteLine(qt.Rect);
// QuadTree<IHasRect> qt1 = new QuadTree<IHasRect>(rect);
Rectangle area = new Rectangle(3, 6, 3, 3);
IHasRect area1 = new QuadTree<IHasRect>(area);
qt.Insert(area1);
Console.WriteLine("Hello");
List<Item> myList=null;
Console.WriteLine(myList[0]);
Console.WriteLine(myList[1]);
Console.Read();
}}
使用的界面
public 接口 IHasRect
{
// 定义对象边界的矩形。
矩形 Rect { get; }
}
我需要通过qt.Insert()传递值
我需要单独的 Class 来实现 S T
部分
where T : IHasRect
要求 T
实现接口 IHasRect,这意味着它必须有一个 getter 称为 Rect,returns 一个 Rectangle。
您需要针对任何类型实现该方法 T
。
然后,你会使用像
这样的东西
public class MyClass : IHasRect
{
public Rectangle Rect { get {return m_rect; } }
// You need some way to set m_rect, and you
// will probably have other properties as well.
}
QuadTree<MyClass> qt = new QuadTree<MyClass>();
qd.Insert(new MyClass());
Rectangle r = qt.First().Rect;
So what about this function public void Insert(T item) { ... } how to resolve T if i m not implementing interface with Quadtree class
QuadTree<T>
上的通用约束说明 T 必须实现 IHasRect。您自己的代码示例表明:
public void Insert(T item)
{
// If this quad doesn't intersect the items rectangle, do nothing
if (!m_rect.IntersectsWith(item.Rect))
return;
}
无论 T 在编译时最终是什么类型,您的代码都知道它会有一个 Rect getter。
public class QuadTree<T> where T : IHasRect
{
private List<T> m_objects = null; // The objects in this
private Rectangle m_rect; // The area this
// Getters and Setters
public List<T> Objects { get { return m_objects; } }
public Rectangle Rect { get {return m_rect; } }
public QuadTree(Rectangle rect)
{
m_rect = rect;
}
public QuadTree(int x, int y, int width, int height)
{
m_rect = new Rectangle(x, y, width, height);
}
public void Insert(T item)
{
// If this quad doesn't intersect the items rectangle, do nothing
if (!m_rect.IntersectsWith(item.Rect))
return;
}
Main Class
static class MainProgram
{
static void Main() {
QuadTree<IHasRect> qt = new QuadTree<IHasRect>(3,6,6,6);
Rectangle rect = qt.Rect;
Console.WriteLine(rect);
// Console.WriteLine(qt.Rect);
// QuadTree<IHasRect> qt1 = new QuadTree<IHasRect>(rect);
Rectangle area = new Rectangle(3, 6, 3, 3);
IHasRect area1 = new QuadTree<IHasRect>(area);
qt.Insert(area1);
Console.WriteLine("Hello");
List<Item> myList=null;
Console.WriteLine(myList[0]);
Console.WriteLine(myList[1]);
Console.Read();
}}
使用的界面
public 接口 IHasRect
{
// 定义对象边界的矩形。
矩形 Rect { get; }
}
我需要通过qt.Insert()传递值 我需要单独的 Class 来实现 S T
部分
where T : IHasRect
要求 T
实现接口 IHasRect,这意味着它必须有一个 getter 称为 Rect,returns 一个 Rectangle。
您需要针对任何类型实现该方法 T
。
然后,你会使用像
这样的东西public class MyClass : IHasRect
{
public Rectangle Rect { get {return m_rect; } }
// You need some way to set m_rect, and you
// will probably have other properties as well.
}
QuadTree<MyClass> qt = new QuadTree<MyClass>();
qd.Insert(new MyClass());
Rectangle r = qt.First().Rect;
So what about this function public void Insert(T item) { ... } how to resolve T if i m not implementing interface with Quadtree class
QuadTree<T>
上的通用约束说明 T 必须实现 IHasRect。您自己的代码示例表明:
public void Insert(T item)
{
// If this quad doesn't intersect the items rectangle, do nothing
if (!m_rect.IntersectsWith(item.Rect))
return;
}
无论 T 在编译时最终是什么类型,您的代码都知道它会有一个 Rect getter。