如何在Fantom中建模GeoJSON几何体?
我从几何的基本抽象类开始:如何在Fantom中建模GeoJSON几何体?,fantom,afbedsheet,Fantom,Afbedsheet,我从几何的基本抽象类开始: @Serializable abstract const class Geometry { const Str type new make( Str type, |This| f ) { this.type = type f(this) } } 然后我扩展了这个抽象类来建模一个点: const class GeoPoint : Geometry { const Decimal[] coordina
@Serializable
abstract const class Geometry {
const Str type
new make( Str type, |This| f ) {
this.type = type
f(this)
}
}
然后我扩展了这个抽象类来建模一个点:
const class GeoPoint : Geometry {
const Decimal[] coordinates
new make( |This| f ) : super( "Point", f ) { }
Decimal? longitude() { coordinates.size >= 1 ? coordinates[ 0 ] : null }
Decimal? latitude() { coordinates.size >= 2 ? coordinates[ 1 ] : null }
Decimal? altitude() { coordinates.size >= 3 ? coordinates[ 2 ] : null }
}
这编译正常,在一个简单的场景中运行良好,但如果我尝试通过IoC使用,我会收到以下错误消息:
[err] [afBedSheet] afIoc::IocErr - Field myPod::GeoPoint.coordinates was not set by ctor sys::Void make(|sys::This->sys::Void| f)
Causes:
afIoc::IocErr - Field myPod::GeoPoint.coordinates was not set by ctor sys::Void make(|sys::This->sys::Void| f)
sys::FieldNotSetErr - myPod::GeoPoint.coordinates
IoC Operation Trace:
[ 1] Autobuilding 'GeoPoint'
[ 2] Creating 'GeoPoint' via ctor autobuild
[ 3] Instantiating myPod::GeoPoint via Void make(|This->Void| f)...
Stack Trace:
afIoc::IocErr : Field myPod::GeoPoint.coordinates was not set by ctor sys::Void make(|sys::This->sys::Void| f)
我认为这是因为构造函数有另一个参数,除了
| This | f
。是否有更好的方法来编写地质和地质点类,请?对于序列化,您需要一个名为make()
的it块ctor。但这并不妨碍您定义自己的CTOR。我会将这两个部门分开,作为分离关注点的一种手段
对于简单的数据类型,我通常会将字段值作为参数传递
这将使对象看起来像这样:
@Serializable
抽象常量类几何{
常量Str类型
//连载
新品牌(|此| f){
f(本)
}
新makeWithType(Str类型){
this.type=type
}
}
@可序列化
常量类地质点:几何学{
常量十进制[]坐标
//连载
新make(| This | f):super.make(f){
新的makeWithCoors(十进制[]坐标):super.makeWithType(“点”){
this.coordinates=坐标
}
}
Fantom Serialization将使用make()
ctor,您可以使用makeWithCoors()
ctor-如下所示:
point1:=地质点([1d,2d])//或
点2:=GeoPoint.makeWithCoors([1d,2d])
请注意,您不必将ctor命名为Fantom,因为Fantom将从参数中计算出来
还请注意,您自己的ctor可以命名为任何名称,但按照惯例,它们以makeXXXX()
开头
然后,要使用自动生成地质点
,请执行以下操作:
point:=(地质点)注册表。自动生成(地质点#,[[1d,2d]]
然后将使用makeWithCoors()
ctor