[Fortran] 纯文本查看 复制代码
module m_father
implicit none
type,abstract :: t_father
integer :: a
contains
procedure(equation),pass(this),deferred :: equation
end type t_father
abstract interface
module subroutine equation(this,other)
implicit none
class(t_father),intent(inout) :: this
class(t_father),intent(in) :: other
end subroutine
end interface
private equation
end module m_father
module m_son_1
use m_father
implicit none
type,extends(t_father) :: t_son_1
integer :: b
contains
procedure,pass(this) :: equation
procedure,pass(this) :: equation_own
generic,public :: assignment(=) => equation,equation_own
end type t_son_1
private equation
contains
subroutine equation(this,other)
implicit none
class(t_son_1),intent(inout) :: this
class(t_father),intent(in) :: other
this%a=other%a
end subroutine equation
subroutine equation_own(this,other)
implicit none
class(t_son_1),intent(inout) :: this
class(t_son_1),intent(in) :: other
this%a=other%a
this%b=other%b
end subroutine equation_own
end module m_son_1
module m_son_2
use m_father
implicit none
type,extends(t_father) :: t_son_2
integer :: c
contains
procedure,pass(this) :: equation
generic,public :: assignment(=) => equation
end type t_son_2
private equation
contains
subroutine equation(this,other)
implicit none
class(t_son_2),intent(inout) :: this
class(t_father),intent(in) :: other
this%a=other%a
end subroutine equation
end module m_son_2
program main
use m_son_1
use m_son_2
implicit none
type(t_son_1) :: a
type(t_son_2) :: b
b%a=1
a=b
call a%equation(b)
print *,a%a
end program main