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 "what is the Scala non-parameter method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the Scala non-parameter method".
Scala class
As we can see, Scala is an object-oriented language, so it has a lot of descriptions of "classes". The Scala class is defined using a syntax similar to Java. But an important difference is that classes in Scala can have parameters, which leads to our definition of plural classes (Complex) as follows:
Class Complex (real: Double, imaginary: Double) {
Def re () = real
Def im () = imaginary
}
Our complex class (Complex) accepts two parameters: the real part and the imaginary part. These parameters must be passed when instantiated, like this: new Complex (1.5,2.3). The class definition includes two methods called re and im, which accept the two parameters mentioned above.
It is worth noting that the return types of these two methods are not explicitly declared. They are automatically recognized by the compiler. In this case, they are identified as Double
But the compiler does not always recognize it automatically, as in this example. Unfortunately, the rules about when to identify and when not to identify are quite cumbersome. This is usually not a problem in practice, because there are quite a few complaints when the compiler cannot handle it. As a recommended principle, novices to Scala can usually try to omit the type definition and let the compiler judge itself by context. Over time, beginners can sense when types should be omitted and when they should not.
No-parameter method
There is one small problem with the methods re and im: you must add a pair of parentheses after the name to call them. Take a look at the following example:
Object ComplexNumbers {
Def main (args: Array [String]) {
Val c = new Complex (1.2,3.4)
Println ("imaginary part:" + c.im ())
}
}
You may find it more comfortable to use these functions as variables rather than calling them as functions. In fact, we can do this in Scala by defining nonparametric functions. The difference between this kind of function and other functions with 0 arguments is that they don't need to be parenthesized after their names, so they don't have to be added when using them (but they are functions, no doubt), so our Complex class can be rewritten as follows
Class Complex (real: Double, imaginary: Double) {
Def re = real
Def im = imaginary
}
Inherit and overwrite
All classes in Scala inherit a parent class, and when the declared parent class is not displayed (like the Complex defined above), their parent class is implicitly specified as scala.AnyRef.
It is possible to override the members of the parent class in a subclass. However, you need to display the overrides of the specified member through the override modifier. Such rules can avoid accidental overrides. As a demonstration, we override Object's toString method in the definition of Complex.
Class Complex (real: Double, imaginary: Double) {
Def re = real
Def im = imaginary
Override def toString () =
"" + re + (if (im
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.