Sto provando a creare una piccola porzione di codice in F #. L'idea è che avrei un'interfaccia che definisce alcuni metodi, proprietà ed eventi. Usando questa interfaccia voglio definire una classe astratta (con un tipo generico) che implementerebbe alcune proprietà/metodi ma non tutti i membri definiti nell'interfaccia. La responsabilità di definire tutti i membri sarebbe nelle classi derivate.Interfacce e classi astratte in F #
Finora sono bloccato con l'interfaccia e la classe astratta. I problemi che sto avendo:
- La definizione della classe astratta non riconosce la definizione di interfaccia
- Non sono sicuro di come definire i membri astratti per l'interfaccia nella classe astratta
L'interfaccia si compila bene ma la classe astratta ha diversi errori. I messaggi sono inclusi nel codice
Tutti i commenti, le correzioni, i suggerimenti, ecc. Sono apprezzati. Poiché questa è la mia prima prova su F # ci sono probabilmente molti errori da segnalare.
Il codice che ho finora
definizione dell'interfaccia
namespace A
type IValueItem =
interface
[<CLIEvent>]
abstract member PropertyChanged :
Control.IEvent<System.ComponentModel.PropertyChangedEventHandler,
System.ComponentModel.PropertyChangedEventArgs>
abstract ConvertedX : double with get, set
abstract Y : double with get, set
abstract member CreateCopy : obj
abstract member NewTrendItem : obj
end
E la classe astratta
namespace A
[<AbstractClass>]
type ValueItem<'TX>() =
[<DefaultValue>]
val mutable _y : double
let _propertyChanged = new Event<_>()
// ERROR: This type is not an interface type
// ERROR: The type 'obj' is not an interface type
// ERROR: The type 'IValueItem' is not defined
interface IValueItem with
// ERRROR No abstract or interface member was found that corresponds to this override
[<CLIEvent>]
member this.PropertyChanged :
Control.IEvent<System.ComponentModel.PropertyChangedEventHandler,
System.ComponentModel.PropertyChangedEventArgs>
= _propertyChanged.Publish
// This definition is incomplete, should be abstract
member ConvertedX : double with get, set
// ERROR: Incomplete structured construct at or before this point in pattern
member CreateCopy() : obj
member NewTrendItem() : obj
abstract X : 'TX with get, set
member this.Y
with get() = this._y
and set(value) =
if this._y <> value then
this._y <- value
this.NotifyPropertyChanged("Y")
member this.NotifyPropertyChanged(propertyName) =
this.PropertyChanged.Trigger(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName))
Grazie per l'aiuto. Sono ancora solo bloccato dall'ereditarietà e dall'override dei metodi. La classe astratta ora definisce 'member this.NewTrendItem() = null' e la classe derivata assomiglia a' type NumberItem() = inherit ValueItem() ...... override this.NewTrendItem() = new NumberItem () '. Tuttavia, sto ricevendo _Non è stato trovato alcun elemento astratto o interfaccia corrispondente a questo errore di override_. –
user6423491
Nessun problema, spero che stia aiutando. Ho aggiornato il commento per fornire un esempio di come potresti farlo. –