In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use scala classOf, typeOf, object". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In java, jvm uses type wipe at run time, and types can no longer be distinguished simply by class. For example, the class of List and List is Class, but the two types (type) are different.
In Scala, the type system is much more complex than java, which provides a scala.reflect.runtime.universe.Type of its own.
Use typeOf to get type information in scala:
Scala > import scala.reflect.runtime.universe._
Scala > class A
Scala > typeOf [A]
Res44: reflect.runtime.universe.Type = A
Similarly, it is also convenient to get Class information in scala:
Scala > classOf [A]
In addition, because the getClass method is provided in java's Object, for scala objects, you can also use the
Scala > val a = new A
Scala > a.getClass
Res53: Class [_ object O
Scala > classOf [O] / / where O is a singleton object and an instance
: 14: error: not found: type O
For an instance, the only way to get its Class information is through the getClass method
Scala > O.getClass
Res60: Class [_ class A {class B} / / nested classes
Scala > val A1 = new A
Scala > val a2 = new A
Scala > val b1 = new a1.B
Scala > val b2 = new a2.B
For instances of inner class B, their class are all the same: inner class B (indicating that B is the inner class of A)
Scala > b1.getClass
Res8: Class [_ b1.getClass = = b2.getClass
Res7: Boolean = true
But their types are different:
Scala > typeOf [a1.B] = = typeOf [a2.B]
Res10: Boolean = false
This is because internal types depend on external instances (path dependent types), and external instances are different from each other.
But you can also abstract this type again.
Scala > typeOf [a1.B] typeOf [a2.B] classOf [list [string]] = = classOf [list [string]] (type erase)
Res16: Boolean = true
Scala > typeOf [list [Int]] = = typeOf [list [string]]
Res17: Boolean = false
In jvm, instances of a class are in the form of references, while types do not have this constraint, and the underlying type int,byte,char and so on are non-referenced. Types are more "specific" and "more detailed" than classes.
This is the end of "how to use scala classOf, typeOf, object". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.