"트레이트"의 두 판 사이의 차이

3번째 줄: 3번째 줄:
;트레이트
;트레이트
* 클래스 기능을 확장하기 위해 사용하는 메소드들의 집합
* 클래스 기능을 확장하기 위해 사용하는 메소드들의 집합
==예시: PHP==
* 싱글턴 템플릿
<source lang="php">
// The template
trait TSingleton {
    private static $_instance = null;
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}
class FrontController {
    use TSingleton;
}
class WebSite extends SomeClass {
    use TSingleton;
}
</source>
This allows simulating aspects of multiple inheritance:
<source lang="php">
trait TBounding
{
    public $x, $y, $width, $height;
}
trait TMoveable
{
    public function moveTo($x, $y)
    {
        // ...
    }
}
trait TResizeable
{
    public function resize($newWidth, $newHeight)
    {
        // ...
    }
}
class Rectangle
{
    use TBounding, TMoveable, TResizeable;
    public function fillColor($color)
    {
        // ...
    }
}
</source>


==같이 보기==
==같이 보기==

2016년 7월 31일 (일) 17:50 판

1 개요

trait
트레이트
  • 클래스 기능을 확장하기 위해 사용하는 메소드들의 집합

2 예시: PHP

  • 싱글턴 템플릿
// The template
trait TSingleton {
    private static $_instance = null;
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}
class FrontController {
    use TSingleton;
}
class WebSite extends SomeClass {
    use TSingleton;
}

This allows simulating aspects of multiple inheritance:

trait TBounding
{
    public $x, $y, $width, $height;
}

trait TMoveable
{
    public function moveTo($x, $y)
    {
        // ...
    }
}

trait TResizeable
{
    public function resize($newWidth, $newHeight)
    {
        // ...
    }
}

class Rectangle
{
    use TBounding, TMoveable, TResizeable;

    public function fillColor($color)
    {
        // ...
    }
}

3 같이 보기

4 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}