include 'class_Date.f90'
module class_Person ! filename: class_Person.f90
use class_Date
implicit none
public :: Person
type Person
private
character (len=20) :: name
character (len=20) :: nationality
integer :: sex
type (Date) :: dob, dod ! birth, death
end type Person
contains
function make_Person (nam, nation, s, b, d) result (who)
! Optional Constructor for a Person type
character (len=*), optional, intent(in) :: nam, nation
integer, optional, intent(in) :: s ! sex
type (Date), optional, intent(in) :: b, d ! birth, death
type (Person) :: who
who = Person (" ","USA",1,Date_(1,1,0),Date_(1,1,0)) ! defaults
if ( present(nam) ) who%name = nam
if ( present(nation) ) who%nationality = nation
if ( present(s) ) who%sex = s
if ( present(b) ) who%dob = b
if ( present(d) ) who%dod = d ; end function
function Person_ (nam, nation, s, b, d) result (who)
! Public Constructor for a Person type
character (len=*), intent(in) :: nam, nation
integer, intent(in) :: s ! sex
type (Date), intent(in) :: b, d ! birth, death
type (Person) :: who
who = Person (nam, nation, s, b, d) ; end function Person_
subroutine print_DOB (who)
type (Person), intent(in) :: who
call print_Date (who%dob) ; end subroutine print_DOB
subroutine print_DOD (who)
type (Person), intent(in) :: who
call print_Date (who%dod) ; end subroutine print_DOD
subroutine print_Name (who)
type (Person), intent(in) :: who
print *, who%name ; end subroutine print_Name
subroutine print_Nationality (who)
type (Person), intent(in) :: who
print *, who%nationality ; end subroutine print_Nationality
subroutine print_Sex (who)
type (Person), intent(in) :: who
if ( who%sex == 1 ) then ; print *, "male"
else ; print *, "female" ; end if ; end subroutine print_Sex
subroutine set_DOB (who, m, d, y)
type (Person), intent(inout) :: who
integer, intent(in) :: m, d, y ! month, day, year
who%dob = Date_ (m, d, y) ; end subroutine set_DOB
subroutine set_DOD(who, m, d, y)
type (Person), intent(inout) :: who
integer, intent(in) :: m, d, y ! month, day, year
who%dod = Date_ (m, d, y) ; end subroutine set_DOD
end module class_Person
fortran OOP(2) class_Person.f90
最新推荐文章于 2023-02-28 14:17:00 发布