class - 如何覆盖用户定义的 I/O 过程?
问题描述
我有一个抽象类,其中包含用于未格式化二进制流的读/写方法。我也有一些从抽象类继承的类,其中一些还有我还想序列化的附加组件。
因此,我希望第一组方法用作“默认”行为,并且在继承的类中覆盖它的方法仅由这些特定类使用。
我试图像这样实现它:
module m
implicit none
type, abstract :: a
integer, public :: num
contains
procedure :: write_a
procedure :: read_a
generic :: write(unformatted) => write_a
generic :: read(unformatted) => read_a
end type a
type, extends(a) :: b
integer, public :: num2
contains
procedure :: write_b
procedure :: read_b
generic :: write(unformatted) => write_b
generic :: read(unformatted) => read_b
end type b
type, extends(a) :: c
end type c
contains
subroutine write_a(this, unit, iostat, iomsg)
class(a), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
write(unit, iostat=iostat, iomsg=iomsg) this%num
end subroutine write_a
subroutine read_a(this, unit, iostat, iomsg)
class(a), intent(inout) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
read(unit, iostat=iostat, iomsg=iomsg) this%num
end subroutine read_a
subroutine write_b(this, unit, iostat, iomsg)
class(b), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
write(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
end subroutine write_b
subroutine read_b(this, unit, iostat, iomsg)
class(b), intent(inout) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
read(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
end subroutine read_b
end module m
program mwe
use m
implicit none
class(a), allocatable :: o1, o2, o3
o1 = b(1,2)
o2 = c(3)
open(123, file='test5.dat', form='unformatted')
write(123) o1
close(123)
allocate(b :: o3)
open(123, file='test5.dat', form='unformatted')
read(123) o3
close(123)
write(*,*) o3%num, o3%num2
end program mwe
但我收到以下错误:
test5.f90(29): error #8638: The type/rank signature for arguments of this specific subroutine is identical to another specific subroutine that shares the same defined I/O. [WRITE_A]
subroutine write_a(this, unit, iostat, iomsg)
---------------^
test5.f90(86): error #6460: This is not a field name that is defined in the encompassing structure. [NUM2]
write(*,*) o3%num, o3%num2
--------------------------^
在我看来,就像类中的write
方法a
不能被覆盖一样。我怎样才能正确实施它?
解决方案
这不是完全与定义的输入/输出过程有关的问题,而是更广泛的通用绑定和类型绑定过程。
您的类型b
具有类型绑定过程,就好像(通过扩展)它被定义为
type b
integer, public :: num, num2
contains
procedure :: write_a, write_b
procedure :: read_a, read_b
generic :: write(unformatted) => write_a, write_b
generic :: read(unformatted) => read_a, read_n
end type
绑定write_a
和write_b
确实是模棱两可的(就像read_a
和一样read_b
)。[其他地方的更多细节。]
你并不真的需要这些write_a
和read_a
绑定,所以它们应该被覆盖:
type, abstract :: a
integer, public :: num
contains
procedure :: write => write_a
procedure :: read => read_a
generic :: write(unformatted) => write
generic :: read(unformatted) => read
end type a
type, extends(a) :: b
integer, public :: num2
contains
procedure :: write => write_b
procedure :: read => read_b
end type b
这里 type 的write
andread
绑定b
覆盖了 type的绑定a
。的通用绑定write(unformatted)
并read(unformatted)
保留到b
.
推荐阅读
- tcl - TCL 中多个 ::math::linearalgebra::mkMatrices 的容器
- mysql - 无法在ubuntu中启动mysql
- apex - 无法在 Windows 上安装 apex
- symfony - Redis 连接(在 Doctrine 缓存中)在每个命令上都被初始化
- python - Python - Tkinter Frame 的 XY 滚动无法有效加载小部件
- reactjs - 如何在 reactjs 中做一个 READme 样式的标记?
- javascript - 如何仅在 javascript 中获得像 yyyy-mm-dd hh:mm:ss 这样的中央时间
- forms - 如果键盘类型发生变化,Flutter TextField 焦点会重建小部件
- javascript - 使用 Google Earth Engine 将 Landsat Image Collection 缩减为长格式列表时缺少 NDVI 值
- android - SQL Cipher android studio中的“没有这样的表”