標題可能有點不具說明性,但我會嘗試在此處儘可能清楚地解釋情況。我創建了一個將由其他人創建的類繼承的類。我的類包含使用我創建了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
有沒有一種方法或解決方法可以讓最終用戶輕鬆使用?
您可以嘗試使用泛型。 – MarcinJuraszek