Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

  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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top