diff --git a/src/3_13_Interpolation/3_13_interprolation.sc b/src/3_13_Interpolation/3_13_interprolation.sc new file mode 100644 index 0000000..fafb307 --- /dev/null +++ b/src/3_13_Interpolation/3_13_interprolation.sc @@ -0,0 +1,55 @@ +/* 단순 참조 */ +//val name = "Buck Trends" +//println(s"Hello, ${name}") + + +val number = 3.14 +/*중괄호 없이, 숫자->문자*/ +//println(s"Hello, $number") + +/* $표시 */ +//println("$") // $출력 +//println(s"$") // s를 붙이면 $는 인터폴레이선 처리를 위해 에러가 발생 +//println(s"$$") // $를 사용하기 위해서는 $$를 사용해야한다. +//println(f"$$") // $를 사용하기 위해서는 $$를 사용해야한다. + +/* s 와 f의 차이 */ +//println(s"$number%.4f") +/* + s로 하게 되면 인터폴레이션만 처리 + f로 하게되면 포맷터 사용 +*/ + +/* + printf 형식화를 위해 자바의 Formatter를 사용. + */ +//println(f"$number%.4f") +//printf(f"$number%.4f\n") // printf로도 사용가능 + +//val fNum = f"$number%.4f" //String 타입으로 저장 +//println(s"==> $fNum") + + +//val gross = 100000F +//val net = 64000F +//val percent = (net / gross * 100) +//println(f"$$${gross}%.2f vs. $$${net}%.2f or ${percent}%.1f%%") +//println(f"$$$gross%.2f vs. $$$net%.2f or $percent%.1f%%") + + +//val i = 200 + +//f"${i}%.2f" // i 는 Int형이나 암시적 변환이 적용되어 float형으로 포맷처 + +//val d = 100.22 + +//f"${d}%2d" // Double을 Int로 표현하려고 시도해서 컴파일 오류 + +//val s = "%02d: name = %s".format(5, "Dean Wampler") + + +//val name = "Dean Wampler" +// +//s"123\n$name\n456" +// +//raw"123\n$name\n456" diff --git a/src/3_14_Trait/3_14_trait.sc b/src/3_14_Trait/3_14_trait.sc new file mode 100644 index 0000000..6a80e2f --- /dev/null +++ b/src/3_14_Trait/3_14_trait.sc @@ -0,0 +1,45 @@ + +class ServiceImportante(name: String) { + def work(i: Int): Int = { + println(s"ServiceImportante: Doing important work! $i") + i+1 + } +} + +val service1 = new ServiceImportante("uno") +(1 to 3) foreach (i => println(s"Result: ${service1.work(i)}")) + + + +trait Logging { + def info (message:String): Unit + def warning(message:String): Unit + def error (message:String): Unit +} + +trait StdoutLogging extends Logging { + def info (message:String) = println(s"INFO: $message") + def warning (message:String) = println(s"WARNING: $message") + def error (message:String) = println(s"ERROR $message") +} + +trait StdoutLoggin2 { + def debug (message:String) = println(s"DEBUG: $message") + def value (message:String) = println(s"Value: $message") +} + + +val service2 = new ServiceImportante("dos") with StdoutLogging with StdoutLoggin2 { + override def work(i: Int): Int = { + info(s"String work: i = $i") + val result = super.work(i) + info(s"Ending work: i = $i, result = $result") + debug(s"Debug") + result + } +} +(1 to 3) foreach (i => println(s"Result: ${service2.work(i)}")) + +class LoggedServiceImportante(name: String) + extends ServiceImportante(name) with StdoutLogging { +} \ No newline at end of file diff --git a/src/3_14_Trait/LogginScala.scala b/src/3_14_Trait/LogginScala.scala new file mode 100644 index 0000000..8e4ca6f --- /dev/null +++ b/src/3_14_Trait/LogginScala.scala @@ -0,0 +1,10 @@ +package Trait3_14 + +/** + * Created by Neo on 2016-08-22. + */ +trait LoggingScala { + def info (message:String): Unit + def warning(message:String): Unit + def error (message:String): Unit +} \ No newline at end of file diff --git a/src/3_14_Trait/LoggingJava.java b/src/3_14_Trait/LoggingJava.java new file mode 100644 index 0000000..9c18b7e --- /dev/null +++ b/src/3_14_Trait/LoggingJava.java @@ -0,0 +1,10 @@ +package Trait3_14; + +/** + * Created by Neo on 2016-08-22. + */ +public interface LoggingJava { + void info(String message); + void warning(String message); + void error(String messae); +} diff --git a/src/3_14_Trait/ServiceImportanateScala.scala b/src/3_14_Trait/ServiceImportanateScala.scala new file mode 100644 index 0000000..dbc8ac8 --- /dev/null +++ b/src/3_14_Trait/ServiceImportanateScala.scala @@ -0,0 +1,11 @@ +package Trait3_14 + +/** + * Created by Neo on 2016-08-22. + */ +class ServiceImportanteScala(name: String) { + def work(i: Int, j: Int): Int = { + println(s"ServiceImportante: Doing important work! $i") + i+1 + } +} diff --git a/src/3_14_Trait/StdoutLoggingScala.scala b/src/3_14_Trait/StdoutLoggingScala.scala new file mode 100644 index 0000000..5295e95 --- /dev/null +++ b/src/3_14_Trait/StdoutLoggingScala.scala @@ -0,0 +1,10 @@ +package Trait3_14 + +/** + * Created by Neo on 2016-08-22. + */ +trait StdoutLoggingScala extends LoggingJava { + def info (message:String) = println(s"INFO: $message") + def warning (message:String) = println(s"WARNING: $message") + def error (message:String) = println(s"ERROR $message") +} diff --git a/src/3_14_Trait/TestService2.scala b/src/3_14_Trait/TestService2.scala new file mode 100644 index 0000000..68728d9 --- /dev/null +++ b/src/3_14_Trait/TestService2.scala @@ -0,0 +1,20 @@ +package Trait3_14 + +/** + * Created by Neo on 2016-08-22. + */ + +object TestService2 { + def main(args: Array[String]): Unit = { + val service2 = new ServiceImportanteScala("dos") with StdoutLoggingScala { + override def work(i: Int, j:Int): Int = { + info(s"String work: i = $i") + val result = super.work(i,3) + info(s"Ending work: i = $i, result = $result") + result + } + } + (1 to 3) foreach (i => println(s"Result: ${service2.work(i,3)}")) + } + +} diff --git a/src/3_14_Trait/stack_modification.sc b/src/3_14_Trait/stack_modification.sc new file mode 100644 index 0000000..c3c098e --- /dev/null +++ b/src/3_14_Trait/stack_modification.sc @@ -0,0 +1,73 @@ +/*** +트레이트로 확장되는 정수형 큐 예제 +***/ +//정수형 큐의 추상화 클래스 +abstract class IntQueue { + def get() : Int + def put(x:Int) +} + +//큐기능 구현 +import scala.collection.mutable.ArrayBuffer +class BasicIntQueue extends IntQueue { + private var buf = new ArrayBuffer[Int] + def get() = buf.remove(0) + def put(x: Int) {buf += x} +} + +/////실행부 q1 +val q1 = new BasicIntQueue +q1.put(10) +q1.put(20) +q1.get() +q1.get() + + +// 더블링 트레이트 정의 ( *2 ) +trait Doubling extends IntQueue { + abstract override def put(x: Int) {super.put(2 * x)} +} + +//기본 정수큐 상속 및 더블링큐 믹스 +class MyQueue extends BasicIntQueue with Doubling { +} + +//더블링 큐 실행테스트 +val q2 = new MyQueue +q2.put(10) +q2.get() + + +//숫자 +1증가 트레이트 정의 +trait Incrementing extends IntQueue { + abstract override def put(x: Int): Unit = {super.put(x+1)} +} + +// 음수 제거 트레이트 정의 +trait Filtering extends IntQueue { + abstract override def put(x: Int) {if (x >= 0) super.put(x)} +} + +/// 기본정수큐 와 1증가, 필터링 트레이트 믹스 테스트 +val q3 = (new BasicIntQueue with Incrementing with Filtering) +//q3.put(-1) +//q3.put(0) +//q3.put(1) +//q3.get() +//q3.get() + + +/// stack modification +// 값을 증가하고 필터링이 되었으면 값이 3개가 들어가야하나 +// 필터링->숫자+1증가 순서로 진행 +// trait가 stack modification 기능을 제공하는지 이해할 수 있음. + + +//추가 테스트 +val q4 =(new BasicIntQueue with Incrementing with Doubling with Filtering) +q4.put(-10) //필터링 +q4.put(0) // 필터링 -> 더블링:0 -> 1증가:1 -> 큐삽입 +q4.put(10) // 필터링 -> 더블링:20 -> 1증가:21 -> 큐삽입 +q4.get() // 1 +q4.get() // 21 +q4.get() // 에러~ -10 필터링됨.