Fortran Coder

查看: 6086|回复: 2
打印 上一主题 下一主题

[面向对象] 一个关于继承的问题

[复制链接]

79

帖子

17

主题

0

精华

专家

齊天大聖

F 币
433 元
贡献
266 点
跳转到指定楼层
楼主
发表于 2019-1-9 00:28:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[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)
还望组里大大不吝赐教,小弟十分感谢。



分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

79

帖子

17

主题

0

精华

专家

齊天大聖

F 币
433 元
贡献
266 点
沙发
 楼主| 发表于 2019-1-9 12:42:38 | 只看该作者
问题已经解决,使用select type... class is() ...class is() ...end select 即可

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
板凳
发表于 2019-1-10 20:53:18 | 只看该作者
不吝赐教,希望学习一下改后的代码。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-4-27 02:41

Powered by Tencent X3.4

© 2013-2024 Tencent

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