2013-10-09 56 views
2

在下面的一段代码中,具有私有变量(名称)和该变量的访问函数的抽象类型(应该由所有派生类型定义)在模块:在不同模块中抽象类型的扩展

module baseTypeModule 

type, abstract :: baseType 
private 
    character(len=maxLengthCompLabel) :: Name = ""   ! Component name 
contains 
    procedure, non_overridable :: getName     ! Access functio to Name (read only) 
end type baseType 

contains 

character(len=100) function getName(this) 
    implicit none 
    class(baseType), intent(in) :: this 
    getName = this % Name 
end function getName 

end module baseTypeModule 

由于有很多其他的变量和函数在每个派生类型,我想在不同的模块来定义每个派生类型。

Fortran中是否有方法告诉编译器,我希望只有派生类型的baseType才能够更改变量Name?

回答

2

编号组件名称的可访问性使用与其他模块实体相同的“按模块”模型。如果其他派生类型位于不同模块中,则它们不能访问组件。

请记住,派生类型实际上并不包含过程 - 它们包含过程的绑定。因此派生类型不能真正“做”任何事情。也可以将一个过程绑定到多种类型。