In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
[TOC]
Overview
What are the type parameters? Type parameters are actually generics in Java. You should know something about generics in Java, for example, we have List list = new ArrayList (), then list.add (1), no problem, list.add ("2"), and then we list.get (1) = = 2, right? Definitely not. List.get (1) actually gets a String-- "2". How can String-- "2" be equal to an Integer type 2?
So the concept of generics, which is actually the concept of type parameters, is put forward in Java. At this point, you can create List,List list = new ArrayList [Integer] () with generics, so list.add (1) is fine, but list.add ("2")? No, because type generics restrict the parameters passed in, and you can only add Integer types to the collection list, thus avoiding the numerical problems mentioned above.
The type parameter in Scala is the same as the generics of Java, which also defines a type parameter.
Finally, Scala type parameters are also very common in Spark source code, so you must also master them in order to understand the spark source code.
Generic class
Like Java or C++, classes and attributes can take type parameters. In Scala, we use square brackets to define type parameters
Class Student [T, S] (val first: t, val second: s)
The above defines a class with two type parameters T and S. In the definition of a class, you can use type parameters to define variables, method parameters, and the types of return values.
We call a class with one or more type parameters a generic class. If you replace the type parameter with the actual type, you will get a normal class. Such as Student [Int,String]
Scala infers the actual type from the construction parameters:
Val p = new Student (42, "String")
You can also specify the type yourself, and the test code is as follows:
The type parameters of package cn.xpleaf.bigdata.p5.mygeneric/** * scala, that is, generics in java, are defined in different ways. Java is used, and scala uses [] * generics to define the role of generics in class idiosyncratic method functions * generics, that is, exceptions during the run Advanced to compiler * improve code versatility * / object _ 01GenericOps {def main (args: Array [String]): Unit = {genericOps1} / * definition of generic classes * / def genericOps1: Unit = {class Student [T, S] (val first: t, val second: s) {println (first + "\ t" + second)} new Student (23 "xpleaf") / / automatic type inference new Student [Int, String] (22, "jieling") new Student [Any, Any] ("hadoop", "spark")}}
The output is as follows:
23 xpleaf22 jielinghadoop spark generic function
Functions and methods can also take type parameters:
Def getStudentInfo [T] (stu: Array [T]) = stu (stu.length / 2)
Like generic classes, you need to put type parameters after the method name.
Scala infers the type from the actual type used to call the method:
Def methodOps: Unit = {def getStudentInfo [T] (stu: Array [T]) = stu (stu.length / 2) val student = getStudentInfo (Array ("garry", "tom", "john", "lucy", "Richard")) println (student)}
Test in the main function, and the output is as follows:
John type variable definition-upper limit (upper bounds)
Let's first look at a simple example, which is used to judge the larger values of the two variables, both of which are of type int.
/ * Type variable definition * / def typeValueOps: Unit = {class StudentInt (val first: Int, val second: Int) {def bigger = {if (first.compareTo (second) > 0) first else second}} val studentInt = new StudentInt (1,2) println (studentInt.bigger)}
The bigger method in the above StudentInt class calls the compare method. If we want to compare the size of two string-type variables, we can add the StudentStr class as above:
Class StudentStr (val first: String, val second: String) {def bigger = {if (first.compareTo (second) > 0) first else second}}
If we write a specific class for each basic type, the code is too large and not concise enough, and we think that generics can solve this problem more easily:
Class Student [T] (val first: t, val second: t) {def smaller = if (first.compareTo (second) < 0) first else second}
At the same time, however, the generic T we defined does not specify an implementation of the compareTo method, nor is it a subclass of a type. In Java generics, a type is a subtype of the Test type, using the extends keyword:
/ / or in the form of wildcards:
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.