Oop 初始化派生类型中的参数
我仍然沉浸在Fortran的“面向对象”风格中 是否可以初始化派生类型中的变量,如果希望该变量成为Oop 初始化派生类型中的参数,oop,fortran,Oop,Fortran,我仍然沉浸在Fortran的“面向对象”风格中 是否可以初始化派生类型中的变量,如果希望该变量成为参数,该怎么办 例如,典型的动物,猫,蜜蜂类型集应设置每种动物的腿数: 动物模块 module animal_module implicit none type, abstract :: animal private integer, public :: nlegs = -1 contains ... module cat_module
参数,该怎么办
例如,典型的动物
,猫
,蜜蜂
类型集应设置每种动物的腿数:
动物模块
module animal_module
implicit none
type, abstract :: animal
private
integer, public :: nlegs = -1
contains
...
module cat_module
use animal_module, only : animal
implicit none
type, extends(animal) :: cat
private
! error here about redefining nlegs...
integer, public :: nlegs = 4
...
cat模块
module animal_module
implicit none
type, abstract :: animal
private
integer, public :: nlegs = -1
contains
...
module cat_module
use animal_module, only : animal
implicit none
type, extends(animal) :: cat
private
! error here about redefining nlegs...
integer, public :: nlegs = 4
...
我在网上找到了关键字initial
,但我的编译器(英特尔)抱怨该关键字有语法错误
补遗
我尝试了一个,但显然我无法为派生类型编写一个。这是我的尝试,仅针对cat类型:
module cat_module
use animal_module, only : animal
implicit none
type, extends(animal) :: cat
private
real :: hidden = 23.
contains
procedure :: setlegs => setlegs
procedure :: speak
end type cat
interface cat
module procedure init_cat
end interface cat
contains
type(cat) function init_cat(this)
class(cat), intent(inout) :: this
this%nlegs = -4
end function init_cat
...
program oo
use animal_module
use cat_module
use bee_module
implicit none
character(len = 3) :: what = "cat"
class(animal), allocatable :: q
select case(what)
case("cat")
print *, "you will see a cat"
allocate(cat :: q)
...
print *, "this animal has ", q%legs(), " legs."
由于animal
类型具有integer,public::nlegs=-1
,因此我希望cat
具有-4
腿,但遗憾的是,它仍然是-1
,组件必须是命名常量。此外,不可能声明抽象类型的组件,然后在扩展类型中为其值定义默认初始化。尝试使用默认初始化重新声明组件会导致问题错误
High Performance Mark’s提供了一种途径:为每个扩展类型提供一个适当的值设置。您可以进一步将组件设置为private以进行封装或保护
或者,您可以提供引用命名常量的“getter”类型绑定过程:
module animal_module
implicit none
type, abstract :: animal
contains
procedure(getter), deferred :: nlegs
end type animal
abstract interface
integer function getter(creature)
import animal
class(animal) creature
end function getter
end interface
end module animal_module
module cat_module
use animal_module, only : animal
implicit none
type, extends(animal) :: cat
contains
procedure :: nlegs => nlegs_cat
end type cat
contains
integer function nlegs_cat(creature)
class(cat) creature
integer, parameter :: my_cat_has_legs=3
nlegs_cat = my_cat_has_legs
end function nlegs_cat
end module cat_module
use cat_module
implicit none
class(animal), allocatable :: fido
fido = cat()
print*, "Fido has", fido%nlegs(), "legs"
end
最后,initial
是(并且会有类似的问题)。问题是,cat
已经有了一个名为nlegs
的成员变量,因为它是animal
的扩展。所以编译器不会让你声明另一个同名的成员。我想你必须破解一个构造函数,它将猫的nlegs
设置为4
。你是想得到一个派生类型的(命名的)常量,还是想让一个对象中的一个组件成为常量?我想得到一个对所有扩展动物的类型都通用的字段,在本例中,支腿的数量。@HighPerformanceMark我同意,尽管我并不真正了解它们是如何工作的。除了显式引用之外,构造函数不会被调用:没有“分配时自动构造”。我尝试过自定义构造函数的方法,因为它似乎是最精巧的一种,但很明显,我在理解fortran中构造函数的工作原理时遗漏了一些东西。另一个显示构造函数尝试的问题可能是解决这个问题的最清晰的方法。你可以在这里链接到你的问题(提到收到的评论)作为背景。我已经按照你的建议。