2015. 5. 23. 13:56 카테고리 없음

임시

책 내용 정리

10장 상속과 구성
 . 구성 : 어떤 클래스가 다른 클래스의 참조를 갖는것을 말함
 . 상속 : 슈퍼 클래스/서브 클래스 관계를 이룸
10.1 2차원 레이아웃 라이브러리
 . 조립 연산자(예 above, beside)는 특정 도메인의 요소를 결합해 새로운 요소를 만들어 내기 때문에 콤비네이터(combinator)라고도 부름
10.2 추상 클래스
 . class 앞에 수식자 abstract 를 사용
 . 메소드에는 사용하지 않음, 구현이 없으면 추상 메서드임
  예) def contens: Array[String]
10.3 파라미터 없는 메소드 정의
 . def width: Int 와 같은 것을 파라미터 없는 메서드
 . def width(): Int 와 같은 것을 빈괄호 메소드라 부름
 . 어떤 메소드가 인자도 받지 않고 메소드가 속한 객체의 변경 가능한 필드를 읽기만 하는 경우(객체의 상태를 변경하지 않는 경우)에는
   파라미터 없는 메소드를 사용. 이러한 관례는 필드나 메소드 중 어떤 방식으로 속성을 정의하더라도 클라이언트 코드에는 영향을 끼치지 말아야
   한다는 단일 접근 원칙에 부합
 . def width: Int, val width = xxxx; 는 클라이언트 관점에서 보면 같음. 유일한 차이는 필드를 사용하면 클래스가 초기화 값을 미리 계산해두기
   때문에 매번 계산을 수행하는 메소드 방식보다 약간 빠름, 반면 필드로 구현하면 각 Element 객체마다 값을 저장할 별도의 메모리 공간이 필요
 . 인자를 받지 않고 부수 효과도 없는 메소드는 파라미터 없는 메소드를, 효과가 있다면 필드로 접근하는 것과 동일하게 보일수 있으므로 괄호를 권장
10.4 클래스 확장
 . 서브클래서는 슈퍼 클래스의 비공개 멤버를 상속하지 않고, 슈퍼클래스의 멤버와 이름과 파라미터가 모두 동일한 멤버 정의가 있으면 오버라이드 함
10.5 메소드와 필드 오버라이드
 . 필드가 파라미터 없는 메소드를 오버라이드 할수 있음.
 . 한 클래스에서 같은 이름의 필드와 메소드를 동시에 정의하지 못함
 . 스칼라에는 값(필드, 메소드, 패키지, 싱글톤 객체)와 타입(클래스와 트레이트 이름)의 두가지 네임스페이스만 있음
10.6 파라미터 필드 정의
10.7 슈퍼클래스의 생성자 호출
 . 슈퍼클래스의 생성자를 호출하려면 원하는 인자를 슈퍼클래스의 이름 뒤에 괄호로 묶어서 넘기면 됨
10.8 override 수식자 사용
 . 추상 멤버를 구현할 경우에는 override 수식자를 생략할수 있음
 . 기반 클래스에 있는 멤버를 오러라이드 하거나 구현하는 경우가 아니라면 override 수식자를 사용하면 안됨.
10.9 다형성과 동적 바인딩
10.10 final 멤버 선언
 . 메소드나 클래스에 final 수식자를 붙이면 상속을 못함.
10.11 상속과 구성 사용
10.12 above, beside, toString 구현
 . above - 어떤 요소를 또 다른 요소 위에 올려놓는다는 것은 2개의 요소가 가진 내용을 이어붙인다는 것.
 . ++ 연산을 두 배열을 이어 붙임
 .beside - 두 요소를 서로의 옆에 놓기 위해, 두 요소의 각 줄을 한 줄로 합친 결과가 새로운 요소의 각 줄이 되게 될것
 .
  예)
  abstract class Element {
    def contents: Array[String]
   
    def height: Int = contents.length
    def width: Int = if(height == 0) 0 else contents(0).length
   
    def above(that: Element): Element =
      new ArrayElement(this.contents ++ that.contents)
   
    def beside(that: Element): Element = {
  //    val plusContents = new Array[String](this.contents.length)
  //    for(i <- 0 until this.contents.length)
  //      plusContents(i) = this.contents(i) + that.contents(i)
  //    new ArrayElement(plusContents)
     
      new ArrayElement(
        for (
            (line1, line2) <- this.contents zip that.contents
        ) yield line1 + line2   
      )
    }
   
    override def toString = contents mkString "\n"
  }
10.13 팩토리 객체 정의
 . 객체 생성 기능을 한곳에 모아서 제공하고, 구체적인 내부 표현을 감출 수 있음
10.14 높이와 너비 조절
package study3

import factory.Element._

abstract class Element {
  def contents: Array[String]
 
  def width: Int = if(height == 0) 0 else contents(0).length
  def height: Int = contents.length
 
  def above(that: Element): Element =  {
    val this1 = this widen that.width
    val that1 = that widen that.width
    elem(this1.contents ++ that1.contents)   
  }
   
  def beside(that: Element): Element = {
    val this1 = this heighten that.height
    val that1 = that heighten that.height
   
    elem(for((line1, line2) <- this1.contents zip that1.contents)
      yield line1 + line2)   
  }
 
  def widen(w:Int): Element = {
    if(w <= width) this
    else {
      val left = elem(' ', (w-width) / 2, height)
      val right = elem(' ', width - width - left.width, height)
      left beside this beside right
    }
  }
 
  def heighten(h: Int): Element = {
    if(h <= height) this
    else {
      val top = elem(' ', width, (h - height) / 2)
      var bot = elem(' ', width, h - height - top.height)
      top above this above bot
    }
  }
 
  override def toString = contents mkString "\n"
}
10.15 한데 모아 시험해 보기
package study3

import factory.Element._

object Spiral {

  val space = elem(" ")
  val corner = elem("+")
 
  def spiral(nEdges: Int, direction: Int): Element = {
    if(nEdges == 1)
      elem("+")
    else {
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
      def verticalBar = elem('|', 1, sp.height)
      def horizontalBar = elem('-', sp.width, 1)
     
      if(direction == 0)
        (corner beside horizontalBar) above (sp beside space)
      else if(direction == 1)
        (sp above space) beside (corner above verticalBar)
      else if(direction == 2)
        (space beside sp) above (horizontalBar beside corner)
      else
        (verticalBar above corner) beside (space above sp)
    }
  }
 
  def main(args: Array[String]) {
    val nSides = 5
    println(spiral(nSides, 0))
  }
}

11장 스칼라의 계층구조

https://www.safaribooksonline.com/library/view/Scala+Cookbook/9781449340292/ch05s03.html

http://scala-exercises.47deg.com/koans#parentclasses

https://www.scala-academy.com/


http://www.scala-lang.org/documentation/





Posted by gt1000

블로그 이미지
gt1000

태그목록

공지사항

어제
오늘

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함