특성(컴퓨터 프로그래밍)

Trait (computer programming)

컴퓨터 프로그래밍에서 특성[1][2]객체 지향 프로그래밍에서 사용되는 개념으로 클래스의 기능을 확장하는 데 사용할 수 있는 일련의 메서드를 나타냅니다.

특성.

특성은 둘 다 클래스에 동작을 구현하는 메서드 집합을 제공하며 클래스가 제공된 동작을 매개 변수화하는 메서드 집합을 구현해야 합니다.

객체 간 통신의 경우 특성은 객체 지향 프로토콜(인터페이스)믹스인 사이의 어딘가에 있습니다.인터페이스는 메서드 서명을 통해 하나 이상의 동작을 정의할 수 있으며, 특성은 완전한 메서드 정의를 통해 동작을 정의할 수 있습니다. 즉, 메서드 본문을 포함합니다.반면 믹스인은 완전한 메서드 정의를 포함하고 멤버 변수를 통해 상태를 전달할 수도 있지만 특성은 그렇지 않습니다.

따라서 특성으로 정의된 객체는 메서드의 구성으로 생성되며, 여러 상속 없이 다른 클래스에서 사용할 수 있습니다.명명 충돌의 경우, 클래스가 사용하는 복수의 특성이 같은 이름의 메서드를 가지고 있는 경우, 프로그래머는 클래스에서 사용되는 메서드 중 하나를 명시적으로 명확히 하여 다중 상속의 다이아몬드 문제를 수동으로 해결해야 한다.이는 충돌하는 이름이 범위 지정 규칙에 의해 자동으로 해결되는 객체 지향 프로그래밍의 다른 구성 방법과는 다릅니다.

혼합은 상속 작업을 통해서만 구성할 수 있지만 특성은 다음과 [3][4]같은 훨씬 더 광범위한 작업 선택권을 제공합니다.

  • 대칭합: 두 개의 분리된 특성을 결합하여 새로운 특성을 만드는 연산
  • 오버라이드(또는 비대칭 합계): 기존 특성에 메서드를 추가하여 새로운 특성을 형성하는 연산, 아마도 메서드의 일부를 오버라이드할 수 있다.
  • alias: 기존 메서드의 새 이름을 추가하여 새 특성을 만드는 작업
  • 제외: 기존 특성에서 메서드를 제거하여 새로운 특성을 형성하는 작업입니다.(이를 에일리어스 조작과 조합하면 얕은 이름 변경 조작이 발생합니다).

특성은 다음과 같은 방법으로 구성됩니다.

  • 특성 구성은 가환성이므로 특성을 추가하는 순서는 중요하지 않습니다.예를 들어 특성 S = A + B가 주어지면 특성 T = B + A는 S와 같습니다.
  • 모순되는 메서드는 구성에서 제외됩니다.
  • 중첩된 특성은 평평한 특성과 동일하며 구성 계층은 특성 행동에 영향을 미치지 않습니다.예를 들어 특성 S = A + X가 주어진 경우, 여기서 X = B + C특성 T = A + B + C[1]같습니다.

지원되는 언어

특성은 원래 프로그래밍 언어[5] 자체에서 비롯되었으며 다음 프로그래밍 언어에서 지원됩니다.

  • AmbientTalk: Self 특성(객체 기반 다중 상속)과 Smalltalk의 Squak 특성(프로그래머의 특성 명시적 구성 필요)을 결합합니다.그것은 첫 번째 [6]정의에서는 허용되지 않았던 특성 내의 상태를 가능하게 하기 위해 스테이트풀하고 프리즈 가능한 특성에 대한 연구를 기반으로 한다.
  • C#: 버전 8.0 이후 C#에서는 [8]기본 인터페이스 [7]메서드가 지원되고 있습니다.이러한 메서드에는 몇 가지 특성이 있습니다.
  • C++: Standard Template Library 및 C++ Standard Library에서 일반 컨테이너[9][10] 클래스를 지원하기 위해 사용되며 Boost TypeTraits [11]라이브러리에서 사용됩니다.
  • : 믹스인으로서의 추상 클래스는 메서드 구현을 허용하므로 다른 [citation needed]이름으로 특성을 구성합니다.
  • D: 버전 2.003 이후 __traits 언어[12] 확장 및 std.traits[13] 모듈 도우미 템플릿은 컴파일 시 특성을 제공합니다.다른 언어 기능(특히 템플릿 및 믹스인)과 함께 인터페이스 및 유형에 따라 유연한 메서드 자동 생성을 가능하게 합니다.또한 D는 여러 [14]멤버클래스로의 전송을 포함하여 멤버 메서드 및 변수의 명시적인 에일리어스를 허용합니다.
  • 포트리스[15]
  • 그루비: 버전 2.3 이후[16]
  • Haskell: Haskell에서 특성은 유형 클래스로 알려져 있습니다.
  • Haxe: 버전 2.4.[17]0 이후 매뉴얼에서 정적[18] 확장으로 호출된 이후using키워드
  • Java: 버전 8 이후 Java는 [20][21][22][23]일부 특성 속성을 가진 기본 [19]메서드를 지원합니다.
  • JavaScript:특성은 기능 및 위임[24] 또는 [25][26][27]특성을 제공하는 라이브러리를 통해 구현할 수 있습니다.
  • 줄리아: 몇몇 패키지는 특성을 구현합니다. 예를 들어,[28]
  • Kotlin:특징은 [30]M12부터 인터페이스라고[29] 불리고 있습니다.
  • 라소[31]
  • OCaml: 특성은 모듈 및 모듈 유형 포함, 펑터와 펑터 유형, 클래스 및 클래스 유형 상속 등 다양한 언어 기능을 사용하여 구현할 수 있습니다.
  • Perl: 호출된 역할. 무스 등의 Perl 라이브러리에 구현됩니다.역할:소형 및 역할:: 기본. 역할은 자매 언어[32]라쿠의 일부입니다.
  • PHP: 버전 5.[33][34]4 이후, PHP에서는, 복수의 (trait-) 클래스에서 「상속」하는 기능을 제공하는 템플릿을 의사 다중 상속으로서 지정할 수 있습니다.
  • Python: 서드파티 라이브러리 [35][36]또는 고차 믹스인[37] 클래스 경유
  • Racket: 라이브러리로서의 특성을 지원하며 매크로, 구조체 및 퍼스트 클래스 등을 사용하여 [38]구현합니다.
  • Ruby: 모듈 믹스인을 사용하여 [39]특성을 구현할 수 있습니다.
  • [40]
  • 키워드[41][42] 지원되는 Scala 특성 내장trait.
  • 스몰토크:특성[1] 스퀵[43]파로의 두 가지 방언으로 구현됩니다.
  • Swift: 특성을 프로토콜 [44]확장으로 구현할 수 있습니다.

C#

C# 8.0 에서는, 실장을 인터페이스의 멤버로서 정의할 수 있습니다.

사용. 시스템.;  네임스페이스 CSharp8 신기능;  인터페이스 로거 {     // 기존 인터페이스 방식     무효 로그.(스트링 메세지);     무효 로그 오류(예외. 예외.);      // 기본 인터페이스 방식     무효 로그 경고(스트링 메세지)     {         콘솔.기입선(메세지);     }         }  학급 로거 : 로거 {     일반의 무효 로그.(스트링 메세지)     {         콘솔.기입선(메세지);     }      일반의 무효 로그 오류(예외. 예외.)     {         콘솔.기입선(예외..ToString(ToString)());     } }  학급 프로그램. {     정적인 무효 주된(스트링[] args)     {         로거 로거 = 신규 로거();          로거.로그 경고("일부 경고 메시지");     } } 

PHP

다음 예제에서는 특성을 사용하여 다른 클래스를 강화합니다.

// 템플릿 특성 징글턴 {     사적인 정적인 $_140 = 무효;      사적인 기능. __개요() {} // 개인 기본 생성자가 있어야 하며 클래스에서 열지 않도록 주의하십시오.      일반의 정적인 기능. get Instance(인스턴스()     {         한다면 (무효 === 자신::$_140) {             자신::$_140 = 신규 자신();         }          돌아가다 자신::$_140;     } }  학급 프론트 컨트롤러 {     사용하다 징글턴; }  // 이미 확장된 클래스에서도 사용할 수 있습니다. 학급 웹 사이트 확장 썸클래스 {     사용하다 징글턴; } 

이를 통해 여러 상속의 측면을 시뮬레이션할 수 있습니다.

특성 TBounding(TBunding) {     일반의 x달러, $y, , 높이; }  특성 TMoveable(TMoveable) {     일반의 기능. 이동처(x달러, $y)     {         // …     } }  특성 트리사이징 가능 {     일반의 기능. 크기 조정($newWidth, $신규격)     {         // …     } }  학급 직사각형 {     사용하다 TBounding(TBunding), TMoveable(TMoveable), 트리사이징 가능;      일반의 기능. 필컬러($color)     {         // …     } } 

Rust의 특성은 유형이 [45]구현해야 하는 메서드 집합을 선언합니다.러스트 컴파일러는 특성을 설명해야 하므로 러스트 내 제네릭의 안전성이 보장됩니다.

// ">"  "<" 연산fn max <T: Ord>(a: &[T]) -> 옵션 <&>을 실행하려면 Type T에는 "Ord" 특성 // 가 필요합니다.T> { mut result = a . first ( ) ; { if * n > * result { result = & n ; } 일부(결과) }

번거롭고 반복적인 특성 구현을 단순화합니다.Debug그리고.Ord,그derive매크로를 사용하여 컴파일러가 특정 구현을 자동으로 [46]생성하도록 요청할 수 있습니다.파생 가능한 특성은 다음과 같습니다.Clone,Copy,Debug,Default,PartialEq,Eq,PartialOrd,Ord그리고.Hash.

「 」를 참조해 주세요.

레퍼런스

  1. ^ a b c Schärli, Nathanael; Ducasse, Stéphane; Nierstrasz, Oscar; Black, Andrew P. (2003). "Traits: Composable Units of Behaviour" (PDF). Proceedings of the European Conference on Object-Oriented Programming (ECOOP). Lecture Notes in Computer Science. Springer. 2743: 248–274. CiteSeerX 10.1.1.1011.8. doi:10.1007/978-3-540-45070-2_12. ISBN 978-3-540-45070-2.
  2. ^ Ducasse, Stéphane; Nierstrasz, Oscar; Schärli, Nathanael; Wuyts, Roel; Black, Andrew P. (March 2006). "Traits: A mechanism for fine-grained reuse". ACM Transactions on Programming Languages and Systems. 28 (2): 331–388. CiteSeerX 10.1.1.64.2480. doi:10.1145/1119479.1119483. S2CID 16434119.
  3. ^ Fisher, Kathleen; Reppy, John (2003). "Statically typed traits" (PDF). University of Chicago. Archived (PDF) from the original on May 17, 2004. {{cite journal}}:Cite 저널 요구 사항 journal=(도움말)
  4. ^ Fisher, Kathleen; Reppy, John (2004). A typed calculus of traits (PDF). 11th Workshop on Foundations of Object-oriented Programming. University of Chicago.
  5. ^ Curry, Gael; Baer, Larry; Lipkie, Daniel; Lee, Bruce (1982). Traits: An approach to multiple-inheritance subclassing. SIGOA Conference on Office Information Systems. Philadelphia, Pennsylvania, USA: ACM Press. pp. 1–9. doi:10.1145/966873.806468.
  6. ^ Van Cutsem, Tom; Bergel, Alexandre; Ducasse, Stéphane; De Meuter, Wolfgang (2009). Adding State and Visibility Control to Traits Using Lexical Nesting (PDF). European Conference on Object-Oriented Programming (ECOOP 2009). Lecture Notes in Computer Science. Vol. 5653. Springer-Verlag. pp. 220–243. CiteSeerX 10.1.1.372.1265. doi:10.1007/978-3-642-03013-0_11. ISBN 978-3-642-03012-3.
  7. ^ "Default interface methods". What's new in C# 8.0. Microsoft. Retrieved November 29, 2019.
  8. ^ "Interfaces in C# 8.0 gets a makeover". Default Implementation in Interfaces in C# 8.0. Talking Dotnet. Retrieved November 29, 2019.
  9. ^ "iterator_traits<Iterator>". Standard Template Library. SGI.
  10. ^ Myers, Nathan C. (June 1995). "Traits: a new and useful template technique". C++ Report. Retrieved January 23, 2016.
  11. ^ Abrahams, David. "Generic Programming Techniques: Traits". Boost C++ Libraries. Retrieved January 23, 2016.
  12. ^ "Traits". The D Language Reference. Digital Mars. Retrieved January 23, 2016.
  13. ^ "std.traits". The D Language — Library Reference. Digital Mars. Retrieved January 23, 2016.
  14. ^ "Classes". The D Language Reference. Digital Mars. Retrieved January 23, 2016.
  15. ^ Steele, Guy; Maessen, Jan-Willem (June 11, 2006). "Fortress Programming Language Tutorial" (PDF). Sun Microsystems. Retrieved January 23, 2016.
  16. ^ "Object Orientation: Traits". The Groovy Programming Language. Retrieved January 23, 2016.
  17. ^ "Haxe 2.4.0 - Haxe - The Cross-platform Toolkit". Haxe - The Cross-platform Toolkit. Retrieved 2017-09-12.
  18. ^ "Manual - Haxe - The Cross-platform Toolkit". Haxe - The Cross-platform Toolkit. Retrieved 2017-09-12.
  19. ^ "Default Methods". The Java Tutorials. Oracle. Retrieved January 23, 2016.
  20. ^ Liquori, Luigi; Spiwack, Arnaud (2008). "FeatherTrait: A Modest Extension of Featherweight Java". ACM Transactions on Programming Languages and Systems. 30 (2): 11:1. doi:10.1145/1330017.1330022. S2CID 17231803.
  21. ^ Liquori, Luigi; Spiwack, Arnaud (2008). "Extending FeatherTrait Java with Interfaces". Theoretical Computer Science. 398 (1–3): 243–260. doi:10.1016/j.tcs.2008.01.051. S2CID 12923128.
  22. ^ Bono, Viviana; Mensa, Enrico; Naddeo, Marco (September 2014). Trait-oriented Programming in Java 8. International Conference on Principles and Practices of Programming on the Java Platform: virtual machines, languages, and tools (PPPJ ’14). pp. 181–6. CiteSeerX 10.1.1.902.161. doi:10.1145/2647508.2647520.
  23. ^ Forslund, Emil (February 3, 2016). "Definition of the Trait Pattern in Java". Age of Java. Archived from the original on August 4, 2016. Retrieved February 3, 2016.
  24. ^ Seliger, Peter (April 11, 2014). "The Many Talents of JavaScript". Retrieved January 23, 2015.
  25. ^ "Traits.js: Traits for JavaScript". Retrieved January 23, 2016.
  26. ^ Van Cutsem, Tom; Miller, Mark S. (2012). "Robust Trait Composition for Javascript" (PDF). Science of Computer Programming. Retrieved January 23, 2016.
  27. ^ "CocktailJS". Retrieved January 23, 2016.
  28. ^ mauro3. "SimpleTraits.jl". GitHub. Retrieved March 23, 2017.
  29. ^ "Interfaces". Kotlin Reference. JetBrains. Retrieved January 23, 2016.
  30. ^ Breslav, Andrey (May 29, 2015). "Kotlin M12 is out!". Kotlin Blog. JetBrains. Retrieved January 23, 2016.
  31. ^ "Traits". Lasso Language Guide. LassoSoft. January 6, 2014. Retrieved January 23, 2016.
  32. ^ chromatic (April 30, 2009). "The Why of Perl Roles". Retrieved January 23, 2016.
  33. ^ "Traits". PHP Documentation. The PHP Group. Retrieved January 23, 2016.
  34. ^ Marr, Stefan (January 9, 2011). "Request for Comments: Horizontal Reuse for PHP". PHP.net wiki. The PHP Group. Retrieved January 31, 2011.
  35. ^ Perä, Teppo. "py3traits Documentation". Retrieved January 23, 2016.
  36. ^ Perä, Teppo (2015-03-25). "py2traits". GitHub. Retrieved January 23, 2016.
  37. ^ "Higher Order Mixin Classes". Archived from the original on 2016-10-09.
  38. ^ "Traits". The Racket Reference. Retrieved January 23, 2016.
  39. ^ David Naseby (February 14, 2004). "Traits in Ruby". Ruby Naseby. Retrieved January 23, 2016.
  40. ^ "Traits". The Rust Programming Language. Retrieved September 30, 2019.
  41. ^ "Traits". A Tour of Scala. École polytechnique fédérale de Lausanne. Retrieved January 23, 2016.
  42. ^ Neward, Ted (April 29, 2008). "The busy Java developer's guide to Scala: Of traits and behaviors". IBM developerWorks. IBM. Retrieved January 23, 2016.
  43. ^ "Traits in 10 minutes". Pharo: The CollaborActive Book. Retrieved January 23, 2016.
  44. ^ Hollemans, Matthijs (July 22, 2015). "Mixins and Traits in Swift 2.0". Retrieved January 23, 2016.
  45. ^ "Traits - Introduction to Programming Using Rust".
  46. ^ "Traits - the Rust Programming Language".

외부 링크