简体   繁体   English

调用需要在VB.NET或C#中将派生类实例键入为基类的方法

[英]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.

相关问题 如何从 C# 中的派生类实例调用基方法? - How to call base method from derived class instance in C#? 父类的子节点与VB.NET或C#中的基类的派生有什么区别? - What is the difference between a child of a parent class and the derived of a base class in VB.NET or C#? 在C#中如何从派生类实例调用已经在派生类中覆盖的基类方法 - in C# how to invoke base class method, which already override in derived class, from derived class instance 您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法 - Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C# 如何从VB.Net调用C#类的静态方法? - How to call a C# class's static method from VB.Net? 如何从遥远的类中调用未引用的方法? (允许使用C#和VB.NET) - How do I call an unreferenced method from a distant class? (C# and VB.NET allowed) C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? C#继承防止基类方法调用派生类方法 - C# Inheritance Prevent Base Class Method Call Derived Class method C#基类方法无法读取派生类中的实例属性 - C# base class method can not read instance attribute which is in derived class C#在基类中创建派生类的实例 - C# create instance of the derived class in the base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM