2013-04-18 26 views
0

我想了解一个基于Java的开源项目,其中有一个代码段像约从设计角度来理解一个Java代码段

protected SimpleBinaryModel(ExampleSet exampleSet, double threshold) { 
    super(exampleSet); 
    this.threshold = threshold; 
} 

虽然我通常可以猜测如何,这种功能的目的是实现,我不太明白将此函数定义为“受保护”并将“exampleSet”定义为“超级”的原因。从面向对象设计的角度来定义它们的一般优点是什么?

另外,this.threshold = threshold;的目标是什么?

回答

2

这不是一个函数。这是一个constructor

super(exampleSet);表示基类具有带有ExampleSet参数的构造函数。

this.threshold = threshold;使用参数threshold的值初始化当前类别的阈值字段。

0

SimpleBinaryModel是一个构造函数。

super(exampleSet)正在调用超类的构造函数。它必须始终是第一行。

protected可以通过相同包的类和位于任何包中的子类访问。

1

A protected constructor意味着其他类不能使用new实例化对象,通常还有另一种方法来构建它们的实例(如factory method)。由于它受保护,所以子类仍然可以覆盖它。