From 44c025d8838a9d9bc93c2883e4355a289e300c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Thu, 3 Jan 2019 16:27:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=90=E5=BC=8F=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../top/fjy8018/scala/ImplicitClassApp.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/scala/top/fjy8018/scala/ImplicitClassApp.scala diff --git a/src/main/scala/top/fjy8018/scala/ImplicitClassApp.scala b/src/main/scala/top/fjy8018/scala/ImplicitClassApp.scala new file mode 100644 index 0000000..48fdc7f --- /dev/null +++ b/src/main/scala/top/fjy8018/scala/ImplicitClassApp.scala @@ -0,0 +1,18 @@ +package top.fjy8018.scala + +/** + * 隐式类 + * + * F嘉阳 + * 2019-01-03 16:23 + */ +object ImplicitClassApp extends App { + + implicit class Calculator(x: Int) { + def add(a: Int) = a + x + } + + // 原本整数1不具有add方法,使用隐式类后拥有该方法 + println(1.add(2)) + +}