是否可以根据参数值更改程序调用的函数?我正在考虑类似于函数重载的东西,下面的例子显示了我在想什么。我只是想知道是否有更干净/更好的方法。
function squareArea(s) result(A)
real :: s, A
A = s*s
end function squareArea
function circleArea(r) result(A)
real :: r, A
A = 3.14159 * r * r
end function circleArea
function Area(shape, dim) result(A)
character(len = *) shape
real dim, A
if (shape == 'circle') then
A = circleArea(dim)
elseif (shape == 'square') then
A = squareArea(dim)
end if
end function Area
program main
character(len = 6) :: sh = 'circle'
real :: r = 1.4
real :: A
A = Area(sh, r)
write(*,*) sh, r, A
end program main
答案 0 :(得分:2)
是的 - 你展示了一种可能的方式。
Fortran 2003允许根据用于引用该过程的对象的动态类型来覆盖过程。这是否更好/更清洁取决于您的具体情况。
MODULE Shapes
IMPLICIT NONE
TYPE, ABSTRACT :: Shape
CONTAINS
PROCEDURE(shape_Area), DEFERRED :: Area
END TYPE Shape
INTERFACE
FUNCTION shape_Area(sh) RESULT(area)
IMPORT :: Shape
IMPLICIT NONE
CLASS(Shape), INTENT(IN) :: sh
REAL :: area
END FUNCTION shape_Area
END INTERFACE
TYPE, EXTENDS(Shape) :: Circle
REAL :: radius
CONTAINS
PROCEDURE :: Area => circle_Area
END TYPE Circle
TYPE, EXTENDS(Shape) :: Square
REAL :: side
CONTAINS
PROCEDURE :: Area => square_Area
END TYPE Square
CONTAINS
FUNCTION circle_Area(sh) RESULT(area)
CLASS(Circle), INTENT(IN) :: sh
REAL :: area
area = 3.14159 * sh%radius**2
END FUNCTION circle_Area
FUNCTION square_Area(sh) RESULT(area)
CLASS(Square), INTENT(IN) :: sh
REAL :: area
area = sh%side**2
END FUNCTION square_Area
END MODULE Shapes
PROGRAM Areas
USE Shapes
IMPLICIT NONE
TYPE(Circle) :: c = Circle(1.4)
TYPE(Square) :: s = Square(1.4)
CHARACTER(*), PARAMETER :: fmt = "(A,G0,' has area ',G0)"
PRINT fmt, 'Circle with radius ', c%radius, c%Area()
PRINT fmt, 'Square with side ', s%side, s%Area()
END PROGRAM Areas