F 的建構函式

2022-02-06 15:46:50 字數 3174 閱讀 7996

f#的建構函式

f#是.net中的函式程式語言,雖然,為了跟其它.net語言有更好的協作關係,f#本身也有相當的物件導向的功能,但是並不完整,有一些功能限制。譬如,不支援「protected」訪問控制。再如,或算是一種特色,多建構函式中,必須其中乙個為主建構函式,而其他建構函式只能呼叫主建構函式,這些建構函式稱為「附加建構函式」。主建構函式必須寫在最上方,微軟建議按如下格式:

// this class has a primary constructor that takes three arguments

// and an additional constructor that calls the primary constructor.

type myclass(x0, y0, z0) =

letmutable x = x0 

letmutable y = y0 

letmutable z = z0 

doprintfn "initialized object that has coordinates (%d, %d, %d)" x y z 

member

this.x with get() = x and set(value) = x <- value 

member

this.y with get() = y and set(value) = y <- value 

member

this.z with get() = z and set(value) = z <- value 

new() = myclass(0, 0, 0)// create by using the new keyword.

let myobject1 = new myclass(1, 2, 3)

實際上,還有另外一種寫法:

type 

myclass

=val

mutable

x : 

intval mutable

y : 

intval mutable

z : 

intnew

(x0, y0, z0) =

member

this.x

with

get () = this.x

andset (value) = this.x <- value

member

this.y 

with

get () = this.y

andset (value) = this.y <- value

member

this.z

with

get () = this.z

andset (value) = this.z <- value

new() = 

myclass

(0, 0, 0)

let myobject1 = new myclass(1, 2, 3)

第二種的寫法,只能在建構函式裡面做賦值,而像第一種那樣,支援let繫結和do繫結等等事情。微軟建議採用第一種的寫法,因為這樣功能比較強大一點,甚至,在msdn《建構函式(f#)》裡面都不寫明有下面的這種方法,只是在結構的建構函式裡採用類似寫法。不過,在另一篇《顯式字段:val 關鍵字 (f#)》裡則有提到。那麼,為什麼要提下面的那種方法呢?因為上面第一種方法有個缺陷,主建構函式沒辦法支援xml文件注釋。

xml文件注釋一般都寫在**的上一行,但是在type上一行,已經作為類的注釋,所以已經地方標識主建構函式。

這個是微軟真忘記了,解決問題可以,但是有點醜陋,就是在原來的主建構函式裡面新增乙個空的引數,而把原來的建構函式寫成乙個附加函式,呼叫新的建構函式,附加建構函式是可以注釋的:

type myclass(x0, y0, z0, u:unit

) =let

mutable x = x0 

letmutable y = y0 

let mutable z = z0 

doprintfn "initialized object that has coordinates (%d, %d, %d)" x y z 

member

this.x with get() = x and set(value) = x <- value 

member

this.y with get() = y and set(value) = y <- value 

member

this.z with get() = z and set(value) = z <- value 

/// 注釋加在這裡

/// 引數1

/// 引數2

/// 引數3

new(x0, y0, z0

) = 

myclass(x0, y0, z0, ()

) new

() = myclass(0, 0, 0)

另外,為了呼叫者的美觀,我們還要把主建構函式隱藏起來,msdn沒介紹如何對這種建構函式做訪問控制,private要加在類名和左括號之間,我搞了好久才知道,一般人我不告訴他:

type myclass private (x0, y0, z0, u:unit

) =let

mutable x = x0 

letmutable y = y0 

letmutable z = z0 

doprintfn "initialized object that has coordinates (%d, %d, %d)" x y z 

member

this.x with get() = x and set(value) = x <- value 

member

this.y with get() = y and set(value) = y <- value 

member

this.z with get() = z and set(value) = z <- value 

/// 注釋加在這裡

/// 引數1

/// 引數2

/// 引數3

new(x0, y0, z0) = myclass(x0, y0, z0, ()) 

new() = myclass(0, 0, 0)

這裡加上私有訪問控制,問題才算完美解決。

F 程式構造

大多語言比如c 需要乙個明確的入口點,這就是最常見的main函式。但是我們的f 程式目前為止還沒有乙個特別明確的標記來說明程式要從這兒開始。在f 裡,對乙個個單一的檔案程式來說 fs 檔案裡的內容自上而下已經可以執行 不需要定義乙個明確的main方法 然而對多檔案的工程來說,需要被分成乙個個叫做mo...

建構函式 拷貝建構函式 賦值建構函式

class和struct很大乙個區別在於,c除了成員的訪問控制許可權,struct中的成員預設是public,而class中的field預設是private class還有乙個special的地方是它有建構函式 constructor。建構函式是class中的一員,和成員變數 其他成員函式一起構成乙...

建構函式 解析建構函式的作用

建構函式 是一種特殊的方法。主要用來在建立物件時初始化物件,即為物件 成員變數 賦初始值,總與new 運算子一起使用在建立物件的語句中。特別的乙個類可以有多個建構函式 可根據其引數個數的不同或引數型別的不同來區分它們 即建構函式的 過載。那麼,為什麼要在建立物件時初始化物件呢?又是如何實現的呢?接下...