Skip to content

Instantly share code, notes, and snippets.

@s2kw
Last active August 29, 2015 13:55
Show Gist options
  • Save s2kw/8713054 to your computer and use it in GitHub Desktop.
Save s2kw/8713054 to your computer and use it in GitHub Desktop.
継承の時はメンバの初期化に注意。
using System;
using System.Text;
static void Main (string[] args)
{
StringBuilder sb = new StringBuilder ();
B b = new B ();
sb.AppendLine ("B:" + b.NAME);
sb.AppendLine ("A:" + (new SomethingA(b)).str);
D d = new D ();
sb.AppendLine ("D:" + d.NAME);
sb.AppendLine ("C:" + (new SomethingC(d)).str);
Console.WriteLine( sb.ToString() );
F f = new F ();
sb.AppendLine ("F:" + f.NAME);
sb.AppendLine ("E:" + (new SomethingF(f)).str);
}
public class SomethingA{
public string str;
public SomethingA(A a){this.str = a.NAME;}
}
public class SomethingC{
public string str;
public SomethingC(C a){this.str = a.NAME;}
}
public class SomethingF{
public string str;
public SomethingF(E e){this.str = e.NAME;}
}
public abstract class A{
public string NAME;
public class Window{};
}
public class B : A
{
public string NAME = "B";
public class Window{
/* hogehoge*/
}
}
public abstract class C{
public abstract string NAME{get;}
public class Window{};
}
public class D : C
{
public override string NAME{get{return "D";}}
public class Window{
/* hogehoge*/
}
}
public abstract class E{
public string NAME;
public E(){this.NAME = "E";}
public class Window{};
}
public class F : E
{
public string NAME = "F";
public F(){base.NAME = "G";}
public class Window{
/* hogehoge*/
}
}
}
/*
結果
B:B
A:
D:D
C:D
F:G
E:E
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment