本文介绍了在Fortran中,在扩展定义中时,如何将公共过程设置为私有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我首先定义了一个类型A,其中定义了一个公共过程f,也可以将其绑定到A.在另一个模块中,我将此类型扩展为B.但是,当我使用类型B时,我不想f被暴露.顺便说一句,我不想​​使用submod技术.

Assume I first defined a type A in which a public procedure f is defined, and may also be bonded to A. In another module I have this type extended into B. However, when I use type B, I do not want f to be exposed.By the way, I don't want to use the submod technique.

补语:

假设类型(A)已定义:

Assume type(A) is already defined:

module mA
type::A
 ...
 contains
 procedure::f
endtype
endmodule

在另一个模块B中,我们将A扩展为:

In another module B, we extend A as:

module mB
use mA
type,extends(A)::B
 ...
endtype
endmodule

在此模块中,仍可以使用f.但是,接下来,在模块mC中,我将使用(declare)

In this module, f may still be used. However, next, in module mC I will use(declare)

type(B)::Ob

,我希望调用Ob%f()"是非法的.或者等效地讲,我想在扩展类时禁止某些功能.

and I wish "call Ob%f()" to be illegal. Or equivalently speaking, I want to ban some of the function when I extend a class.

推荐答案

很难理解您的描述,但是如果我理解正确,那是不可能的.

It is hard to understand your descriiption, but if I understand it correctly it is not possible.

请考虑您有一个变量class(A) :: o.您可以打电话

Consider you have a variable class(A) :: o. You are allowed to call

call o%f()

class(A)是多态的,可以是A的任何扩展类型,因此其动态类型可以是type(B).因此,B必须提供可公开访问的过程f以便与父代保持兼容.

class(A) is polymorphic and can be any extended type of A so its dynamic type can be type(B). So B MUST provide publicly accessible procedure f to stay compatible with the parent.

这篇关于在Fortran中,在扩展定义中时,如何将公共过程设置为私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:04