如何在Excel VBA中使用Implements
我正在尝试为工程项目实现一些形状并将其抽象出来用于一些常见功能,以便我可以使用通用程序。
我正在尝试做的是有一个调用的接口,cShape
并拥有cRectangle
并cCircle
实现cShape
我的代码如下:
cShape
接口
Option ExplicitPublic Function getArea()End FunctionPublic Function getInertiaX()End FunctionPublic Function getInertiaY()End FunctionPublic Function toString()End Function
cRectangle
类
Option ExplicitImplements cShapePublic myLength As Double ''going to treat length as dPublic myWidth As Double ''going to treat width as bPublic Function getArea() getArea = myLength * myWidthEnd FunctionPublic Function getInertiaX() getInertiaX = (myWidth) * (myLength ^ 3)End FunctionPublic Function getInertiaY() getInertiaY = (myLength) * (myWidth ^ 3)End FunctionPublic Function toString() toString = "This is a " & myWidth & " by " & myLength & " rectangle."End Function
cCircle
类
Option ExplicitImplements cShapePublic myRadius As DoublePublic Function getDiameter() getDiameter = 2 * myRadiusEnd FunctionPublic Function getArea() getArea = Application.WorksheetFunction.Pi() * (myRadius ^ 2)End Function''Inertia around the X axisPublic Function getInertiaX() getInertiaX = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)End Function''Inertia around the Y axis''Ix = Iy in a circle, technically should use same functionPublic Function getInertiaY() getInertiaY = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)End FunctionPublic Function toString() toString = "This is a radius " & myRadius & " circle."End Function
问题是每当我运行我的测试用例时,它都会出现以下错误:
编译错误:
对象模块需要为接口'〜'实现'〜'
函数式编程