Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

RubyLanguage/Class: Difference between revisions

From ZeroWiki
No edit summary
No edit summary
Line 18: Line 18:
* '''Include''' : 클래스가 모듈을 상속받는 것.
* '''Include''' : 클래스가 모듈을 상속받는 것.
== Namespace ==
== Namespace ==
// 1
  class Service
  class Service
  end
  end
// 2
  module Library
  module Library
   class Service
   class Service
Line 27: Line 30:


위의 두 가지는 다음의 차이가 있다
위의 두 가지는 다음의 차이가 있다
class Service
1은 다음과 같이 사용할 수 있다.
end
  Service
다음과 같이 사용할 수 있다.
또는
  module Library
  ::Service
  class Service
2 는 다음과 같다.
  end
  end
는 다음과 같다.
  Library::Service
  Library::Service



Revision as of 09:44, 19 July 2016

[[pagelist(^RubyLanguage)]]

클래스

변수와 상수

모듈

  • 인스턴스화 할 수 없는 클래스
  • Class 클래스는 Module 클래스의 서브 클래스
    • 클래스 = 모듈 + 인스턴스화 능력
  • 기능
    • Mix-in
    • Namespace

Mix-in

  • 제한된 다중상속
    • Ruby는 단일상속만을 지원한다.
    • 따라서 클래스는 하나만 상속할 수 있다.
    • 그러나 모듈은 여러개를 상속받을 수 있다.
  • Include : 클래스가 모듈을 상속받는 것.

Namespace

// 1
class Service
end

// 2
module Library
  class Service
  end
end
  • 앞의 Service 클래스는 최상위 레벨의 namespace에 속해 있고, 뒤의 Service 클래스는 Library 모듈에 속해 있다.

위의 두 가지는 다음의 차이가 있다 1은 다음과 같이 사용할 수 있다.

Service

또는

::Service

2 는 다음과 같다.

Library::Service