简体   繁体   中英

about understanding a java code segment from its design perspective

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 does this.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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