In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Scala's two-dimensional layout library and abstract classes". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use Scala's two-dimensional layout library and abstract classes.
2D layout library
Create a library that makes and renders 2D layout elements, each of which will represent a rectangle filled with characters. For convenience, the library will provide a factory method called "elem" to construct new elements from the incoming data. For example, you will be able to create an element with a string using the following writing through the factory method:
Elem (s: String): Element
As you can see, the element will be modeled on a type named Element. You will be able to call above or beside on the element, passing in the second element, resulting in a new element that combines the two. For example, the following expression builds a larger element with two columns, each with a height of two:
Val column1 = elem ("hello") above elem ("* *") val column2 = elem ("* *") above elem ("world") column1 beside column2
The result of printing this expression will be:
Hello * * world
Layout elements are a good example in systems where objects can be built with the help of simple widgets and combination operators. In this chapter, we will define classes so that element objects can be built from arrays, row records, and rectangles-simple widgets. We will also define the combination operators above and beside. Such combination operators are also often referred to as combinators: combinator because they combine elements from certain areas into new elements.
Thinking in terms of combinators is often a good way to implement the design of a library: it can be rewarded to consider the underlying approach to building objects in the application domain. What is a simple object? In what way can more interesting objects be constructed from simple objects? How do the combinators hang together? What is the most common combination? Do they meet any interesting rules? If you have good answers to all these questions, your library design is on track.
Abstract class
Our task is to define the type Element that represents the layout element. Since the element is a two-dimensional character rectangle, including members, it makes sense to point to the contents of the content of the layout element. The content can be expressed as an array of strings, where each string represents a line. Therefore, the result type returned by contents is Array [String]. Code 10.1 shows what it looks like.
In this class, contents is declared as an unimplemented method. In other words, the method is an abstraction of the class Element: the abstract member. Classes with abstract members must themselves be declared abstract, as long as the abstract modifier is preceded by the class keyword:
Abstract class Element {def contents: Array [String]}
Code 10.1 defines abstract methods and classes
Abstract class Element...
The abstract modifier indicates that the class may have abstract members that are not implemented. As a result, you cannot instantiate an abstract class. If you try to do this, you will get a compiler error:
Scala > new Element
< console>: 5: error: class Element is abstract; cannot be instantiated new Element customers
Note that the contents method of class Element does not have the abstract modifier. If a method is not implemented (that is, there is no equal sign or method body), it is abstract. Unlike Java, abstract modifiers are not required (or allowed) in method declarations. The method of owning the implementation is called concrete: concrete.
At this point, I believe you have a deeper understanding of "Scala's two-dimensional layout library and how to use abstract classes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.