0%

java:接口中的default方法

前言

  在书写moa.evaluation包下的接口文档时,发现java接口中并非所有的方法都是抽象方法。除了抽象方法之外,还有一种用default作为修饰符具体实现的方法。

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package moa.evaluation;

import com.yahoo.labs.samoa.instances.Prediction;

import moa.MOAObject;
import moa.capabilities.CapabilitiesHandler;
import moa.capabilities.Capability;
import moa.capabilities.ImmutableCapabilities;
import moa.core.Example;
import moa.core.Measurement;

/**
* Interface implemented by learner evaluators to monitor
* the results of the learning process.
*
* @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
* @version $Revision: 7 $
*/
public interface LearningPerformanceEvaluator<E extends Example> extends MOAObject, CapabilitiesHandler {

/**
* Resets this evaluator. It must be similar to
* starting a new evaluator from scratch.
*
*/
public void reset();

/**
* Adds a learning result to this evaluator.
*
* @param example the example to be classified
* @param classVotes an array containing the estimated membership
* probabilities of the test instance in each class
*/
public void addResult(E example, double[] classVotes);
public void addResult(E testInst, Prediction prediction);

/**
* Gets the current measurements monitored by this evaluator.
*
* @return an array of measurements monitored by this evaluator
*/
public Measurement[] getPerformanceMeasurements();

@Override
default ImmutableCapabilities defineImmutableCapabilities() {
return new ImmutableCapabilities(Capability.VIEW_STANDARD);
}

}

  其中,defineImmutableCapabilities()即为用default作为修饰符、具体实现的方法。

  在java8以后,接口中可以添加使用default作为修饰符的方法。使用default作为修饰符的方法,只能在接口中使用,被标记为普通方法,可以直接编写方法体。

-------------本文结束 感谢您的阅读-------------
觉得文章写的不错的话,请我喝瓶怡宝儿吧~ 🤞