1
我的Excel表创建一个包含各种图层的地图,显示由线条,正方形,点和三角形组成的网络。我有一个带参数的每个创作功能,如使用类对象制作不同的形状
cellLocation As Range '(takes a given cell location)
shapeType As String '(oval, triangle, rectangle)
Color '(red, black, whatever)
sizeFactor As Double '(factors the shape size as a function of cell's width)
我只是了解现在的类,但不知道是否班会在这种情况下是有用的,我怎么可能用它们来简化我的代码,而不是具有6个不同参数等的功能。
本来我有这样的功能:
Function CreateWell(cellRng As Range, wellName As String)
'creates a square of particular color, size, etc in the cellRng and names it wellName
Function CreateCompressor(cellRng As Range, compName As String)
'creates an oval of particular color, size , etc. similar to other func.
然后,因为我的这些其中唯一的变化是颜色,大小,形状等我试图使整体功能约5:
Function CreateShape(cellRng As Range, shpName As String, _
shpColor As String, shpSize as double, shpType As string)
但这似乎是混乱的(太多的论据)。雇用类如何清理这种类型的代码?
我会说不。原因是VBa/VB6在继承方面没有真正的类支持,并且对于你正在做的事情,使用泛型函数来做到这一点很好。此外,也许看看可选参数,所以一切都不必传入。 – Jeremy
@杰瑞米感谢您的洞察力。你对类继承有什么意义,我对此并不熟悉。 – teepee
你可以查看这篇文章了。 https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) – Jeremy