本帖最后由 li913 于 2019-1-11 20:14 编辑
但“=”载入是t_father里的eqaul,而不是interface里面的“=”并没有载入。
上面这句话有问题,我试了你的代码,得到恰恰相反的结果。
fortran 不能将子类赋值给父类(ivf2017测试不行,后续版本不清楚),所以 t_father 中的‘=’只用于被继承,实际永远用不到。
[Fortran] 纯文本查看 复制代码 module m_father
implicit none
type t_father
real :: x,y
contains
procedure,pass(this),public :: equal
generic,public :: assignment(=) => equal
end type t_father
private equal
contains
subroutine equal(this,other)
implicit none
class(t_father),intent(inout) :: this
class(t_father),intent(in) :: other
print*,'t_father'
end subroutine equal
end module m_father
module m_a
use m_father
implicit none
type,extends(t_father) :: t_a
real :: z
contains
procedure,pass(this),public :: equal
end type t_a
private equal
contains
subroutine equal(this,other)
implicit none
class(t_a),intent(inout) :: this
class(t_father),intent(in) :: other
print*,'t_a'
end subroutine equal
end module m_a
module m_b
use m_father
implicit none
type,extends(t_father) :: t_b
double precision :: z
contains
procedure,pass(this),public :: equal
end type t_b
private equal
contains
subroutine equal(this,other)
implicit none
class(t_b),intent(inout) :: this
class(t_father),intent(in) :: other
print*,'t_b'
end subroutine equal
end module m_b
module m_interface
use m_a
use m_b
implicit none
interface assignment(=)
procedure equal1
procedure equal2
end interface
private equal1,equal2
contains
subroutine equal1(this,other)
implicit none
type(t_a),intent(inout) :: this
type(t_b),intent(in) :: other
print*,'equal1'
end subroutine equal1
subroutine equal2(this,other)
implicit none
type(t_b),intent(inout) :: this
type(t_a),intent(in) :: other
print*,'equal2'
end subroutine equal2
end module m_interface
module m_c
use m_b
implicit none
type,extends(t_b) :: t_c
integer i
contains
procedure,pass(this),public :: equal
end type t_c
private equal
contains
subroutine equal(this,other)
implicit none
class(t_c),intent(inout) :: this
class(t_father),intent(in) :: other
print*,'t_c'
end subroutine equal
end module m_c
program test
use m_a
use m_b
use m_father
use m_c
use m_interface
type(t_father) f
type(t_a) a
type(t_b) b
type(t_c) c !c是b的子类
call f%equal(a) !调用type中的函数
call a%equal(b) !调用type中的函数
!f=a !出错,不能将子类赋值给父类
!b=c !出错,不能将子类赋值给父类
a=b !重载,调用equal1
b=a !重载,调用equal2
c=b !调用type中的函数
a=f !调用type中的函数
pause
end
|