【发布时间】:2014-03-02 01:20:00
【问题描述】:
标题可能有点难以描述,但我会在这里尽可能清楚地解释情况。我创建了一个类,该类将被其他人创建的类继承。我的类包含一个使用我创建的 IShape 接口的形状结构变量,如下所示:
IShape Bounds = new Rectangle(/*arguments go here*/);
如何让最终用户控制要使用的形状,同时仍然允许他访问所选形状的方法和字段(无需 24/7 开箱)?基本上我希望用户选择他想要的形状,但仍然有 IShape 形式的形状来执行诸如碰撞之类的东西。例如:
Bounds = new Triangle(/*arguments*/);
Bounds.Point3 = new Point(20f, 20f); //this property would be from the Triangle : IShape struct only and is not inherited from IShape
有没有办法或解决方法让最终用户轻松完成这项工作?
【问题讨论】:
-
你可以尝试使用泛型。