我有以下2个Java类
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的条件,但是我不知道如何获得它。如果我提供一个无参数的主要构造函数,那么我不知道如何提供多个args辅助构造函数,反之亦然...如果我将主要构造函数留给几个参数,那么我将不知道如何提供无参数构造函数要求符合POJO。有帮助吗?
答案 0 :(得分:3)
class Foo(s: String) {
// alternative no-arg constructor (with different signature from primary)
def this() {
this("Default value from auxiliary constructor")
}
}
请参考here
答案 1 :(得分:0)
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)
}