白带多是什么原因| 阴道长什么样子| 发扬什么精神| 早上起床吐痰带血是什么原因| 碱性磷酸酶偏高说明什么问题| 飞蚊症是什么原因引起的| 1977属什么| 心存芥蒂是什么意思| 赵云的马叫什么| 吃猪肝补什么| salsa什么意思| 肌电图主要检查什么病| 难缠是什么意思| 不什么为什么| 食管裂孔疝是什么原因造成的| loewe是什么牌子| 什么是碳水化合物食物| 是什么药| 69是什么意思| 颈椎轻度退行性变是什么意思| 背疼应该挂什么科| 发蒙是什么意思| 什么是制片人| 阴道有腥臭味用什么药| 代谢慢是什么原因引起的| 一日无书下一句是什么| 晚上睡觉出汗是什么原因| 刮宫和清宫有什么区别| 肺癌靶向治疗是什么意思| 家里养什么宠物好| 石灰的主要成分是什么| 针灸后需要注意什么| 四库是指什么| 马铃薯是什么| 根基是什么意思| 人皇是什么意思| 着床出血是什么样的| 骨质疏松吃什么好| 百日咳是什么意思| 腰椎间盘突出压迫神经吃什么药| 老年人全身无力是什么原因| 医联体是什么意思| 风加具念什么| 出殡下雨是什么兆头| 什么炒菜好吃| 吃什么除体内湿气最快| stella是什么意思| 肠易激综合征吃什么中成药| 耳鸣是什么原因引起的| 小腹左侧疼是什么原因| 梦见纸钱是什么预兆| 金鱼可以吃什么| 晟读什么| 秋黄瓜什么时候种| 常州冬至吃什么| 秒了是什么意思| 米娜桑是什么意思| 新五行属什么| 考科目二紧张吃什么药| 装修属于什么行业| 送妈妈什么礼物好| 巴氏征阳性是什么意思| 骨折吃什么药恢复快| 脚腕酸是什么原因| 打鸡血是什么意思| 藏医最擅长治什么病| 发质硬适合什么发型| 脸上长痤疮用什么药| 月经不规律是什么原因| 为什么医生都不体检| 什么的大圆盘| 黄金果是什么水果| 吃杨梅有什么好处和功效| mlb中文叫什么| 为什么月经会提前来| 沙僧属什么生肖| 上尉是什么级别| 2157是什么意思| 钧五行属什么| 腰两侧疼痛是什么原因| 1964年属什么生肖| 鸟进屋有什么预兆| 什么的果实| 什么原因引起血糖高| 呕吐吃什么药| 12583是什么电话| 我国的国球是什么球| 安全生产职责是什么| 籍贯是指什么| 赭是什么颜色| 做馒头用什么面粉好| 不将就什么意思| 儿童吃什么| 复刻是什么意思| 人各有命是什么意思| 膝关节退行性改变是什么意思| 西瓜什么时候种植| 做什么能快速赚钱| 进贡是什么意思| 盐冻虾是什么意思| 半夜会醒是什么原因| 吾儿是什么意思| 拔完牙吃什么消炎药| 多吃鱼有什么好处| 眼睛不能见光是什么病| 血糖高适合吃什么食物| 梅菌是什么病| 吃什么升血小板最快最好| 腰肌劳损用什么药最好| 什么叫双规| 6月16号是什么星座| 前列腺是什么东西| 胆汁反流是什么原因| 覃读什么| 正剧是什么意思| 雾化主要治疗什么| 脾胃虚吃什么好| gmv是什么意思| 心肌病吃什么药| eeg是什么意思| 希鲮鱼是什么鱼| 气什么意思| 晚上尿多是什么原因| 破屋坏垣适合干什么| 大驿土命是什么意思| 11.10是什么星座| 梦见摘水果是什么意思| 放屁太臭是什么原因| 火药是什么时候发明的| 履约是什么意思| 脑死亡是什么原因引起的| 阴虱用什么药治疗| 殊途同归是什么意思| 迷惑是什么意思| 马拉车是什么牌子的包| 从小一起长大的姐妹叫什么| 来月经吃什么| 陈醋和香醋有什么区别| 黑枸杞和什么一起泡水喝比较好| 熬夜有什么坏处| 喉咙干咳嗽是什么原因| 11月1号是什么星座| 水痘吃什么药| 母亲节送给妈妈什么礼物好| 尚清是什么意思| 拉肚子吃什么| 吞咽困难是什么原因| 小孩嘴臭是什么原因| 验孕棒阳性代表什么| 婴幼儿积食会有什么症状| moco是什么牌子| rv医学上是什么意思| 什么节气开始凉快| 屈原是什么朝代| 肠胃不好吃什么| 肝不好吃什么药| 手指关节发黑是什么原因| 引火下行是什么意思| 地软有什么功效和作用| 玛丽珍鞋是什么意思| 吃牛肉不能吃什么| 进入icu病房意味着什么| 现在的节气是什么| 转氨酶高吃什么| 副巡视员是什么级别| 牛皮癣用什么药| 龟头有点痒擦什么药| 影子虫咬伤后用什么药| 福星贵人是什么意思| 身上长红色的痣是什么原因| 下巴脱臼是什么感觉| 梦到做饭是什么意思| 肝在什么位置| 梦到鬼是什么意思| 社科院是干什么的| 梨子什么季节成熟| 夜晚尿频尿多是什么原因| 小茴香是什么| 出品是什么意思| 蜜蜂蜇人后为什么会死去| 一张纸可以折什么| 山药有什么营养| 什么鱼最好养不容易死| 单数是什么| 经常早上肚子疼是什么原因| 1.4是什么星座| 阿西吧什么意思| 学字五行属什么| 2月23号是什么星座| 骞字五行属什么| 痛风频繁发作说明什么| 筋膜炎是什么症状| 月经来了同房会导致什么后果| 吃什么菜能降血糖| 三撇读什么| 什么时候着床| 红绿色盲是什么遗传| 手麻木是什么原因| 手指关节疼是什么原因| 579是什么意思| 龙井茶属于什么茶| 什么属于轻微糖尿病| 5月7号是什么星座| 菱角是什么| 腰胀是什么原因引起的| 前胸后背长痘痘是什么原因| bmi指数是什么意思| 舌苔又白又厚是什么原因| 盖世英雄是什么意思| 十二生肖排第七是什么生肖| 什么事每人每天都必须认真的做| 女人补铁有什么好处| 耳朵外面痒是什么原因| 什么情况下月经推迟| 收尿干什么用的| 甲状腺低回声结节是什么意思| 功名利禄是什么意思| 男生圆脸适合什么发型| 人流后吃什么水果| 减肥吃什么米| 牛奶不能和什么一起吃| 后生是什么意思| 小鸡啄米什么意思| 木星是什么颜色| 对等是什么意思| 养生馆起什么名字好| 生殖激素常规检查是查什么的| 3.8什么星座| 疹子长什么样| 脑梗塞什么症状| 拉屎臭是什么原因| 七月十六号是什么星座| 小儿便秘吃什么药| 大便少是什么原因| 水光是什么| 男羊配什么属相最好| 魔性是什么意思| 腰椎间盘突出不能吃什么食物| 乳腺癌吃什么好| 梦到自己的妈妈死了是什么意思| 梦见抬棺材是什么意思| 1992属什么| 增加白细胞吃什么食物最好| 什么是水印| 2e是什么意思| 12月9号是什么星座| 肾内科是看什么病的| 小孩咬人是什么原因| 空腹胰岛素高是什么原因| 降甘油三酯吃什么食物最好| 什么牌子的手机好| 吃藕粉对身体有什么好处| 1981年什么命| 肾阴虚吃什么食物最好| 怀孕喝酒会有什么反应| 口臭是什么原因造成的| 壁虎是什么动物| 血压高是什么引起的| 囊肿是什么病严重吗| 什么是蛇缠腰病| 电波系是什么意思| glu是什么氨基酸| 手上长水泡是什么原因| 过期的钙片有什么用途| 百度Jump to content

欢乐谷--北京频道--人民网

From Wikipedia, the free encyclopedia
百度 中国统一战线新闻网北京6月27日电 (记者闫妍)6月19日至6月23日,中央统战部第一期网络人士理论研讨班在重庆市举办,来自全国各地的48名网络代表人士参加了学习交流。

In computer programming, a trait is a language concept that represents a set of methods that can be used to extend the functionality of a class.[1][2]

Rationale

[edit]

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 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 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.

Traits solve these problems by allowing classes to use the trait and get the desired behavior. If a class uses more than one trait, the order in which the traits are used does not matter. The methods provided by the traits have direct access to the data of the class.

Characteristics

[edit]

Traits combine aspects of protocols (interfaces) and mixins. Like an interface, a trait defines one or more method signatures, of which implementing classes must provide implementations. Like a mixin, a trait provides additional behavior for the implementing class.

In case of a naming collision between methods provided by different traits, the programmer must explicitly disambiguate which one of those methods will be used in the class; thus manually solving the diamond problem of multiple inheritance. This is different from other composition methods in object-oriented programming, where conflicting names are automatically resolved by scoping rules.

Operations which can be performed with traits include:[3][4]

  • symmetric sum: an operation that merges two disjoint traits to create a new trait
  • override (or asymmetric sum): an operation that forms a new trait by adding methods to an existing trait, possibly overriding some of its methods
  • alias: an operation that creates a new trait by adding a new name for an existing method
  • exclusion: an operation that forms a new trait by removing a method from an existing trait. (Combining this with the alias operation yields a shallow rename operation).

If a method is excluded from a trait, that method must be provided by the class that consumes the trait, or by a parent class of that class. This is because the methods provided by the trait might call the excluded method.

Trait composition is commutative (i.e. given traits A and B, A + B is equivalent to B + A) and associative (i.e. given traits A, B, and C, (A + B) + C is equivalent to A + (B + C)).[1]

Limitations

[edit]

While traits offer significant advantages over many alternatives, they do have their own limitations.

Required methods

[edit]

If a trait requires the consuming class to provide certain methods, the trait cannot know if those methods are semantically equivalent to the trait's needs. For some dynamic languages, such as Perl, the required method can only be identified by a method name, not a full method signature, making it harder to guarantee that the required method is appropriate.

Excluding methods

[edit]

If a method is excluded from a trait, that method becomes a 'required' method for the trait because the trait's other methods might call it.

Supported languages

[edit]

Traits come originally from the programming language Self[5] and are supported by the following programming languages:

  • AmbientTalk: Combines the properties of Self traits (object-based multiple inheritance) and Smalltalk's Squeak traits (requiring explicit composition of traits by the programmer). It builds on the research on stateful and freezable traits to enable state within traits, which was not allowed in the first definitions.[6]
  • C#: Since version 8.0, C# has support for default interface methods,[7] which have some properties of traits.[8]
  • C++: Used in Standard Template Library and the C++ Standard Library to support generic container classes[9][10] and in the Boost TypeTraits library.[11]
  • Curl: Abstract classes as mixins permit method implementations and thus constitute traits by another name.[citation needed]
  • Fortress[12]
  • Groovy: Since version 2.3[13]
  • Haskell: In Haskell, Traits are known as Type classes.
  • Haxe: Since version 2.4.0.[14] Called Static Extension[15] in the manual, it uses using keyword
  • Java: Since version 8, Java has support for default methods,[16] which have some properties of traits.[17][18][19][20]
  • JavaScript: Traits can be implemented via functions and delegations[21] or through libraries that provide traits.[22][23][24]
  • Julia: Several packages implement traits, e.g.,[25]
  • Kotlin: Traits have been called interfaces[26] since M12.[27]
  • Lasso[28]
  • Mojo: Since version 0.6.0[29]
  • OCaml: Traits can be implemented using a variety of language features: module and module type inclusion, functors and functor types, class and class type inheritance, et cetera.
  • Perl: Called roles, they are implemented in Perl libraries such as Moose, Role::Tiny and Role::Basic. Roles are part of the sister language Raku. [30] With the acceptance of the Corinna OOP Proposal[31] Perl will have roles native to the language as part of a modern OOP system.
  • PHP: Since version 5.4,[32][33] PHP allows users to specify templates that provide the ability to "inherit" from more than one (trait-)class, as a pseudo multiple inheritance.
  • Python: Via a third-party library,[34][35] or via higher-order mixin classes[36]
  • Racket: Supports traits as a library and uses macros, structures, and first-class classes to implement them.[37]
  • Ruby: Module mixins can be used to implement traits.[38]
  • Rust[39]
  • Scala[40][41] trait is builtin supported with the key word trait.
  • Smalltalk: Traits are implemented in two dialects of Smalltalk, Squeak[1] and Pharo.[42]
  • Swift: Traits can be implemented with protocol extensions.[43]

Examples

[edit]

C#

[edit]

On C# 8.0, it is possible to define an implementation as a member of an interface.

using System;

namespace CSharp8NewFeatures;

interface ILogger
{
    // Traditional interface methods
    void Log(string message);
    void LogError(Exception exception);

    // Default interface method
    void LogWarning(string message)
    {
        Console.WriteLine(message);
    }        
}

class Logger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine(message);
    }

    public void LogError(Exception exception)
    {
        Console.WriteLine(exception.ToString());
    }
}

class Program
{
    static void Main(string[] args)
    {
        ILogger logger = new Logger();

        logger.LogWarning("Some warning message");
    }
}

PHP

[edit]

This example uses a trait to enhance other classes:

// The template
trait TSingleton
{
    private static $_instance = null;

    private function __construct() {} // Must have private default constructor and be aware not to open it in the class

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

class FrontController
{
    use TSingleton;
}

// Can also be used in already extended classes
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)
    {
        // …
    }
}

Rust

[edit]

A trait in Rust declares a set of methods that a type must implement.[44] Rust compilers require traits to be explicated, which ensures the safety of generics in Rust.

// type T must have the "Ord" trait
// so that ">" and "<" operations can be done
fn max<T: Ord>(a: &[T]) -> Option<&T> {
    let mut result = a.first()?;
    for n in a {
        if *n > *result {
            result = &n;
        }
    }
    Some(result)
}

To simplify tedious and repeated implementation of traits like Debug and Ord, the derive macro can be used to request compilers to generate certain implementations automatically.[45] Derivable traits include: Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord and Hash.

See also

[edit]

References

[edit]
  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. Vol. 2743. Springer. pp. 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) (Report). University of Chicago. Archived (PDF) from the original on May 17, 2004.
  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. 9 September 2019. 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. ^ Steele, Guy; Maessen, Jan-Willem (June 11, 2006). "Fortress Programming Language Tutorial" (PDF). Sun Microsystems. Retrieved January 23, 2016.
  13. ^ "Object Orientation: Traits". The Groovy Programming Language. Retrieved January 23, 2016.
  14. ^ "Haxe 2.4.0 - Haxe - The Cross-platform Toolkit". Haxe - The Cross-platform Toolkit. Retrieved 2025-08-06.
  15. ^ "Manual - Haxe - The Cross-platform Toolkit". Haxe - The Cross-platform Toolkit. Retrieved 2025-08-06.
  16. ^ "Default Methods". The Java Tutorials. Oracle. Retrieved January 23, 2016.
  17. ^ 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.
  18. ^ 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.
  19. ^ 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.
  20. ^ 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.
  21. ^ Seliger, Peter (April 11, 2014). "The Many Talents of JavaScript". Retrieved January 23, 2015.
  22. ^ "Traits.js: Traits for JavaScript". Retrieved January 23, 2016.
  23. ^ Van Cutsem, Tom; Miller, Mark S. (2012). "Robust Trait Composition for Javascript" (PDF). Science of Computer Programming. Retrieved January 23, 2016.
  24. ^ "CocktailJS". Retrieved January 23, 2016.
  25. ^ Mauro Werder. "SimpleTraits.jl". GitHub. Retrieved March 23, 2017.
  26. ^ "Interfaces". Kotlin Reference. JetBrains. Retrieved January 23, 2016.
  27. ^ Breslav, Andrey (May 29, 2015). "Kotlin M12 is out!". Kotlin Blog. JetBrains. Retrieved January 23, 2016.
  28. ^ "Traits". Lasso Language Guide. LassoSoft. January 6, 2014. Retrieved January 23, 2016.
  29. ^ "Modular Docs - Mojo?? changelog". docs.modular.com. Retrieved 2025-08-06.
  30. ^ chromatic (April 30, 2009). "The Why of Perl Roles". Retrieved January 23, 2016.
  31. ^ Curtis "Ovid" Poe. "Corinna OOP Proposal". Corinna RFC. Retrieved September 30, 2022.
  32. ^ "Traits". PHP Documentation. The PHP Group. Retrieved January 23, 2016.
  33. ^ Marr, Stefan (January 9, 2011). "Request for Comments: Horizontal Reuse for PHP". PHP.net wiki. The PHP Group. Retrieved January 31, 2011.
  34. ^ Per?, Teppo. "py3traits Documentation". Retrieved January 23, 2016.
  35. ^ Per?, Teppo (2025-08-06). "py2traits". GitHub. Retrieved January 23, 2016.
  36. ^ "Higher Order Mixin Classes". Archived from the original on 2025-08-06.
  37. ^ "Traits". The Racket Reference. Retrieved January 23, 2016.
  38. ^ David Naseby (February 14, 2004). "Traits in Ruby". Ruby Naseby. Retrieved January 23, 2016.
  39. ^ "Traits". The Rust Programming Language. Retrieved September 30, 2019.
  40. ^ "Traits". A Tour of Scala. école polytechnique fédérale de Lausanne. Retrieved January 23, 2016.
  41. ^ Neward, Ted (April 29, 2008). "The busy Java developer's guide to Scala: Of traits and behaviors". IBM developerWorks. IBM. Retrieved January 23, 2016.
  42. ^ "Traits in 10 minutes". Pharo: The CollaborActive Book. Retrieved January 23, 2016.
  43. ^ Hollemans, Matthijs (July 22, 2015). "Mixins and Traits in Swift 2.0". Retrieved January 23, 2016.
  44. ^ "Traits - Introduction to Programming Using Rust". Archived from the original on 2025-08-06.
  45. ^ "Traits - the Rust Programming Language".
[edit]
盐酸二甲双胍缓释片什么时候吃 酪朊酸钠是什么 脖子长痘痘是因为什么原因 nibpdia过高是什么意思 ns是什么单位
老年人心跳过快是什么原因 意见是什么意思 知心朋友是什么意思 女人一般什么时候容易怀孕 指桑骂槐是什么生肖
男性尿道炎吃什么药 血压高吃什么 梦到和婆婆吵架是什么意思 狗狗为什么会咬人 肺部纤维化是什么意思
头孢过敏什么症状 吃什么药能让月经马上来 什么水果含糖量最低 排骨炖什么比较好吃 出汗对身体有什么好处
肛门瘙痒用什么药膏好hcv9jop3ns1r.cn 银屑病吃什么药hcv7jop9ns9r.cn 膝关节疼痛挂什么科hcv7jop9ns1r.cn 右眼皮跳是什么预兆女hcv8jop3ns8r.cn 经常玩手机有什么危害xianpinbao.com
小孩为什么吃果糖二磷酸钠hcv9jop6ns5r.cn 重庆有什么特产hcv7jop6ns0r.cn 脚气挂什么科室hcv8jop9ns2r.cn cas号是什么意思hcv9jop0ns7r.cn 驴肉不能和什么一起吃hcv8jop5ns9r.cn
我看见了什么hcv8jop3ns7r.cn 野兽之王是什么动物hcv9jop4ns5r.cn 右肋骨疼是什么原因hcv9jop4ns4r.cn mra是什么检查520myf.com 月经几个月不来是什么原因hcv7jop4ns8r.cn
声音的高低叫什么hcv7jop7ns3r.cn 金骏眉是什么茶类0735v.com 月月红是什么花hcv9jop5ns9r.cn 一直打嗝是什么原因luyiluode.com 男人小腹疼痛是什么原因jinxinzhichuang.com
百度