2017-05-29 5 views
1

So haben statische Methoden in anderen Sprachen Zugriff auf statische Member und ihre Sichtbarkeit wird durch den Klassenbereich eingeschränkt. In fortran gibt es keine statischen Mitglieder (korrigiere mich, wenn ich falsch liege) und der Methodenname ist global zugänglich, so dass ich nicht zwei statische Methoden mit demselben Namen in verschiedenen Klassen haben kann. Ich halte "NOPASS" -Methoden für "statisch", aber ich bin mir nicht einmal sicher, ob der Begriff anwendbar ist. Angesichts des oben Gesagten sehe ich keinen Unterschied von nur einfachen Modulfunktionen. Gibt es einen Vorteil der Verwendung von NOPASS-Methoden gegenüber einfachen Funktionen?Prozedur nopass vs. Plain-Funktion in Fortran

Edit:

Eine Abbildung der Unfähigkeit, in verschiedenen Klassen zwei statische Methoden mit dem gleichen Namen zu haben:

module test_mod 
    type type1 
    contains 
    procedure, nopass :: proc => proc1 
    end type 

    type type2 
    contains 
    procedure, nopass :: proc => proc2 
    end type 

contains 
    subroutine proc1() 
    print *, 'proc1' 
    end subroutine 

    subroutine proc2() 
    print *, 'proc2' 
    end subroutine 
end module 

Offensichtlich kann ich nicht nur call proc() jetzt sagen, noch kann ich die Klassenname, um dem Compiler zu helfen, die richtige Methode auszuwählen.

+0

Was meinen Sie nicht dasselbe ist "Ich kann nicht zwei statische Methoden mit demselben Namen in verschiedenen Klassen haben"? – francescalus

+0

@francescalus Ich habe meine Frage – DartLenin

+2

aktualisiert Sie können "proc" in keinem Fall aufrufen. 'call t1% proc' andererseits (für' type (type1) t1') ... – francescalus

Antwort

2

Berücksichtigen Sie die Barrierefreiheit: Eine öffentliche typgebundene Prozedur ist immer verfügbar, wenn auf den Typ zugegriffen werden kann.

module mod 
    private 
    type, public :: type1 
    contains 
    procedure, nopass :: proc 
    end type 

contains 

    subroutine proc 
    end subroutine 

end module 

    use mod 
    type(type1) t1 

    call t1%proc 
    call proc ! No, we can't call proc from mod1 

end 

In ähnlicher Weise mit use <module>, only <...>

module mod1 
    type type1 
    contains 
    procedure, nopass :: proc 
    end type 

contains 

    subroutine proc 
    end subroutine 

end module 

module mod2 
    type type2 
    contains 
    procedure, nopass :: proc 
    end type 

contains 

    subroutine proc 
    end subroutine 

end module 

Wir Konflikte in den Modulprozedurnamen ohne Umbenennung auf Verwendung Verein vermeiden:

stattdessen würden wir
use mod1, only : type1 
    use mod2, only : type2 

    type(type1) t1 
    type(type2) t2 

    call t1%proc 
    call t2%proc ! These aren't ambiguous 

end 

Zur Vermeidung von Unklarheiten über die Verfahren müssen umbenennen:

use mod1, proc1=>proc 
    use mod2, proc2=>proc 

    call proc1 
    call proc2 

end 

Es gibt auch dynamische Auswahl des Verfahrens Referenz:

module mod 
    type type1 
    contains 
    procedure, nopass :: proc=>proc1 
    end type 

    type, extends(type1) :: type2 
    contains 
    procedure, nopass :: proc=>proc2 
    end type 

contains 

    subroutine proc1 
    end subroutine 

    subroutine proc2 
    end subroutine 

end module 

use mod 
class(type1), allocatable :: t 

t=type1() 
call t%proc ! proc1 

t=type2() 
call t%proc ! proc2 

end 

Es sollte jedoch beachtet werden, dass ein verbindlicher Namen wie t1%proc als Prozedurname von

use mod 
type(type1) t1 
call sub(proc1) ! Where this is accessible 
call sub(t1%proc) ! We cannot do this 

contains 

subroutine sub(proc) 
    procedure() proc 
end subroutine 

end