[英]Call a method that requires a derived class instance typed as base class in VB.NET or C#
I have two objects - "Spaceship" and "Planet" derived from a base "Obj". 我有两个对象-从基本“ Obj”派生的“太空飞船”和“行星”。 I have defined several classes - Circle, Triangle, Rectangle, etc. which all inherit from a "Shape" Class. 我定义了几个类-Circle,Triangle,Rectangle等,它们都从“ Shape”类继承。
For collision detection purposes, I want to give Obj a "shape": 为了检测冲突,我想给Obj一个“形状”:
Dim MyShape as Shape
So that in "Spaceship" I can: 这样,在“太空飞船”中,我可以:
MyShape = new Triangle(blah,blah)
and in "Planet" I can: 在“行星”中,我可以:
MyShape = new Circle(blah,blah)
I have a method (overloaded several times) which checks for collisions between different shapes, for example: 我有一个方法(多次重载),用于检查不同形状之间的碰撞,例如:
public shared overloads function intersects(byval circle1 as circle, byval circle2 as circle) as boolean
AND 和
public shared overloads function intersects(byval circle as circle, byval Tri as triangle) as boolean
This works fine when I call the function using the derived classes, for example: 当我使用派生类调用函数时,这可以很好地工作,例如:
dim A as new circle(blah, blah)
dim B as new triangle(blah, blah)
return intersects(A,B)
But when I call it using MyShape, I get an error because the method is being passed a "Shape" (rather than the derived type) which the method does not have an overload for. 但是,当我使用MyShape调用它时,出现了一个错误,因为该方法正在传递该方法没有重载的“ Shape”(而不是派生类型)。
I could solve it by doing something like: 我可以通过做类似的事情来解决它:
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
But that seems messy. 但这似乎很混乱。 Is there a better way? 有没有更好的办法?
A way around it is to insert MyActualFunction
as a class member. 解决方法是将MyActualFunction
插入为类成员。
In Shape: 形状:
Public MustOverride Function MyActualFunction()
End Function
In Circle and Triangle: 在圆和三角形中:
Public Overrides Function MyActualFunction()
End Function
Then call it like that: 然后这样称呼它:
MyShape.MyActualFunction()
and this will know which function to call. 这将知道要调用哪个函数。
Polymorphism can't help you with that therefore you'll have to create a common method with both parameters of type Shape
and then distinguish them inside it: 多态性无法帮助您,因此您必须使用Shape
类型的两个参数创建一个通用方法,然后在其中区分它们:
Public Function DoCollide(ByRef shape1 As Shape, ByRef shape2 As Shape) As Boolean
If TryCast(shape1, Circle) IsNot Nothing And TryCast(shape2, Circle) IsNot Nothing Then
Return DoCollide(TryCast(shape1, Circle), TryCast(shape2, Circle))
ElseIf TryCast(shape1, Circle) IsNot Nothing And TryCast(shape2, Triangle) IsNot Nothing Then
Return DoCollide(TryCast(shape1, Circle), TryCast(shape2, Triangle))
Else
Return False
End If
End Function
I would put this function along with all the specialized implementations doing the actual collision detection in their own class CollisionDetector
我将此功能与所有专门的实现实际碰撞检测的专用实现一起放在自己的类CollisionDetector
Public Class CollisionDetector
Public Function DoCollide(ByRef shape1 As Shape, ByRef shape2 As Shape) As Boolean
If TryCast(shape1, Circle) IsNot Nothing And TryCast(shape2, Circle) IsNot Nothing Then
Return DoCollide(TryCast(shape1, Circle), TryCast(shape2, Circle))
ElseIf TryCast(shape1, Circle) IsNot Nothing And TryCast(shape2, Triangle) IsNot Nothing Then
Return DoCollide(TryCast(shape1, Circle), TryCast(shape2, Triangle))
Else
Return False
End If
End Function
Public Function DoCollide(ByRef circle1 As Circle, ByRef circle2 As Circle) As Boolean
Return True
End Function
Public Function DoCollide(ByRef circle As Circle, ByRef triangle As Triangle) As Boolean
Return True
End Function
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.