module class_Rectangle
implicit none
type Rectangle
real :: base, height
end type Rectangle
contains
function rectangle_area (r) result (area)
type(Rectangle), intent(in) :: r
real :: area
area = r%base * r%height
end function rectangle_area
end module class_Rectangle
module class_Circle
real :: pi = 3.1415926535897931d0
type Circle
real :: raidus
end type Circle
contains
function circle_area(c) result(area)
type(Circle), intent(in) :: c
real :: area
area = pi * c%raidus**2
end function circle_area
end module class_Circle
program geometry
use class_Rectangle
use class_Circle
implicit none
interface compute_area
module procedure rectangle_area, circle_area
end interface
type(Rectangle) :: four_sides
type(Circle) :: two_sides
real :: area = 0.0
! initialize a rectangel and compute area
four_sides = Rectangle(2.1, 4.3)
area = compute_area(four_sides)
write(*,*) four_sides, area
! initialize a circle and compute area
two_sides = Circle(5.4)
area = compute_area(two_sides)
write(*,*) two_sides, area
end program geometry
矩形的类class_Rectangle.f90
最新推荐文章于 2022-07-31 14:28:07 发布