Created
April 27, 2017 12:25
-
-
Save DominicFinn/0ae81322e2c25766b924e5a63e974576 to your computer and use it in GitHub Desktop.
Classes for my blog post on Object Creation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class BadDojo | |
{ | |
public IList<Student> Students { get; set; } | |
public Sensei Sensei { get; set; } | |
public string Summary => $"this dojo is run by {Sensei.Name} and has {Students.Count} students"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Person | |
{ | |
public string Name { get; set; } | |
protected Person(string name) | |
{ | |
Name = name; | |
} | |
} | |
sealed class Sensei : Person | |
{ | |
public Sensei(string name) : base(name) | |
{ | |
} | |
} | |
sealed class Student : Person | |
{ | |
public Student(string name) : base(name) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class GoodDojo | |
{ | |
private readonly IList<Student> students; | |
public Sensei Sensei { get; } | |
public IEnumerable<Student> Students => this.students; | |
public GoodDojo(Sensei sensei) | |
{ | |
Sensei = sensei; | |
this.students = new List<Student>(); | |
} | |
public GoodDojo(Sensei sensei, IEnumerable<Student> students) | |
{ | |
Sensei = sensei; | |
this.students = students.ToList(); | |
} | |
public string Summary => $"this dojo is run by {Sensei.Name} and has {Students.Count()} students"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var badDojo = new BadDojo() | |
{ | |
Sensei = new Sensei("Barney") | |
}; | |
Console.WriteLine(badDojo.Summary); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var goodDojo = new GoodDojo(new Sensei("Dom")); | |
Console.WriteLine(goodDojo.Summary); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment