我在.NET控制台中创建了一个渲染引擎,为此我已经使用Inherited RenderObject构建了该结构。
此RenderObject是一个类,用于存储继承此对象的对象的所有渲染信息。例如,X,Y坐标,宽度和高度等。
我试图做以下事情。但它反而从RenderObject(继承对象)而不是子对象调用Render函数。
我如何实现这一目标?调用Childs渲染函数而不是Parent渲染函数。
谢谢!
渲染器:
class Renderer {
public List<RenderObject> RenderObjects { get; }
public Renderer()
{
RenderObjects = new List<RenderObject>();
AddRenderObject(new RenderText());
AddRenderObject(new RenderBox());
}
public void AddRenderObject(RenderObject ro)
{
RenderObjects.Add(ro);
}
public void Draw()
{
foreach(RenderObject ro in RenderObjects)
{
ro.Render(); //This should call the function from RenderText and RenderBox
}
}
}
父类(可以继承的类)
class RenderObject
{
public int X;
public int Y;
public int Width;
public int Height;
public void Render()
{
//This should NOT be called
}
}
继承RenderObject的子对象
class RenderBox : RenderObject
{
public void Render()
{
//This should be called
}
}
class RenderText : RenderObject
{
public void Render()
{
//This should be called
}
}
答案 0 :(得分:1)
您应将其标记为virtual
并在子类
class RenderObject
{
public int X;
public int Y;
public int Width;
public int Height;
public virtual void Render()
{
//This should NOT be called
}
}
class RenderBox : RenderObject
{
public override void Render()
{
//This should be called
}
}
class RenderText : RenderObject
{
public override void Render()
{
//This should be called
}
}