【发布时间】:2018-06-09 22:35:12
【问题描述】:
一般来说,抽象类型是后代派生类型的模型。如果类型中包含的过程被推迟,则该类型中的过程应该是 PUBLIC,因为 PRIVATE 不能被模块本身引用或覆盖。但是,如果我希望每个后代都有相同的功能 PRIVATE 过程来做某事(例如,测试它的实例的初始化状态),我该如何设计 ABSTRACT 派生类型?我是否必须在每个后代中手动定义它们,或者我可以将模型放在抽象中并在后代中实现它? 这是一个例子:
module test1
implicit none
private
public :: my_numeric_type
type, abstract :: my_numeric_type
contains
private
procedure(op2), deferred :: add
procedure(op2), deferred :: subtract
generic, public :: operator(+) => add
generic, public :: operator(-) => subtract
end type my_numeric_type
abstract interface
function op2(a, b) result(r)
import :: my_numeric_type
class(my_numeric_type), intent(in) :: a, b
class(my_numeric_type), allocatable :: r
end function op2
end interface
end module test1
module test2
use test1, only: my_numeric_type
implicit none
type, extends(my_numeric_type) :: my_integer
private
integer :: value
contains
private
procedure, public :: add => add_my_integer
procedure, public :: subtract => subtract_my_integer
end type my_integer
contains
function add_my_integer(a, b) result(r)
class(my_integer), intent(in) :: a
class(my_numeric_type), intent(in) :: b
class(my_numeric_type), allocatable :: r
! DO SOME ADDITION.
end function add_my_integer
function subtract_my_integer(a, b) result(r)
class(my_integer), intent(in) :: a
class(my_numeric_type), intent(in) :: b
class(my_numeric_type), allocatable :: r
! DO SOME SUBTRACTION.
end function subtract_my_integer
end module test2
在示例中,后代派生类型 my_integer 是非抽象类型并继承了延迟绑定,因此必须重写过程(加法和减法)。但是程序在my_numeric_type中定义为private,也就是说它们不能在模块test1之外被覆盖,所以上面的my_integer类型是无效的。我该怎么办?
【问题讨论】:
-
线程已更新。非常感谢,@Vladimir。
-
我觉得这和stackoverflow.com/questions/47959048/…有很多共同点,可以重复吗?
-
@VladimirF,我看不出这个问题和那个问题之间有什么联系。
-
@Vladimir,在您建议的线程中,两种派生类型中的类型绑定过程都定义为默认访问权限为 PUBLIC。但我遇到的问题是实现 PRIVATE 延迟类型绑定过程。
标签: oop fortran private abstract