Fortran Coder

标题: 一个关于继承的问题 [打印本页]

作者: lookbook    时间: 2019-1-9 00:28
标题: 一个关于继承的问题
[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

我写的代码如上,目的是实现等号功能,
1、如果等号左边是某个继承类,而右边是同一个父类的其他继承类,则a想等。
2、如果等号左边是某个继承类,而右边是该继承类的子类,则a,b相等。

在加入红色字符之后,上面程序会引起冲突:Ambiguous interfaces in intrinsic assignment operator for ‘equation’ at (1) and ‘equation_own’ at (2)
还望组里大大不吝赐教,小弟十分感谢。




作者: lookbook    时间: 2019-1-9 12:42
问题已经解决,使用select type... class is() ...class is() ...end select 即可
作者: li913    时间: 2019-1-10 20:53
不吝赐教,希望学习一下改后的代码。




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2