ベースの「オブジェクト」から派生した「宇宙船」と「惑星」の 2 つのオブジェクトがあります。Circle、Triangle、Rectangle など、いくつかのクラスを定義しました。これらはすべて「Shape」クラスから継承されます。
衝突検出の目的で、Obj に「形状」を与えたいと思います。
Dim MyShape as Shape
そのため、「宇宙船」では次のことができます。
MyShape = new Triangle(blah,blah)
「Planet」では次のことができます。
MyShape = new Circle(blah,blah)
たとえば、異なる形状間の衝突をチェックするメソッド(数回オーバーロード)があります。
public shared overloads function intersects(byval circle1 as circle, byval circle2 as circle) as boolean
と
public shared overloads function intersects(byval circle as circle, byval Tri as triangle) as boolean
派生クラスを使用して関数を呼び出すと、これは正常に機能します。たとえば、次のようになります。
dim A as new circle(blah, blah)
dim B as new triangle(blah, blah)
return intersects(A,B)
しかし、MyShape を使用して呼び出すと、メソッドがオーバーロードを持たない (派生型ではなく) "Shape" をメソッドに渡されているため、エラーが発生します。
次のようなことで解決できました。
Public Function Translate(byval MyShape1 as Shape, byval MyShape2 as Shape )as boolean
if shape1.gettype = gettype(circle) and shape2.gettype=gettype(circle) then ''//do circle-circle detection
if shape1.gettype = gettype(triangle) and shape2.gettype=gettype(circle) then ''//do triangle-circle detection
End Function
しかし、それは面倒なようです。より良い方法はありますか?