自定义类型基本练习

This commit is contained in:
Sanchime 2022-02-20 21:33:07 +08:00
parent 2e9d5613c5
commit 6665adb75b
2 changed files with 88 additions and 0 deletions

View File

@ -14,5 +14,6 @@
<Compile Include="FunctionSign.fs" />
<Compile Include="CurryingFunction.fs" />
<Compile Include="Loop.fs" />
<Compile Include="TypeCreating.fs" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,87 @@
module TypeCreating
//
type List =
| Cons of int * List
| Nil
let list = Cons(1, Cons(2, Cons(3, Nil)))
printfn "%A" list
//
type UserInfo = {
Username: string
Password: string
}
let user = { Username = "小明"; Password = "123456" }
printfn "%A" user
//
// val
// member
type UserInfoBtStruct = struct
val Username: string
val Password: string
new(username: string, password: string) = { Username = username; Password = password }
member this.Display() =
printfn $"用户名: {this.Username} 密码: {this.Password}"
member this.ToString() = this.Display()
end
//
//
// let,
// 使,
// do
// 使,F#letdo
type UserInfoByClass(username: string, password: string) =
let _username = username
let _password = password
do printfn "%A" "DoBinding"
member this.Username = _username
member this.Password = _password
member this.Display() =
printfn $"用户名: {this.Username} 密码: {this.Password}"
member this.ToString() = this.Display()
//
// ,,abstract
// 访public
type IUserInfo =
abstract member Display: unit -> unit
//
// 使as,this
type UserInfoWith() =
interface IUserInfo with
member this.Display() = printfn "IUserInfoDisplay"
//
// type class-name(...)
// inherit base-class-name(...)
// other-member...
//
type BaseClass() =
let mutable counter = 0
//
abstract member Method: int -> int
//
default this.Method(a) = counter <- counter + a; counter
type SubClass() =
inherit BaseClass()
//
override this.Method(a) = a + 1
//
// [<AbstactClass>]
// [<AbstactClass>]
// type AbstactClass(...) = ...