Fortran Coder

查看: 6232|回复: 3
打印 上一主题 下一主题

[面向对象] 一个有意思的问题

[复制链接]

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
楼主
发表于 2019-1-11 19:59:31 | 显示全部楼层
本帖最后由 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
  

QQ截图20190111195835.png (50.64 KB, 下载次数: 205)

QQ截图20190111195835.png

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
沙发
发表于 2019-1-13 16:16:24 | 显示全部楼层
可能是编译器差异,这就不好搞了。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-7 03:45

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表