- UID
- 3599
- 性别
- 保密
齊天大聖
- 积分
- 778
- F 币
- 433 元
- 最后登录
- 2021-10-14
- 贡献
- 266 点
- 注册时间
- 2018-11-8
- 权杖
- 0 枚
惯用编译器:GFortran / G77 for Linux
专家
齊天大聖
- F 币
- 433 元
- 贡献
- 266 点
|
文件:m_father.f90
module m_father
implicit none
type t_father
real :: x,y
contains
procedure,pass(this),private :: 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
this%x=other%x
this%y=other%y
end subroutine equal
end module m_father
module m_a
use m_father
implicit none
文件:m_a.f90
type,extends(t_father) :: t_a
real :: z
contains
procedure,pass(this),private :: 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
select type(other)
class is(t_a)
this%x=other%x
this%y=other%y
this%z=other%z
class is(t_father)
this%x=other%x
this%y=other%y
class default
print*,"erro"
end select
end subroutine equal
end module m_a
文件: m_b.f90
module m_b
use m_father
implicit none
type,extends(t_father) :: t_b
double precision :: z
contains
procedure,pass(this),private :: 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
select type(other)
class is(t_b)
this%x=other%x
this%y=other%y
this%z=other%z
class is(t_father)
this%x=other%x
this%y=other%y
class default
print*,"erro"
end select
end subroutine equal
end module m_b
文件:m_interface.f90
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
this%x=other%x
this%y=other%y
this%z=real(other%z,kind(0.))
end subroutine equal1
subroutine equal2(this,other)
implicit none
type(t_b),intent(inout) :: this
type(t_a),intent(in) :: other
this%x=other%x
this%y=other%y
this%z=real(other%z,kind(0d0))
end subroutine equal2
end module m_interface
=======================================
t_father是t_a和t_b的父类,
里面继承了equal这种方法,
又定义了m_interface这个模块,
里面直接定义了t_a和t_b相等,
在main.f90中使用了 “=”,原先以为会造成混乱,
但目前来看,并不会出现编译的错误,但“=”载入是t_father里的eqaul,而不是interface里面的“=”并没有载入。
有没有办法载入interface中的“=”号,by the way 这真是个有意思的问题。
|
|