简体   繁体   English

覆盖java / scala中的整个类

[英]Override whole class in java/scala

I have a library, already provided, which has two Scala classes: 我有一个已经提供的库,它有两个Scala类:

class EvaluationFunction (data:DataHolder) {
    def calculate(params:ParamHolder) = {...}
}

And

class Regression {
    def train(data:DataHolder) = {
        ...
        val costFunction = new EvaluationFunction(data)

        //find params to minimize costFunction.calculate(params)
        ....
    }
}

I want to make my own regression class, which behaves exactly like the original regression, but uses a different evaluation function. 我想创建自己的回归类,其行为与原始回归完全相同,但使用不同的评估函数。 However, the only way I can think of to do that would involve extending Regression and overwriting the whole train method. 但是,我能想到的唯一方法就是扩展回归并覆盖整个列车方法。 But it contains pretty much all the logic in the Regression class - I may as well write it from scratch! 但它包含了Regression类中的几乎所有逻辑 - 我也可以从头开始编写它!

Is there a better way? 有没有更好的办法? For example, somehow tell my extended regression class to invoke another class every time the original invokes EvaluationFunction? 例如,每当原始调用EvaluationFunction时,以某种方式告诉我的扩展回归类调用另一个类?

The code you show would not compile: the data does not have a datatype. 您显示的代码无法编译: data没有数据类型。

That aside - the structure of the Regression class is strange. 除此之外 - Regression类的结构很奇怪。 It should have the EvaluationFunction as overridable. 它应该具有可覆盖的EvaluationFunction As it stands it is built into the train method. 目前它已经内置到train方法中。 That is why you need to override (/rewrite) the entire class. 这就是你需要覆盖(/重写)整个类的原因。 It is a problem with the class structure. 这是类结构的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM