Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/3_13_Interpolation/3_13_interprolation.sc
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 45 additions & 0 deletions src/3_14_Trait/3_14_trait.sc
Original file line number Diff line number Diff line change
@@ -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 {
}
10 changes: 10 additions & 0 deletions src/3_14_Trait/LogginScala.scala
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions src/3_14_Trait/LoggingJava.java
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 11 additions & 0 deletions src/3_14_Trait/ServiceImportanateScala.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions src/3_14_Trait/StdoutLoggingScala.scala
Original file line number Diff line number Diff line change
@@ -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")
}
20 changes: 20 additions & 0 deletions src/3_14_Trait/TestService2.scala
Original file line number Diff line number Diff line change
@@ -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)}"))
}

}
73 changes: 73 additions & 0 deletions src/3_14_Trait/stack_modification.sc
Original file line number Diff line number Diff line change
@@ -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 필터링됨.