|
Il presente articolo è stato tradotto manualmente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale. Ulteriori informazioni.
|
Traduzione
Originale
|
Proprietà (F#)
// Property that has both get and set defined.
[ attributes ]
[ static ] member [accessibility-modifier] [self-identifier.]PropertyName
with [accessibility-modifier] get() =
get-function-body
and [accessibility-modifier] set parameter =
set-function-body
// Alternative syntax for a property that has get and set.
[ attributes-for-get ]
[ static ] member [accessibility-modifier-for-get] [self-identifier.]PropertyName =
get-function-body
[ attributes-for-set ]
[ static ] member [accessibility-modifier-for-set] [self-identifier.]PropertyName
with set parameter =
set-function-body
// Property that has get only.
[ attributes ]
[ static ] member [accessibility-modifier] [self-identifier.]PropertyName =
get-function-body
// Alternative syntax for property that has get only.
[ attributes ]
[ static ] member [accessibility-modifier] [self-identifier.]PropertyName
with get() =
get-function-body
// Property that has set only.
[ attributes ]
[ static ] member [accessibility-modifier] [self-identifier.]PropertyName
with set parameter =
set-function-body
// Automatically implemented properties.
[attributes ]
[ static ] member val [accessibility-modifier ] PropertyName = initialization-expression [ with get, set ]
// A read-only property. member this.MyReadOnlyProperty = myInternalValue // A write-only property. member this.MyWriteOnlyProperty with set (value) = myInternalValue <- value // A read-write property. member this.MyReadWriteProperty with get () = myInternalValue and set (value) = myInternalValue <- value
member this.MyReadWriteProperty with get () = myInternalValue member this.MyReadWriteProperty with set (value) = myInternalValue <- value
type MyClass(property1 : int) = member val Property1 = property1 member val Property2 = "" with get, set
type MyClass() = let random = new System.Random() member val AutoProperty = random.Next() with get, set member this.ExplicitProperty = random.Next() let class1 = new MyClass() printfn "class1.AutoProperty = %d" class1.AutoProperty printfn "class1.AutoProperty = %d" class1.AutoProperty printfn "class1.ExplicitProperty = %d" class1.ExplicitProperty printfn "class1.ExplicitProperty = %d" class1.ExplicitProperty
Output
Class1.AutoProperty = 1853799794 class1.AutoProperty = 1853799794 class1.ExplicitProperty = 978922705 class1.ExplicitProperty = 1131210765
Attenzione |
|---|
static member MyStaticProperty with get() = myStaticValue and set(value) = myStaticValue <- value
// To apply a type annotation to a property that does not have an explicit // get or set, apply the type annotation directly to the property. member this.MyProperty1 : int = myInternalValue // If there is a get or set, apply the type annotation to the get or set method. member this.MyProperty2 with get() : int = myInternalValue
// Assume that the constructor argument sets the initial value of the // internal backing store. let mutable myObject = new MyType(10) myObject.MyProperty <- 20 printfn "%d" (myObject.MyProperty)
// Abstract property in abstract class. // The property is an int type that has a get and // set method [<AbstractClass>] type AbstractBase() = abstract Property1 : int with get, set // Implementation of the abstract property type Derived1() = inherit AbstractBase() let mutable value = 10 override this.Property1 with get() = value and set(v : int) = value <- v // A type with a "virtual" property. type Base1() = let mutable value = 10 abstract Property1 : int with get, set default this.Property1 with get() = value and set(v : int) = value <- v // A derived type that overrides the virtual property type Derived2() = inherit Base1() let mutable value2 = 11 override this.Property1 with get() = value2 and set(v) = value2 <- v
Attenzione