ソースコード:X.ToString()

(メソッド set.__str__/frozenset.__str__ に相当する)メソッド ToString の定義を含む、ソースコードの断片を次に示します。


# IronPython-1.1.2/Src/IronPython/Runtime/Set.cs
namespace IronPython.Runtime {

/// Common interface shared by both Set and FrozenSet
internal interface ISet : IEnumerable {
...

/// Mutable set class
[PythonType("set")]
public class SetCollection : ISet, ...
Dict items;
...
#region Object overrides
[PythonName("__str__")]
public override string ToString() {
return (SetHelpers.SetToString(this, this.items.Keys));
}

/// Non-mutable set class
[PythonType("frozenset")]
public class FrozenSetCollection : ISet, ...
Dict items;
int hashCode;
...
#region Object overrides
[PythonName("__str__")]
public override string ToString() {
return (SetHelpers.SetToString(this, this.items.Keys));
}

集合を表現するクラスは、変更可能な SetCollection と、変更不能な FrozenSetCollection とに分かれます。インターフェース ISet は、これらに共通するプロトコルを規定します。まず、クラス SetCollection のヘッダーを見ると、

    /// Mutable set class
[PythonType("set")]
public class SetCollection : ISet, ...

組み込み型 set を実現しているのが分かります。次に、クラス FrozenSetCollection のヘッダーを見ると、

    /// Non-mutable set class
[PythonType("frozenset")]
public class FrozenSetCollection : ISet, ...

組み込み型 frozenset を実現しているのが分かります。さらに、メソッド ToString のヘッダーを見ると、次のように、

    #region Object overrides
[PythonName("__str__")]

それぞれが set.__str__/frozenset.__str__ に相当するのが分かります。そして、コメントを見ると、この後に続くメソッド群が、クラス Object にあるものを再定義 overrides したものだと分かります。
メソッド ToString の本体では、次のように、

    return (SetHelpers.SetToString(this, this.items.Keys));

実引数に、this を指定しています。これは(共有する操作を実現する)静的クラス SetHelpers に、自身 this の処理を委ねるためです。このとき(SetCollection/FrozenSetCollection のインスタンス)this は、インターフェース ISet の規定に従うものとします。


Previous|2/3|Next