我在F#中尝试OOP并遇到了问题。我正在尝试构建一个无人机和空域类,但由于某种原因我的printDrones功能不起作用,所以我无法检查其余部分是否有效。我没有得到任何错误,但该功能只是没有打印任何东西。我想我的类型或列表函数可能有些问题,但我已经尝试了很多东西,现在我要放弃了。希望有人可以帮助我!
type Drone(startposition: (float * float), destination: (float * float), speed: float) =
let mutable position = startposition
let a = fst destination - fst position
let b = snd destination - snd position
let c = sqrt(a**2.0 + b**2.0)
let speeddir = (a/c, b/c)
member this.fly =
position <- (fst position + (fst speeddir * speed), snd position + (snd speeddir * speed))
member this.getPos = position
member this.isFinished =
(fst startposition - fst destination)*(fst position - fst destination) < 0.0
type Airspace() =
let mutable drones: Drone List = []
member this.droneDist (d1: Drone) (d2: Drone) : float =
let d1pos = d1.getPos
let d2pos = d2.getPos
let a = fst d1pos - fst d2pos
let b = snd d1pos - snd d2pos
sqrt(a**2.0+ b**2.0)
member this.flyDrones() =
drones |> List.iter (fun x -> x.fly)
member this.addDrone(dr: Drone) =
drones <- dr :: drones
member this.printDrones() =
drones |> List.iter (fun x -> printfn "%A" (x.getPos))
let aspace = Airspace()
aspace.addDrone(Drone((50.0,100.0),(0.0,0.0), 20.0))
aspace.addDrone(Drone((0.0,100.0),(0.0,0.0), 20.0))
aspace.addDrone(Drone((25.0,100.0),(0.0,0.0), 20.0))
aspace.addDrone(Drone((75.0,100.0),(0.0,0.0), 20.0))
aspace.flyDrones
aspace.printDrones