Created
November 24, 2023 13:15
-
-
Save sunmeat/32297181b31bc129ebef946de29c6c33 to your computer and use it in GitHub Desktop.
aggregation example C#
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 Hat | |
{ | |
public string Color { get; set; } | |
public string Model { get; set; } | |
public double Price { get; set; } | |
public override string ToString() | |
{ | |
return Model; | |
} | |
} | |
class Person | |
{ | |
public string Name { get; set; } // тут больше композиция | |
private Hat hat; // тут агрегирование | |
public Hat Hat | |
{ | |
get | |
{ | |
return hat ?? new Hat { Color = "улетела", Model = "улетела", Price = 0.0 }; | |
} | |
set | |
{ | |
hat = value; | |
} | |
} | |
public void TakeHat(Hat hat) | |
{ | |
Hat = hat; | |
} | |
public void GoWalk() | |
{ | |
Console.WriteLine("внезапно подул ветер..."); | |
Hat = null; | |
} | |
public void WhereIsYourHat() | |
{ | |
Console.WriteLine($"where is your ha-a-at, is your ha-a-at, person? а вот она: {Hat}"); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
var hat = new Hat(); | |
hat.Model = "шляпа на месте"; | |
var alex = new Person(); | |
alex.TakeHat(hat); | |
alex.WhereIsYourHat(); | |
alex.GoWalk(); | |
alex.WhereIsYourHat(); | |
Console.WriteLine($"шляпа летит в мейне: {hat}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment