문제

I am trying to understand a java-based open source project, which has a code segment like

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

Although I can generally guess how does this function aim to achieve, I do not quite understand the reason of defining this function as "protected" and defining "exampleSet" as "super". What are the general advantages of defining them this way from the perspective of OO design?

In addition, what doesthis.threshold = threshold; aim to achieve?

도움이 되었습니까?

해결책

This is not a function. It's a constructor.

super(exampleSet); means that a base class has a constructor with an ExampleSet parameter.

And this.threshold = threshold; initialized the threshold field of the current class with the value of the parameter threshold.

다른 팁

  1. The "protected" keyword declares that this method can only be referenced by a derived class or the declaring class. Go read In Java, difference between default, public, protected, and private

  2. "super" is calling the constructor of the parent class.

  3. this.threshold = threshold is assigning the input parameter to a local data member of the object instance.

You may want to read some basi java tutorials.

A protected constructor means that other classes can't instantiate objects using new and generally there's another way to build instances of them (like a factory method). Because it's protected, subclasses can still override it.

SimpleBinaryModel is a constructor.

super(exampleSet) is calling the superclass constructor. it must always be the first line.

protected are accessible by the classes of the same package and the subclasses residing in any package.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top