Content deleted Content added
m →Limitations: sentence-case subsection names |
m promote {{unreferenced section}} -> article-wide {{more citations needed}} |
||
Line 1:
{{Short description|Concept used in object-oriented programming}}
{{Technical|date=March 2012}}
In [[computer programming]], a '''trait''' is a concept used in [[object-oriented programming]] which represents a set of [[Method (computer programming)|methods]] that can be used to extend the functionality of a [[Class (computer science)|class]].<ref name="schaerli-ecoop-2003">{{cite journal | first1=Nathanael | last1=Schärli | first2=Stéphane | last2=Ducasse | first3=Oscar | last3=Nierstrasz | author-link3=Oscar Nierstrasz | first4=Andrew P. | last4=Black | url=http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf | title=Traits: Composable Units of Behaviour | journal=Proceedings of the European Conference on Object-Oriented Programming (ECOOP). | series=Lecture Notes in Computer Science | volume=2743 | year=2003 | pages=248–274 |publisher=Springer |doi=10.1007/978-3-540-45070-2_12 |isbn=978-3-540-45070-2 |citeseerx=10.1.1.1011.8}}</ref><ref>{{cite journal | first1=Stéphane | last1=Ducasse | first2=Oscar | last2=Nierstrasz | first3=Nathanael | last3=Schärli | first4=Roel | last4=Wuyts | first5=Andrew P. | last5=Black | title=Traits: A mechanism for fine-grained reuse. | journal= ACM Transactions on Programming Languages and Systems | volume=28 | issue=2 | pages=331–388 | date=March 2006 | doi=10.1145/1119479.1119483 |citeseerx=10.1.1.64.2480| s2cid=16434119 }}</ref>
==Rationale==
▲{{unreferenced section|date=November 2022}}
In object-oriented programming, behavior is sometimes shared between classes which are not related to each other. For example, many unrelated classes may have methods to [[Serialization|serialize]] objects to [[JSON]]. Historically, there have been several approaches to solve this without duplicating the code in every class needing the behavior. Other approaches include [[multiple inheritance]] and [[mixin|mixins]], but these have drawbacks: the behavior of the code may unexpectedly change if the order in which the mixins are applied is altered, or if new methods are added to the parent classes or mixins.
|