我从几何的基本抽象类开始:
@Serializable
abstract const class Geometry {
const Str type
new make( Str type, |This| f ) {
this.type = type
f(this)
}
}然后,我扩展了这个抽象类来建模一个Point:
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使用,我会得到以下错误消息:
[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类?
发布于 2015-12-18 15:59:03
对于序列化,您需要一个名为make()的it块ctor。但这并不能阻止你定义你自己的角色。我会把这两位主持人分开,作为分离关切的一种手段。
对于一个简单的数据类型,我通常会将字段值作为ctor参数传递。
这将使对象看起来像这样:
@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 -如下所示:
point1 := GeoPoint([1d, 2d]) // or
point2 := GeoPoint.makeWithCoors([1d, 2d])请注意,您不必将ctor命名为扇汤姆,它将从参数中计算出来。
还请注意,您自己的ctors可能被命名为任何东西,但按照惯例,它们以makeXXXX()开头。
要使用GeoPoint自动构建IoC,请执行以下操作:
point := (GeoPoint) registry.autobuild(GeoPoint#, [[1d, 2d]])然后使用makeWithCoors() ctor。
https://stackoverflow.com/questions/34358121
复制相似问题