在构造包含类时设置类的属性

时间:2015-04-13 11:41:58

标签: f#

Test在构造时创建另一个类的实例并设置属性看起来像这样(我想):

type Test() as this =
    let a = new A()

    do this.Init()

    member this.Init() = 
        let a.Size = 10

但是,我得到Block following this 'let' is unfinished. Expect an expression.

这样做的正确和首选方式是什么?

1 个答案:

答案 0 :(得分:5)

如果你想改变Size属性,你必须使用赋值运算符:

type Test() as this =
    let a = new A()

    do this.Init()

    member this.Init() = 
        a.Size <- 10

但是,您可以更简洁地写出这样的内容:

type Test() =
    let a = A (Size = 10)