From 790f45d25b4c65f833415520c2d7c759024a1b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Thu, 3 Jan 2019 11:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=90=E5=BC=8F=E8=BD=AC=E6=8D=A2=EF=BC=8C?= =?UTF-8?q?=E9=9D=A2=E5=90=91=E5=88=87=E9=9D=A2=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scala/top/fjy8018/scala/ImplicitApp.scala | 6 ++++-- .../scala/top/fjy8018/scala/ImplicitAspect.scala | 15 +++++++++++++++ .../scala/top/fjy8018/scala/RichFileApp.scala | 6 +++--- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/main/scala/top/fjy8018/scala/ImplicitAspect.scala diff --git a/src/main/scala/top/fjy8018/scala/ImplicitApp.scala b/src/main/scala/top/fjy8018/scala/ImplicitApp.scala index b8f749b..85ced79 100644 --- a/src/main/scala/top/fjy8018/scala/ImplicitApp.scala +++ b/src/main/scala/top/fjy8018/scala/ImplicitApp.scala @@ -1,5 +1,7 @@ package top.fjy8018.scala +import ImplicitAspect._ + /** * 隐式转换 * @@ -8,8 +10,8 @@ package top.fjy8018.scala */ object ImplicitApp extends App { - // 定义隐式转换函数 - implicit def man2superman(man: Man): Superman = new Superman(man.name) + // // 定义隐式转换函数 + // implicit def man2superman(man: Man): Superman = new Superman(man.name) val man = new Man("FJY") man.fly() diff --git a/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala b/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala new file mode 100644 index 0000000..9450243 --- /dev/null +++ b/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala @@ -0,0 +1,15 @@ +package top.fjy8018.scala + +import java.io.File + +/** + * F嘉阳 + * 2019-01-03 11:19 + */ +object ImplicitAspect { + // 定义隐式转换函数 + implicit def man2superman(man: Man): Superman = new Superman(man.name) + + // 目的在于为Java IO类增加读取文件内容方法 + implicit def file2RichFile(file: File): RichFile = new RichFile(file) +} diff --git a/src/main/scala/top/fjy8018/scala/RichFileApp.scala b/src/main/scala/top/fjy8018/scala/RichFileApp.scala index 7ae9e6f..4e7d78d 100644 --- a/src/main/scala/top/fjy8018/scala/RichFileApp.scala +++ b/src/main/scala/top/fjy8018/scala/RichFileApp.scala @@ -3,7 +3,7 @@ package top.fjy8018.scala import java.io.File import scala.io.{BufferedSource, Source} - +import ImplicitAspect._ /** * 隐式转换样例2 * @@ -12,8 +12,8 @@ import scala.io.{BufferedSource, Source} */ object RichFileApp extends App { - // 目的在于为Java IO类增加读取文件内容方法 - implicit def file2RichFile(file: File): RichFile = new RichFile(file) + // // 目的在于为Java IO类增加读取文件内容方法 + // implicit def file2RichFile(file: File): RichFile = new RichFile(file) val f = new File("tmp/hello.txt") println(f.readConent)