当前位置:网站首页>Scala learning -- six uses of generics [t]
Scala learning -- six uses of generics [t]
2022-07-23 07:50:00 【Wu Nian】
package com.dtspark.scala.basics
/**
* 1,scala Classes and methods of 、 Functions can be generic .
*
* 2, The definition of type boundary is divided into upper boundary and lower boundary ( Limit classes )
* Upper boundary : The type that expresses generics must be " Some kind of " Or some kind of " Subclass ", The grammar is “<:”,
* Lower boundary : The type that expresses generics must be " Some kind of " Or some kind of " Parent class ", The grammar is “>:”,
*
* 3, "<%" :view bounds Can make some mysterious transformation , Convert your type into the target type without consciousness ,
* In fact, you can think view bounds It is the strengthening and supplement of the upper and lower boundaries , The grammar is :"<%", Want to use implicit Make implicit conversions ( See the example below )
*
* 4,"T:classTag": Equivalent to dynamic type , What type you pass in when using is what type ,(spark The compilation and operation of the program are distinguished Driver and Executor Of , The complete type information is only known at runtime )
* The grammar is :"[T:ClassTag]"
*
* 5, Contravariant and covariant :-T and +T( Here are specific examples )+T You can pass in its subclasses and itself ( And inheritance relationship one to )-T You can pass in its parent class and itself ( Contrary to inheritance ),
*
* 6,"T:Ordering" : It means that you will T become Ordering[T], You can directly use its method to compare the size , It can complete sorting and other work
*/
class Person(val name:String){
def talk(person:Person){
println(this.name+" speak to "+person.name)
}
}
class Worker(name:String)extends Person(name)
class Dog(val name:String)
// Note that generics use []
class Club[T<:Person](p1:T,p2:T){
//"<:" Must be person or person Subclasses of
def comminicate = p1.talk(p2)
}
class Club2[T<%Person](p1:T,p2:T){
def comminicate = p1.talk(p2)
}
class Engineer
class Expert extends Engineer
// If it is +T, When the specified type is a certain class , Pass in its subclasses or itself
// If it is -T, When the specified type is a certain class , Pass in its parent class or itself
class Meeting[+T]// You can pass in T or T Subclasses of
class Maximum[T:Ordering](val x:T,val y:T){
def bigger(implicit ord:Ordering[T])={
if(ord.compare(x, y)>0)x else y
}
}
object HelloScalaTypeSystem {
def main(args: Array[String]): Unit = {
val p= new Person("Spark")
val w= new Worker("Scala")
new Club(p,w).comminicate
//"<%" Liezi
// It just provides a conversion method , In case of <% Will call to see dog Whether it has been converted .
implicit def dog2Person(dog:Dog)=new Person(dog.name)
val d = new Dog("dahuang")
// Note that you must cast ,implicit Although it will dog Implicitly convert to person,
// But it's actually object erasure , Turned into object, So we have to force the type to be converted to person Only after that can we use
// use [person] Coercive transformation
new Club2[Person](p,d).comminicate
//-T +T Example , Below participateMeeting Method specifies the specific generic
val p1=new Meeting[Engineer]
val p2=new Meeting[Expert]
participateMeeting(p1)
participateMeeting(p2)
// T:Ordering Example
println(new Maximum(3,5).bigger)
println(new Maximum("Scala","Java").bigger)
}
// Here you specify what the incoming generic is
def participateMeeting(meeting:Meeting[Engineer])= println("welcome")
}
T:ClassTag Example
Command line code :
scala> import scala.reflect.ClassTag
import scala.reflect.ClassTag
scala> def mkArray[T: ClassTag](elems: T*) = Array[T](elems: _*)
mkArray: [T](elems: T*)(implicit evidence$1: scala.reflect.ClassTag[T])Array[T]
scala> mkArray(1,2,3)
res1: Array[Int] = Array(1, 2, 3)
scala> mkArray("ss","dd")
res2: Array[String] = Array(ss, dd)
边栏推荐
- Talk about 12 business scenarios of concurrent programming
- [record of question brushing] 18. Sum of four numbers
- 局域网SDN技术硬核内幕 4 从计算虚拟化到网络虚拟化
- 【翻译】宣布Krius--加速你对Kubernetes的监控采用
- The new idea 2022.2 was officially released, and the new features are really fragrant
- 如何使用订单流分析工具(中)
- 关于Redis,是先更新数据库,还是先更新缓存?
- 船新 IDEA 2022.2 正式发布,新特性真香
- 工作流引擎在vivo营销自动化中的应用实践
- Scala 当用到.contains() .exists()的性能问题
猜你喜欢

我为OpenHarmony 写代码,战“码”先锋第二期正式开启!

Redis三种集群方案

【翻译】宣布Krius--加速你对Kubernetes的监控采用

11.37万的星瑞是怎样一个产品和表现力?一起来看看吧

I use the factory mode in jd.com and explain the factory mode clearly

一年130+新服务和功能,这个存储“全家桶”又壮大了

Wechat campus second-hand book trading applet graduation design finished product (7) Interim inspection report

Trees and binary trees

Uniapp switches the tab bar to display different pages, remembers the page location and pulls up to get new data

Clever use of curl
随机推荐
ROS based navigation framework
LAN SDN hard core technology insider 19 unite all forces that can be united
Tensorflow2.0 sparse matrix input operation
11.37万的星瑞是怎样一个产品和表现力?一起来看看吧
无代码生产新模式探索
Clever use of curl
景联文科技提供3D点云-图像标注服务
LAN SDN hard core technology insider 17 from one to 100
21 -- 除自身以外数组的乘积
Inside the hard core of LAN SDN technology - evpn implementation of 16 three from thing to person user roaming in the park
flink批量读取es
Scala学习——泛型[T]的6种使用
Ftxui basic notes (Hello World)
【开发技术】SpingBoot数据库与持久化技术,JPA,MongoDB,Redis
6-14漏洞利用-rpcbind漏洞利用
Delete the duplicate items in the array (keep the last duplicate elements and ensure the original order of the array)
Understand the domestic open source Magnolia license series agreement in simple terms
多传感器融合综述---FOV与BEV
毕业设计-----基于STM32的物联网环境检测系统
局域网SDN技术硬核内幕 5 虚拟化网络的实现