【发布时间】:2015-08-07 01:07:08
【问题描述】:
我拥有扩展单个 ViewComponent 类的 ViewComponents 类型。在我的视图中,我让它遍历 ViewComponents 并打印它们。不幸的是,它拉的是铸造方法而不是实际的类方法。例如:
using System;
namespace test
{
class Component {
public string getType() {
return "Component";
}
}
class ButtonComponent: Component {
public string getType() {
return "Button";
}
}
public class test
{
public static void Main() {
Component[] components = new Component[1];
components [0] = new ButtonComponent();
Console.WriteLine(components[0].getType()); // prints Component
}
}
}
如何让按钮打印“按钮”而不是“组件”?
【问题讨论】:
标签: c# class methods overriding