Fortran Coder

查看: 8267|回复: 6
打印 上一主题 下一主题

[派生类型] 球坐标和直角坐标能不能继承于同一个父类的问题

[复制链接]

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
楼主
发表于 2019-1-13 21:11:25 | 显示全部楼层
[Fortran] 纯文本查看 复制代码
module Point_m
  implicit none
  type,abstract:: Point
  end type
end module
module Point2D_m
use Point_m
  implicit none
  type,extends(Point):: Point2D
    real x, y
  end type
end module
module Point3D_m
use Point2D_m
  implicit none
  type,extends(Point2D):: Point3D
    real z
  end type
end module  
module Point2D_sph_m
use Point_m
  implicit none
  type,extends(Point):: Point2D_sph
    real r, phi
  end type
end module
module Point3D_sph_m
use Point2D_sph_m
use Point3D_m
  implicit none
  type,extends(Point2D_sph):: Point3D_sph
    real theta
  contains
  procedure XYZ2Sph, sph
  generic:: assignment(=) => XYZ2Sph, sph
  end type
  contains
  subroutine XYZ2Sph(this,other)
    implicit none
    class(Point3D_sph),intent(inout) :: this
    class(Point2D),intent(in) :: other
    select type(other)
    class is(Point2D)
      this%r = 1
      this%phi = 2
      this%theta = 0
    class is(Point3D)
      this%r = 1
      this%phi = 2
      this%theta = 3
    end select
  end subroutine XYZ2Sph

  subroutine Sph(this,other)
    implicit none
    class(Point3D_sph),intent(inout) :: this
    class(Point2D_sph),intent(in) :: other
    select type(other)
    class is(Point2D_sph)
      this%r = 5
      this%phi = 6
      this%theta = 0
    class is(Point3D_sph)
      this%r = 5
      this%phi = 6
      this%theta = 7
    end select
  end subroutine sph
  end module  
  
  program test
  use Point3D_m
  use Point3D_sph_m
  type(point2d) a
  type(point3d) b
  type(point2d_sph) c
  type(point3d_sph) d
  d = a
  print*,d
  d = b
  print*,d
  d = c
  print*,d
  d = d
  print*,d
  pause
  end

  
  

QQ截图20190113211020.png (23.29 KB, 下载次数: 266)

QQ截图20190113211020.png
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-3 11:17

Powered by Tencent X3.4

© 2013-2024 Tencent

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