POJO 和 Scala
POJO's in Scala
我在 Java
中有以下 2 类
public class DataPoint<T> {
public long timeStampMs;
public T value;
public DataPoint() {}
public DataPoint(long timeStampMs, T value) {
this.timeStampMs = timeStampMs;
this.value = value;
}
public <R> DataPoint<R> withNewValue(R newValue){
return new DataPoint(this.timeStampMs, newValue);
}
public KeyedDataPoint withKey(String key){
return new KeyedDataPoint(key, this.timeStampMs, this.value);
}
}
和
public class KeyedDataPoint<T> extends DataPoint<T>{
public String key;
public KeyedDataPoint() {}
public KeyedDataPoint(String key, long timeStampMs, T value) {
super(timeStampMs, value);
this.key = key;
}
public <R> KeyedDataPoint<R> withNewValue(R newValue){
return new KeyedDataPoint(this.key, this.timeStampMs, newValue);
}
}
我需要将其翻译成 Scala:
class DataPoint[T] (@BeanProperty val timeStampMs: Long, @BeanProperty val value: T) {
def withNewValue[R](value: R): DataPoint[R] = new DataPoint[R](timeStampMs, value)
def withKey(key: String): KeyedDataPoint[T] = new KeyedDataPoint(key, timeStampMs, value)
}
class KeyedDataPoint[T](@BeanProperty val key: String,
@BeanProperty override val timeStampMs: Long,
@BeanProperty override val value: T) extends DataPoint (timeStampMs, value) {
}
类 需要符合 POJO 的条件,但我不知道如何获得它。如果我提供一个无参数的主构造函数,那么我不知道如何提供多参数辅助构造函数,反之亦然......如果我将主构造函数留给几个参数,那么我不知道如何提供无参数构造函数要求符合 POJO。有帮助吗?
class KeyedDataPoint[T](@BeanProperty val key: String,
@BeanProperty override val timeStampMs: Long,
@BeanProperty override val value: Option[T]) extends DataPoint (timeStampMs, value) {
}
class DataPoint[T] (@BeanProperty val timeStampMs: Long, @BeanProperty val value: Option[T]) {
def this() {
this(0, None);
}
def withNewValue[R](value: Option[R]): DataPoint[R] = new DataPoint(timeStampMs, value)
def withKey(key: String): KeyedDataPoint[T] = new KeyedDataPoint(key, timeStampMs, value)
}
class Foo(s: String) {
// alternative no-arg constructor (with different signature from primary)
def this() {
this("Default value from auxiliary constructor")
}
}
请参考here
我在 Java
中有以下 2 类public class DataPoint<T> {
public long timeStampMs;
public T value;
public DataPoint() {}
public DataPoint(long timeStampMs, T value) {
this.timeStampMs = timeStampMs;
this.value = value;
}
public <R> DataPoint<R> withNewValue(R newValue){
return new DataPoint(this.timeStampMs, newValue);
}
public KeyedDataPoint withKey(String key){
return new KeyedDataPoint(key, this.timeStampMs, this.value);
}
}
和
public class KeyedDataPoint<T> extends DataPoint<T>{
public String key;
public KeyedDataPoint() {}
public KeyedDataPoint(String key, long timeStampMs, T value) {
super(timeStampMs, value);
this.key = key;
}
public <R> KeyedDataPoint<R> withNewValue(R newValue){
return new KeyedDataPoint(this.key, this.timeStampMs, newValue);
}
}
我需要将其翻译成 Scala:
class DataPoint[T] (@BeanProperty val timeStampMs: Long, @BeanProperty val value: T) {
def withNewValue[R](value: R): DataPoint[R] = new DataPoint[R](timeStampMs, value)
def withKey(key: String): KeyedDataPoint[T] = new KeyedDataPoint(key, timeStampMs, value)
}
class KeyedDataPoint[T](@BeanProperty val key: String,
@BeanProperty override val timeStampMs: Long,
@BeanProperty override val value: T) extends DataPoint (timeStampMs, value) {
}
类 需要符合 POJO 的条件,但我不知道如何获得它。如果我提供一个无参数的主构造函数,那么我不知道如何提供多参数辅助构造函数,反之亦然......如果我将主构造函数留给几个参数,那么我不知道如何提供无参数构造函数要求符合 POJO。有帮助吗?
class KeyedDataPoint[T](@BeanProperty val key: String,
@BeanProperty override val timeStampMs: Long,
@BeanProperty override val value: Option[T]) extends DataPoint (timeStampMs, value) {
}
class DataPoint[T] (@BeanProperty val timeStampMs: Long, @BeanProperty val value: Option[T]) {
def this() {
this(0, None);
}
def withNewValue[R](value: Option[R]): DataPoint[R] = new DataPoint(timeStampMs, value)
def withKey(key: String): KeyedDataPoint[T] = new KeyedDataPoint(key, timeStampMs, value)
}
class Foo(s: String) {
// alternative no-arg constructor (with different signature from primary)
def this() {
this("Default value from auxiliary constructor")
}
}
请参考here