提交 f5c68f45 编写于 作者: 梦境迷离's avatar 梦境迷离

split

上级 fa864e90
......@@ -25,4 +25,4 @@ Scala程序员、函数式爱好者
* examples other
* leetcode leetcode
* scala-* scala
* rust-* rust
* rust-* rust
\ No newline at end of file
......@@ -37,6 +37,7 @@ subprojects {
dependencies {
compile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.8'
compile group: 'org.scala-lang', name: 'scala-reflect', version: '2.12.8'
}
[compileJava, compileScala, compileTestScala, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
......
......@@ -6,7 +6,6 @@ dependencies {
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
compile group: 'org.ow2.asm', name: 'asm-all', version: '5.1'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.29'
compile group: 'com.typesafe.play', name: 'play_2.12', version: '2.7.0'
compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.2.15'
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
}
\ No newline at end of file
顶级目录执行build task(gradle build)完成构建
> Task :scala-syntax:processResources NO-SOURCE
> Task :scala-syntax:classes
> Task :scala-syntax:jar
> Task :scala-syntax:assemble
> Task :scala-syntax:compileTestJava NO-SOURCE
> Task :scala-syntax:compileTestScala NO-SOURCE
> Task :scala-syntax:processTestResources NO-SOURCE
> Task :scala-syntax:testClasses UP-TO-DATE
> Task :scala-syntax:test NO-SOURCE
> Task :scala-syntax:check UP-TO-DATE
> Task :scala-syntax:build
> Task :scala-utils:compileJava NO-SOURCE
> Task :scala-utils:compileScala
> Task :scala-utils:processResources NO-SOURCE
> Task :scala-utils:classes
> Task :scala-utils:jar
> Task :scala-utils:assemble
> Task :scala-utils:compileTestJava NO-SOURCE
> Task :scala-utils:compileTestScala NO-SOURCE
> Task :scala-utils:processTestResources NO-SOURCE
> Task :scala-utils:testClasses UP-TO-DATE
> Task :scala-utils:test NO-SOURCE
> Task :scala-utils:check UP-TO-DATE
> Task :scala-utils:build
BUILD SUCCESSFUL in 12s
10 actionable tasks: 4 executed, 6 up-to-date
18:01:23: Task execution finished 'build'.
\ No newline at end of file
dependencies {
compile group: 'com.typesafe.play', name: 'play_2.12', version: '2.7.0'
}
\ No newline at end of file
package io.github.dreamylost
import examples.io.Source
import scala.io.Source
/**
* 从文件读取行,文件写入使用Java IO
......@@ -72,6 +72,6 @@ object Test11 extends App {
//控制台交互
print("Please enter your input:")
//val line = Console.readLine()//过期
val line = examples.io.StdIn.readLine()
val line = scala.io.StdIn.readLine()
println("Thanks,you just typed:" + line)
}
\ No newline at end of file
package io.github.dreamylost
import examples.util.control.Breaks._
import scala.util.control.Breaks._
/**
* Java是指令式风格,Scala是函数式风格。
......
......@@ -51,7 +51,7 @@ class OutPrivateClass {
// private val name: String = "hello" //下面方法均正常输出
// val name: String = "hello" //下面方法均正常输出
//限定具体的包的权限
private[basic] val name: String = "hello" //下面方法均正常输出
private[dreamylost] val name: String = "hello" //下面方法均正常输出
}
......
......@@ -2,7 +2,7 @@ package io.github.dreamylost
object Test6 extends App {
import examples.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
//List是具体实现,函数式不可变集合,相当于Java的LinkedList,但是Java没有不可变对象,它们不完全相同。Sequence相当于Java的List
val list = List(1, 2, 3) //不需要new,使用函数风格的调用,底层调用了List的伴生对象的工厂方法List.apply()
......
......@@ -10,7 +10,7 @@ object Test8 extends App {
// 注意:因为对不可变集使用+=实际是调用+=方法set1.+=("hhh"),所以表达式不需要重新赋值,set1可以是val
// 而对于可变集,+=实际是+调用后再进行赋值,所以不能为val
//对于只有一个参数的方法调用可以省略 . () ,所以set1.+=("hhh") ===> set1 += "hhh"
val set1 = examples.collection.mutable.Set("hello", "world") //提供+=方法
val set1 = collection.mutable.Set("hello", "world") //提供+=方法
set1 += "hhh" //实际是调用方法
println(set1.contains("hhh"))
println("===============Set遍历=============")
......@@ -18,7 +18,7 @@ object Test8 extends App {
println
println("================Map集===========")
//创建并初始化可变的Map
val map = examples.collection.mutable.Map[String, String]()
val map = collection.mutable.Map[String, String]()
map += ("a" -> "b") //实际是(a).->("b")方法调用,同样也是省略了. ()
map += ("b" -> "c") //底层也是+=的方法调用,所以map也可以是val
map += ("c" -> "d") //Map("hello"->"world","a"->"b")默认是不可变,不再解释
......
package io.github.dreamylost.examples
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ Await, Future, Promise }
import scala.util.Success
/**
*
* @author liguobin@growingio.com
......
......@@ -8,265 +8,265 @@ package io.github.dreamylost.examples.fb
*/
object errorhandling extends App {
// Console println Option.failingFn(1)
Console println Option.failingFn2(1)
Console println Option.mean(Seq(1, 2, 3, 4))
Console println Either.safeDiv(1, 0)
//可选数据类型
//函数放到特质中只是风格上的选择。以便使用 obj fn arg1 方式替代fn(obj,arg1)
sealed trait Option[+A] {
//4.1:Option基本函数实现
//如果Option不为None,对其应用f函数
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(a) => Some(f(a))
}
//如果Option不为None,返回实际值,否则返回默认值,default: => B表示非立即求值
def getOrElse[B >: A](default: => B): B = this match {
case None => default
case Some(a) => a
}
//可能会失败
def flatMap[B](f: A => Option[B]): Option[B] = {
map(f) getOrElse None
}
//使用模式匹配
def flatMap_1[B](f: A => Option[B]): Option[B] = this match {
case None => None
case Some(a) => f(a)
}
def orElse[B >: A](ob: => Option[B]): Option[B] = {
this map (Some(_)) getOrElse ob
}
//使用模式匹配,B >: A 表示B的类型必须是A或者是A的超类类型
def orElse_1[B >: A](ob: => Option[B]): Option[B] = this match {
case None => ob
case _ => this
}
def filter(f: A => Boolean): Option[A] = {
flatMap(a => if (f(a)) Some(a) else None)
}
//使用模式匹配
def filter_1(f: A => Boolean): Option[A] = this match {
case Some(a) if f(a) => this
case _ => None
}
// Console println Option.failingFn(1)
Console println Option.failingFn2(1)
Console println Option.mean(Seq(1, 2, 3, 4))
Console println Either.safeDiv(1, 0)
//可选数据类型
//函数放到特质中只是风格上的选择。以便使用 obj fn arg1 方式替代fn(obj,arg1)
sealed trait Option[+A] {
//4.1:Option基本函数实现
//如果Option不为None,对其应用f函数
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(a) => Some(f(a))
}
//如果Option不为None,返回实际值,否则返回默认值,default: => B表示非立即求值
def getOrElse[B >: A](default: => B): B = this match {
case None => default
case Some(a) => a
}
//可能会失败
def flatMap[B](f: A => Option[B]): Option[B] = {
map(f) getOrElse None
}
//使用模式匹配
def flatMap_1[B](f: A => Option[B]): Option[B] = this match {
case None => None
case Some(a) => f(a)
}
def orElse[B >: A](ob: => Option[B]): Option[B] = {
this map (Some(_)) getOrElse ob
}
//使用模式匹配,B >: A 表示B的类型必须是A或者是A的超类类型
def orElse_1[B >: A](ob: => Option[B]): Option[B] = this match {
case None => ob
case _ => this
}
def filter(f: A => Boolean): Option[A] = {
flatMap(a => if (f(a)) Some(a) else None)
}
//使用模式匹配
def filter_1(f: A => Boolean): Option[A] = this match {
case Some(a) if f(a) => this
case _ => None
}
}
//非空
case class Some[+A](get: A) extends Option[A]
//空
case object None extends Option[Nothing]
object Option {
//书上例子
def failingFn(i: Int): Int = {
val y: Int = throw new Exception("fail!")
try {
val x = 42 + 5
x + y
}
catch {
case e: Exception => 43
}
}
//书上例子
def failingFn2(i: Int): Int = {
try {
val x = 42 + 5
//抛出的异常可以被赋予任何类型
x + ((throw new Exception("fail!")): Int)
}
catch {
case e: Exception => 43
}
}
//书上例子
def mean(xs: Seq[Double]): Option[Double] = {
if (xs.isEmpty) None
else Some(xs.sum / xs.length)
}
/**
* 4.2:根据flatMap实现方差函数
*
* @param xs
* @return
*/
def variance(xs: Seq[Double]): Option[Double] = {
//方差:对每个元素求math.pow(x-m,2)的累加和并/元素个数,标准差再开2次根号
mean(xs) flatMap (m => mean(xs.map(x => math.pow(x - m, 2))))
}
/**
* 4.3:使用一个二元函数组合两个Option值
*
* @param a
* @param b
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return
*/
def map2[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = {
a flatMap {
aa => b map (bb => f(aa, bb))
}
}
//for推导实现
def map2_1[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = {
for {
aa <- a
bb <- b
} yield f(aa, bb)
}
/**
* 4.4:将Option列表结合为一个Option
*
* @param a
* @tparam A
* @return
*/
def sequence[A](a: List[Option[A]]): Option[List[A]] = {
a match {
case Nil => Some(Nil)
case h :: t => h flatMap (hh => sequence(t) map (hh :: _)) //显示递归
}
}
//使用右折叠,参考3.15的concat函数
def sequence_1[A](a: List[Option[A]]): Option[List[A]] = {
a.foldRight[Option[List[A]]](Some(Nil))((x, y) => map2(x, y)(_ :: _))
}
//组合所有Option元素
def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = {
a match {
case Nil => Some(Nil)
case h :: t => map2(f(h), traverse(t)(f))(_ :: _) //显示递归
}
}
//使用右折叠
def traverse_1[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = {
a.foldRight[Option[List[B]]](Some(Nil))((h, t) => map2(f(h), t)(_ :: _))
}
/**
* 4.5:使用map和sequence函数,只遍历一次列表
*
* @param a
* @tparam A
* @return
*/
def sequenceViaTraverse[A](a: List[Option[A]]): Option[List[A]] = {
traverse(a)(x => x)
}
}
//包含异常信息,基本与Option相似
sealed trait Either[+E, +A] {
//4.6:Either的map、flatMap、orElse、map2,类似Option
def map[B](f: A => B): Either[E, B] = {
this match {
case Right(a) => Right(f(a))
case Left(e) => Left(e)
}
}
//对右侧进行mapping时,必须限定左边的类型参数是E的父类型
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = {
this match {
case Left(e) => Left(e)
case Right(a) => f(a)
}
}
def orElse[EE >: E, AA >: A](b: => Either[EE, AA]): Either[EE, AA] = {
this match {
case Left(_) => b
case Right(a) => Right(a)
}
}
def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = {
for {a <- this; b1 <- b} yield f(a, b1)
}
}
//互斥并集。一般表示失败
case class Left[+E](get: E) extends Either[E, Nothing]
//一般表示成功
case class Right[+A](get: A) extends Either[Nothing, A]
object Either {
//书上例子
def mean(xs: IndexedSeq[Double]): Either[String, Double] = {
if (xs.isEmpty)
Left("mean of empty list!")
else
Right(xs.sum / xs.length)
}
//书上例子
def safeDiv(x: Int, y: Int): Either[Exception, Int] = {
try Right(x / y)
catch {
case e: Exception => Left(e)
}
}
//书上例子
def Try[A](a: => A): Either[Exception, A] = {
try Right(a)
catch {
case e: Exception => Left(e)
}
}
//非空
case class Some[+A](get: A) extends Option[A]
//空
case object None extends Option[Nothing]
object Option {
//书上例子
def failingFn(i: Int): Int = {
val y: Int = throw new Exception("fail!")
try {
val x = 42 + 5
x + y
}
catch {
case e: Exception => 43
}
}
//书上例子
def failingFn2(i: Int): Int = {
try {
val x = 42 + 5
//抛出的异常可以被赋予任何类型
x + ((throw new Exception("fail!")): Int)
}
catch {
case e: Exception => 43
}
}
//书上例子
def mean(xs: Seq[Double]): Option[Double] = {
if (xs.isEmpty) None
else Some(xs.sum / xs.length)
}
/**
* 4.2:根据flatMap实现方差函数
*
* @param xs
* @return
*/
def variance(xs: Seq[Double]): Option[Double] = {
//方差:对每个元素求math.pow(x-m,2)的累加和并/元素个数,标准差再开2次根号
mean(xs) flatMap (m => mean(xs.map(x => math.pow(x - m, 2))))
}
/**
* 4.3:使用一个二元函数组合两个Option值
*
* @param a
* @param b
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return
*/
def map2[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = {
a flatMap {
aa => b map (bb => f(aa, bb))
}
}
//for推导实现
def map2_1[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = {
for {
aa <- a
bb <- b
} yield f(aa, bb)
}
/**
* 4.4:将Option列表结合为一个Option
*
* @param a
* @tparam A
* @return
*/
def sequence[A](a: List[Option[A]]): Option[List[A]] = {
a match {
case Nil => Some(Nil)
case h :: t => h flatMap (hh => sequence(t) map (hh :: _)) //显示递归
}
}
//使用右折叠,参考3.15的concat函数
def sequence_1[A](a: List[Option[A]]): Option[List[A]] = {
a.foldRight[Option[List[A]]](Some(Nil))((x, y) => map2(x, y)(_ :: _))
}
//组合所有Option元素
def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = {
a match {
case Nil => Some(Nil)
case h :: t => map2(f(h), traverse(t)(f))(_ :: _) //显示递归
}
}
//使用右折叠
def traverse_1[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = {
a.foldRight[Option[List[B]]](Some(Nil))((h, t) => map2(f(h), t)(_ :: _))
}
/**
* 4.5:使用map和sequence函数,只遍历一次列表
*
* @param a
* @tparam A
* @return
*/
def sequenceViaTraverse[A](a: List[Option[A]]): Option[List[A]] = {
traverse(a)(x => x)
}
//4.7:类似Option
def traverse[E, A, B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
es match {
case Nil => Right(Nil)
case h :: t => (f(h) map2 traverse(t)(f)) (_ :: _)
}
}
//包含异常信息,基本与Option相似
sealed trait Either[+E, +A] {
//4.6:Either的map、flatMap、orElse、map2,类似Option
def map[B](f: A => B): Either[E, B] = {
this match {
case Right(a) => Right(f(a))
case Left(e) => Left(e)
}
}
//对右侧进行mapping时,必须限定左边的类型参数是E的父类型
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = {
this match {
case Left(e) => Left(e)
case Right(a) => f(a)
}
}
def orElse[EE >: E, AA >: A](b: => Either[EE, AA]): Either[EE, AA] = {
this match {
case Left(_) => b
case Right(a) => Right(a)
}
}
def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = {
for {a <- this; b1 <- b} yield f(a, b1)
}
def traverse_1[E, A, B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
es.foldRight[Either[E, List[B]]](Right(Nil))((a, b) => f(a).map2(b)(_ :: _))
}
//互斥并集。一般表示失败
case class Left[+E](get: E) extends Either[E, Nothing]
//一般表示成功
case class Right[+A](get: A) extends Either[Nothing, A]
object Either {
//书上例子
def mean(xs: IndexedSeq[Double]): Either[String, Double] = {
if (xs.isEmpty)
Left("mean of empty list!")
else
Right(xs.sum / xs.length)
}
//书上例子
def safeDiv(x: Int, y: Int): Either[Exception, Int] = {
try Right(x / y)
catch {
case e: Exception => Left(e)
}
}
//书上例子
def Try[A](a: => A): Either[Exception, A] = {
try Right(a)
catch {
case e: Exception => Left(e)
}
}
//4.7:类似Option
def traverse[E, A, B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
es match {
case Nil => Right(Nil)
case h :: t => (f(h) map2 traverse(t)(f)) (_ :: _)
}
}
def traverse_1[E, A, B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
es.foldRight[Either[E, List[B]]](Right(Nil))((a, b) => f(a).map2(b)(_ :: _))
}
def sequence[E, A](es: List[Either[E, A]]): Either[E, List[A]] = {
traverse(es)(x => x)
}
def sequence[E, A](es: List[Either[E, A]]): Either[E, List[A]] = {
traverse(es)(x => x)
}
}
//4.8使用Either[List[E],_]
//或使用新数据结构
trait Partial[+A, +B]
//4.8使用Either[List[E],_]
//或使用新数据结构
trait Partial[+A, +B]
case class Errors[+A](get: Seq[A]) extends Partial[A, Nothing]
case class Errors[+A](get: Seq[A]) extends Partial[A, Nothing]
case class Success[+B](get: B) extends Partial[Nothing, B]
case class Success[+B](get: B) extends Partial[Nothing, B]
}
......@@ -10,97 +10,97 @@ import scala.annotation.tailrec
*/
object gettingstarted extends App {
Console println fib(5) //等价 println(finb(5)),以后只会使用这种方法
//降序
Console println isSorted[Int](Array(7, 6, 5, 4, 3, 2, 1), (n, m) => n > m)
//升序
Console println isSorted[Int](Array(1, 2, 3, 4, 5, 6, 7), (n, m) => n < m)
Console println fib(5) //等价 println(finb(5)),以后只会使用这种方法
//降序
Console println isSorted[Int](Array(7, 6, 5, 4, 3, 2, 1), (n, m) => n > m)
//升序
Console println isSorted[Int](Array(1, 2, 3, 4, 5, 6, 7), (n, m) => n < m)
/**
* 2.1:写一个递归函数,获取第n个斐波那契数,前两个是0,1
*
* @param n
* @return
*/
def fib(n: Int): Int = {
/**
* 2.1:写一个递归函数,获取第n个斐波那契数,前两个是0,1
* 使用局部尾递归函数而不是循环
*
* @param n
* @param n 第几个斐波那契数
* @param ret1 第n个值
* @param ret2 第n与第n+1的和
* @return
*/
def fib(n: Int): Int = {
/**
* 使用局部尾递归函数而不是循环
*
* @param n 第几个斐波那契数
* @param ret1 第n个值
* @param ret2 第n与第n+1的和
* @return
*/
@tailrec //尾递归优化,不符合优化规则报错
def go(n: Int, ret1: Int, ret2: Int): Int = {
//注意是从0开始的斐波那契数列
if (n == 1) ret1 else go(n - 1, ret2, ret1 + ret2)
}
go(n, 0, 1)
@tailrec //尾递归优化,不符合优化规则报错
def go(n: Int, ret1: Int, ret2: Int): Int = {
//注意是从0开始的斐波那契数列
if (n == 1) ret1 else go(n - 1, ret2, ret1 + ret2)
}
/**
* 2.2:实现isSorted方法,检测Array[A]是否按照给定的比较函数排序
*
* @param as 数组
* @param ordered 排序需要的比较方式,是个函数
* @tparam A 泛型类型,测试一律使用Int
* @return
*/
def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = {
@tailrec
def loop(n: Int): Boolean = {
if (n + 1 < as.length && ordered(as(n), as(n + 1))) loop(n + 1)
else if (as.length - 1 == n) true
else false
}
//启动调用,数组第一个下标是0
loop(0)
go(n, 0, 1)
}
/**
* 2.2:实现isSorted方法,检测Array[A]是否按照给定的比较函数排序
*
* @param as 数组
* @param ordered 排序需要的比较方式,是个函数
* @tparam A 泛型类型,测试一律使用Int
* @return
*/
def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = {
@tailrec
def loop(n: Int): Boolean = {
if (n + 1 < as.length && ordered(as(n), as(n + 1))) loop(n + 1)
else if (as.length - 1 == n) true
else false
}
//启动调用,数组第一个下标是0
loop(0)
}
/**
* 2.3:柯里化
*
* 2个参数转换为只有1个参数的部分应用函数
* 满足编译通过即可
*
* 部分应用表示函数被应用的参数不是它所需要的完整的参数
* 因为=>是右结合的,A=>(B=>C)后面的括号可去掉
*
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return 返回类型A => (B => C),需要参数A,参数B,并应用到函数f中
*/
def curry[A, B, C](f: (A, B) => C): A => (B => C) = (a: A) => ((b: B) => f(a, b))
/**
* 2.3:柯里化
*
* 2个参数转换为只有1个参数的部分应用函数
* 满足编译通过即可
*
* 部分应用表示函数被应用的参数不是它所需要的完整的参数
* 因为=>是右结合的,A=>(B=>C)后面的括号可去掉
*
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return 返回类型A => (B => C),需要参数A,参数B,并应用到函数f中
*/
def curry[A, B, C](f: (A, B) => C): A => (B => C) = (a: A) => ((b: B) => f(a, b))
/**
*2.4:反柯里化
*
* 与柯里化相反。
*
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return 返回类型(A, B) => C 需要a,b参数,返回值需要应用f函数两次,可以理解是柯里化调用两次
*/
def uncurry[A, B, C](f: A => B => C): (A, B) => C = (a: A, b: B) => f(a)(b)
/**
*2.4:反柯里化
*
* 与柯里化相反。
*
* @param f
* @tparam A
* @tparam B
* @tparam C
* @return 返回类型(A, B) => C 需要a,b参数,返回值需要应用f函数两次,可以理解是柯里化调用两次
*/
def uncurry[A, B, C](f: A => B => C): (A, B) => C = (a: A, b: B) => f(a)(b)
/**
* 2.5:实现高阶函数,组合两个函数为一个函数
*
* 使用compose方法是作弊
*
* @param f
* @param g
* @tparam A
* @tparam B
* @tparam C
* @return
*/
def compose[A, B, C](f: B => C, g: A => B): A => C = (a: A) => f(g(a))
/**
* 2.5:实现高阶函数,组合两个函数为一个函数
*
* 使用compose方法是作弊
*
* @param f
* @param g
* @tparam A
* @tparam B
* @tparam C
* @return
*/
def compose[A, B, C](f: B => C, g: A => B): A => C = (a: A) => f(g(a))
}
......@@ -17,8 +17,8 @@ object ScalaReflectDemoTest extends App {
def method() {
//使用Scala的反射API对象方法
val classMirror = universe.runtimeMirror(getClass.getClassLoader)
val classTest = classMirror.reflect(new cn.edu.jxnu.scala.reflect.ReflectDemoTestClass) //获取需要反射的类对象
val methods = universe.typeOf[cn.edu.jxnu.scala.reflect.ReflectDemoTestClass]
val classTest = classMirror.reflect(new io.github.dreamylost.examples.reflect.ReflectDemoTestClass) //获取需要反射的类对象
val methods = universe.typeOf[io.github.dreamylost.examples.reflect.ReflectDemoTestClass]
val method = methods.decl(universe.TermName("testMethod")).asMethod
val result = classTest.reflectMethod(method)("test name for args")
val ret = result.asInstanceOf[String]
......@@ -28,7 +28,7 @@ object ScalaReflectDemoTest extends App {
def staticMethod() {
//使用Scala的反射API对象的静态方法
val classMirror = universe.runtimeMirror(getClass.getClassLoader) //获取运行时类镜像
val classTest = classMirror.staticModule("cn.edu.jxnu.scala.reflect.ReflectDemoTestClass") //获取需要反射object
val classTest = classMirror.staticModule("io.github.dreamylost.examples.reflect.ReflectDemoTestClass") //获取需要反射object
val methods = classMirror.reflectModule(classTest) //构造获取方式的对象
val objMirror = classMirror.reflect(methods.instance) //反射结果赋予对象
val method = methods.symbol.typeSignature.member(universe.TermName("testStaticMethod")).asMethod //反射调用函数
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册