Last active
January 8, 2026 18:02
-
-
Save sunmeat/e99984200f754f3ff32e3f3a3322ab41 to your computer and use it in GitHub Desktop.
aggregation C# example
This file contains hidden or 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
| using System; | |
| namespace AggregationExample | |
| { | |
| public class Hat | |
| { | |
| public string Color { get; set; } | |
| public string Model { get; set; } | |
| public double Price { get; set; } | |
| } | |
| public class Person | |
| { | |
| public string? Name { get; set; } | |
| public Hat? Hat { get; private set; } // агрегування - поле з типом іншого класу | |
| public void TakeHat(Hat hat) // важливо: капелюх береться ззовні, а не створюється одночасно з людиною | |
| { | |
| this.Hat = hat; | |
| } | |
| public void GoWalk() | |
| { | |
| Console.WriteLine("раптом подув вітер..."); | |
| Hat = null; | |
| } | |
| public void WhereIsYourHat() | |
| { | |
| Console.WriteLine("а ось вона: " + Hat); | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = System.Text.Encoding.UTF8; | |
| var hat = new Hat(); | |
| var olena = new Person(); | |
| olena.TakeHat(hat); | |
| olena.WhereIsYourHat(); | |
| olena.GoWalk(); | |
| olena.WhereIsYourHat(); | |
| Console.WriteLine("капелюх: " + hat); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment