具有一个实现通用类型为“ This”类的接口的类
是否有一种无需强制转换的方法?
简单代码:
interface Triggerable<This: Triggerable<This>> {
var trigger: (This) -> Unit
fun triggerNow() = trigger(this as This)
}
class Test : Triggerable<Test>{
override var trigger: (Test) -> Unit = { /*...*/ }
}
有点复杂:
interface TriggerInterface<T> {
val trigger: (T) -> Unit
fun triggerNow()
}
interface Triggerable<T: Triggerable<T>>: TriggerInterface<T> {
override fun triggerNow() = trigger(this as T)
}
interface Signalable<T>: TriggerInterface<T> {
var value: T
override fun triggerNow() = trigger(value)
}
class Test : Triggerable<Test>{
override val trigger: (Test) -> Unit = { /*...*/ }
}
答案 0 :(得分:3)
应该可能是这样
from pyspark.sql.window import Window
from pyspark.sql import functions as F
from pyspark.sql.functions import when
w= Window().partitionBy("I_id","p_id").orderBy(F.col("xyz").asc_nulls_first())
w2= Window().partitionBy("I_id","p_id")
df.withColumn("xyz1",F.count(F.col("xyz").isNotNull()).over(w))\
.withColumn("xyz2", F.max(F.row_number().over(w)).over(w2))\
.withColumn("xyz3", F.first("xyz1").over(w))\
.withColumn("xyz10", F.col("xyz2")-F.col("xyz3"))\
.withColumn("xyz9", F.when((F.col("xyz2")-F.col("xyz3"))%2!=0, F.col("xyz2")-F.col("xyz3")+1).otherwise(F.col("xyz2")-F.col("xyz3")))\
.withColumn("xyz4", (F.col("xyz9")/2))\
.withColumn("xyz6", F.col("xyz4")+F.col("xyz3"))\
.withColumn("xyz7", F.when(F.col("xyz10")%2==0,(F.col("xyz4")+F.col("xyz3")+1)).otherwise(F.lit(None)))\
.withColumn("xyz5", F.row_number().over(w))\
.withColumn("medianr", F.when(F.col("xyz6")==F.col("xyz5"), F.col("xyz")).when(F.col("xyz7")==F.col("xyz5"),F.col("xyz")).otherwise(F.lit(None)))\
.withColumn("medianr2", (F.mean("medianr").over(w2)))\
.withColumn("xyz", F.when(F.col("xyz").isNull(), F.col("medianr2")).otherwise(F.col("xyz")))\
.select("I_id","p_id","xyz")\
.orderBy("I_id").show()
+----+----+---+
|I_id|p_id|xyz|
+----+----+---+
| 1| 5| 4|
| 1| 5| 1|
| 1| 5| 4|
| 1| 5| 4|
| 2| 5| 2|
| 2| 5| 2|
| 2| 5| 1|
| 2| 5| 2|
| 2| 5| 4|
+----+----+---+
选中http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206
我个人也建议重新考虑是否确实需要TriggerInterface和Triggerable(其中一个继承自另一个)。
答案 1 :(得分:0)
调用override fun triggerNow() = trigger(this as T)
就是要将Triggerable<T>
强制转换为T
,这就是编译器警告您有关未经检查的强制转换的原因
拥有
val trigger: (TriggerInterface<T>) -> Unit
将允许您在不强制转换的情况下调用触发器
override fun triggerNow() = trigger(this)
实际执行
override val trigger: (Test) -> Unit = { /*...*/ }
您需要Test
的实例才能在尚未在代码中任何位置声明的触发器内传递