型別程式
為了獲得類似行為,型別和相關程式(子程式和函式)應放在一個模組中:
例:
module MShape
implicit none
private
type, public::Shape
private
integer::radius
contains
procedure::set => shape_set_radius
procedure::print => shape_print
end type Shape
contains
subroutine shape_set_radius(this, value)
class(Shape), intent(in out) :: self
integer, intent(in) :: value
self%radius = value
end subroutine shape_set_radius
subroutine shape_print(this)
class(Shape), intent(in) :: self
print *, 'Shape: r = ', self%radius
end subroutine shape_print
end module MShape
稍後,在程式碼中,我們可以使用此 Shape 類,如下所示:
! declare a variable of type Shape
type(Shape) :: shape
! call the type-bound subroutine
call shape%set(10)
call shape%print