In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what is the difference between the class level of Scala and Java class, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Illustration 11.1 Scala class hierarchy diagram
Recommended by 51CTO editors: special topic on Scala programming language
Illustration 11.1 shows a class-level outline of Scala. At the top of the hierarchy is the class Any, which defines methods that include the following:
Final def = (that: Any): Boolean final def! = (that: Any): Boolean def equals (that: Any): Boolean def hashCode: Int def toString: String
Because every class inherits from every object in the Any,Scala program, every object can be compared with =,! =, or equals; with hashCode hash; and formatted with toString. The equal and unequal sign methods in class Any, = = and! =, are declared as final, so they cannot be overloaded in subclasses. In fact, = = is always the same as equals and! = is always the opposite of equals. So independent classes can clip the meaning of = or! = by overloading the equals method. We will show an example later in this chapter.
The root class Any has two subclasses: AnyVal and AnyRef. AnyVal is the parent of every built-in value class in Scala. There are nine such value classes: Byte,Short,Char,Int,Long,Float,Double,Boolean and Unit. The first eight of them correspond to the primitive types of Java, and their values are represented as the original values of Java at run time. Instances of these classes in Scala are written as text. For example, 42 is an instance of Int,'x'is an instance of Char, and false is an instance of Boolean. You cannot use new to create instances of these classes. This is a "little trick" in which value classes are defined as both abstract and final, enforced. So if you write:
Scala > new Int
You'll get:
< console>: 5: error: class Int is abstract; cannot be instantiated new Int customers
Another value class, Unit, roughly corresponds to the void type of Java; it is used as the result type of a method that does not return any interesting results. Unit has only one instance value, which is written (), discussed in Section 7. 2.
As explained in Chapter 5, value classes support general mathematical and Boolean operators as methods. For example, Int has methods named + and *, and Boolean has methods named | and & &. The value class also inherits all methods from the class Any. You can test it in the interpreter:
Scala > 42.toString res1: java.lang.String = 42 scala > 42.hashCode res2: Int = 42 scala > 42 equals 42 res3: Boolean = true
Note that the space of value classes is flat; all value classes are subtypes of scala.AnyVal, but they are not subclasses of each other. Instead, their different value class types can be implicitly converted to each other. For example, an instance of class scala.Int can be automatically relaxed (via implicit conversion) to an instance of class scala.Long when needed.
As mentioned in Section 5.9, implicit conversions are also used to add more functionality to value types. For example, the type Int supports all of the following operations:
Scala > 42 max 43 res4: Int = 43 scala > 42 min 43 res5: Int = 42 scala > 1 until 5 res6: Range = Range (1,2,3,4) scala > 1 to 5 res7: Range.Inclusive = Range (1,2,3,4,5) scala > 3.abs res8: Int = 3 scala > (- 3). Abs res9: Int = 3
Here's how it works: the methods min,max,until,to and abs are defined in class scala.runtime.RichInt, and there is an implicit conversion from class Int to RichInt. This transformation is applied when you call a method on Int that is not defined on Int but defined on RichInt. Similar "booster classes" and implicit conversions exist in other value classes. Implicit conversions are discussed in detail in Chapter 21.
Another subclass of class Any is class AnyRef. This is the base class for all reference classes in Scala. As mentioned earlier, AnyRef is actually an alias for class java.lang.Object on the Java platform. So the classes written in Java and the classes written in Scala are inherited from AnyRef. The reason for the existence of AnyRef aliases instead of java.lang.Object names is that Scala is designed to work on both Java and .net platforms. On the .NET platform, AnyRef is an alias for System.Object. In this way, you can think of java.lang.Object as the way to implement AnyRef on Java platform. So, although you can swap Object and AnyRef in Scala programs on the Java platform, the recommended style is to use only AnyRef anywhere.
The Scala class differs from the Java class in that they also inherit a special token feature called ScalaObject. The idea is that ScalaObject includes methods defined and implemented by the Scala compiler to make the execution of Scala programs more efficient. So far, the Scala object contains a single method, named $tag, for internal speed-up mode matching.
About the class level of Scala and Java class what are the differences to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.