Skip to content

Instantly share code, notes, and snippets.

@gabrielgreen
Created October 29, 2012 18:36
Show Gist options
  • Save gabrielgreen/3975581 to your computer and use it in GitHub Desktop.
Save gabrielgreen/3975581 to your computer and use it in GitHub Desktop.
Nested class examples
namespace WindowsGame1.PhysicalModel
{
public class Body : PhysicalObject
{
public Body() : this("body") { }
public Body(string name)
: base(name)
{
Torso = new torso();
}
public torso Torso { get; set; }
public class bone : PhysicalObject
{
public bone(string name) : base(name) { }
}
public class torso
{
public torso()
{
Left = new side("Left");
Right = new side("Right");
}
public side Right { get; set; }
public side Left { get; set; }
public class side
{
public side(string name)
{
Name = name;
Arm = new arm(this.Name);
}
public string Name { get; private set; }
public arm Arm { get; set; }
public class arm
{
public arm(string parentName)
{
Name = parentName + "Arm";
Hand = new hand(parentName);
}
public string Name { get; private set; }
public hand Hand { get; set; }
public class hand
{
public hand(string parentName)
{
Name = parentName + "Hand";
Wrist = new wrist();
Palm = new palm();
Fingers = new fingers(this.Name);
}
public string Name { get; private set; }
public fingers Fingers { get; set; }
public palm Palm { get; set; }
public wrist Wrist { get; set; }
public class fingers
{
public fingers(string parentName)
{
Thumb = new finger(parentName + "Thumb", false);
Index = new finger(parentName + "Index");
Middle = new finger(parentName + "Middle");
Ring = new finger(parentName + "Ring");
Small = new finger(parentName + "Small");
}
public finger Thumb;
public finger Index;
public finger Middle;
public finger Ring;
public finger Small;
public class finger
{
public finger(string parentName, bool hasMiddlePhalanx = true)
{
Phalanges = new phalanges
{
Proximal = new phalanges.phalanx(parentName + "Proximal"),
Middle = hasMiddlePhalanx ? new phalanges.phalanx(parentName + "Middle") : null,
Distal = new phalanges.phalanx(parentName + "Distal")
};
}
public phalanges Phalanges;
public class phalanges
{
public phalanx Proximal;
public phalanx Middle;
public phalanx Distal;
public class phalanx : bone
{
public phalanx(string name) : base(name + "Phalanx") { }
}
}
}
}
public class palm
{
// todo: create metacarpals
public List<metacarpal> metacarpals;
public class metacarpal : bone
{
public metacarpal(string name) : base(name) { }
}
}
public class wrist
{
// todo: ctor should create wrist bones
public List<bone> bones;
}
}
}
}
}
// Left Index Finger
public PhysicalObject LeftIndexFingerTip { get { return Torso.Left.Arm.Hand.Fingers.Index.Phalanges.Distal; } }
public PhysicalObject LeftIndexFingerMiddle { get { return Torso.Left.Arm.Hand.Fingers.Index.Phalanges.Middle; } }
public PhysicalObject LeftIndexFingerKnuckle { get { return Torso.Left.Arm.Hand.Fingers.Index.Phalanges.Proximal; } }
// Left Middle Finger
public PhysicalObject LeftMiddleFingerTip { get { return Torso.Left.Arm.Hand.Fingers.Middle.Phalanges.Distal; } }
public PhysicalObject LeftMiddleFingerMiddle { get { return Torso.Left.Arm.Hand.Fingers.Middle.Phalanges.Middle; } }
public PhysicalObject LeftMiddleFingerKnuckle { get { return Torso.Left.Arm.Hand.Fingers.Middle.Phalanges.Proximal; } }
// Left Ring Finger
public PhysicalObject LeftRingFingerTip { get { return Torso.Left.Arm.Hand.Fingers.Ring.Phalanges.Distal; } }
public PhysicalObject LeftRingFingerMiddle { get { return Torso.Left.Arm.Hand.Fingers.Ring.Phalanges.Middle; } }
public PhysicalObject LeftRingFingerKnuckle { get { return Torso.Left.Arm.Hand.Fingers.Ring.Phalanges.Proximal; } }
// Left Small Finger
public PhysicalObject LeftSmallFingerTip { get { return Torso.Left.Arm.Hand.Fingers.Small.Phalanges.Distal; } }
public PhysicalObject LeftSmallFingerMiddle { get { return Torso.Left.Arm.Hand.Fingers.Small.Phalanges.Middle; } }
public PhysicalObject LeftSmallFingerKnuckle { get { return Torso.Left.Arm.Hand.Fingers.Small.Phalanges.Proximal; } }
}
}
void Main()
{
ABase aBase = new ADerived();
aBase.DoWork();
}
public partial class ABase
{
protected int state1 = 1;
protected int state2 = 2;
List<ABase> workers;
public ABase()
{
workers = new List<ABase>();
CreateWorkers(workers);
}
protected virtual void CreateWorkers(List<ABase> workers)
{
}
public ABase(ABase aBase)
{
this.Target = aBase;
}
public virtual void DoWork()
{
foreach (var worker in this.workers)
{
worker.DoWork();
}
}
protected ABase Target { get; private set; }
}
public partial class ABase
{
public class Worker1 : ABase
{
public Worker1(ABase aBase) : base(aBase) { }
public override void DoWork()
{
Console.WriteLine (Target.state1);
}
}
public class Worker2 : ABase
{
public Worker2(ABase aBase) : base(aBase) { }
public override void DoWork()
{
Console.WriteLine (Target.state2);
}
}
}
public class ADerived : ABase
{
protected override void CreateWorkers(List<ABase> workers)
{
workers.Add(new Worker1(this));
workers.Add(new Worker2(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment