Scala Learning: Invoking the use of superclass constructors and override modifiers

Source: Internet
Author: User
Tags constructor modifiers

Calling the Superclass Builder

Now you have a complete system of two classes: the abstract class element, and the specific class arrayelement that extends it. Perhaps you are still imagining other ways of expressing elements. For example, a customer might want to create a layout element that is composed of a given single-line string. Object-oriented programming makes it easy to extend a system with new data variants. Just add a subclass. For example, code 10.6 shows the Lineelement class for extending arrayelement:

Class Lineelement (s:string) extends

arrayelement (Array (s)) {
override def width = s.length
override de F height = 1
}

Code 10.6 calls the superclass constructor

Because the lineelement extends the arrayelement and the Arrayelement constructor takes a parameter (array[string]), Lineelement needs to pass a parameter to the main constructor of its superclass. To invoke a superclass constructor, simply place the argument or argument list you want to pass in parentheses after the superclass name. For example, class Lineelement passes the main constructor of array (s) to arrayelement, placing it in parentheses behind the name of the superclass arrayelement:

... extends Arrayelement (Array (s)) ...

With the new subclass, the inheritance level of the layout element now looks like it was shown in emoticons 10.2.

Class diagram of 10.2 lineelement of emoticons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.