首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在扇汤姆中建模GeoJSON几何?

我如何在扇汤姆中建模GeoJSON几何?
EN

Stack Overflow用户
提问于 2015-12-18 14:44:48
回答 1查看 30关注 0票数 1

我从几何的基本抽象类开始:

代码语言:javascript
复制
@Serializable
abstract const class Geometry {

    const Str type

    new make( Str type, |This| f ) {
        this.type = type
        f(this)
    }
}

然后,我扩展了这个抽象类来建模一个Point:

代码语言:javascript
复制
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 } 
}

这可以编译Ok,在一个简单的场景中可以正常工作,但是如果我尝试通过IoC使用,我会得到以下错误消息:

代码语言:javascript
复制
[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之外还有另一个参数。请问有没有更好的方法来编写地质学和GeoPoint类?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-18 15:59:03

对于序列化,您需要一个名为make()的it块ctor。但这并不能阻止你定义你自己的角色。我会把这两位主持人分开,作为分离关切的一种手段。

对于一个简单的数据类型,我通常会将字段值作为ctor参数传递。

这将使对象看起来像这样:

代码语言:javascript
复制
@Serializable
abstract const class Geometry {
    const Str type

    // for serialisation
    new make(|This| f) {
        f(this)
    }

    new makeWithType(Str type) {
        this.type = type
    }
}

@Serializable
const class GeoPoint : Geometry {
    const Decimal[] coordinates

    // for serialisation
    new make(|This| f) : super.make(f) { }

    new makeWithCoors(Decimal[] coordinates) : super.makeWithType("Point") {
        this.coordinates = coordinates
    }
} 

扇汤姆序列化将使用make() ctor,您可以使用makeWithCoors() ctor -如下所示:

代码语言:javascript
复制
point1 := GeoPoint([1d, 2d])  // or
point2 := GeoPoint.makeWithCoors([1d, 2d])

请注意,您不必将ctor命名为扇汤姆,它将从参数中计算出来。

还请注意,您自己的ctors可能被命名为任何东西,但按照惯例,它们以makeXXXX()开头。

要使用GeoPoint自动构建IoC,请执行以下操作:

代码语言:javascript
复制
point := (GeoPoint) registry.autobuild(GeoPoint#, [[1d, 2d]])

然后使用makeWithCoors() ctor。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34358121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档