Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 8, 2026 18:02
Show Gist options
  • Select an option

  • Save sunmeat/c4c25aff2928a6afa9af2609e9ae74ef to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/c4c25aff2928a6afa9af2609e9ae74ef to your computer and use it in GitHub Desktop.
composition C# example
using System;
namespace CompositionExample
{
public class PegLeg // дерев'яна нога
{
public string Color { get; set; }
public bool Dirty { get; set; }
public double Length { get; set; }
public int Usability { get; set; }
public PegLeg()
{
Console.WriteLine("конструктор дерев'яної ноги");
}
~PegLeg()
{
// з фіналізаторами все складно, так просто сюди не потрапити :)
Console.WriteLine("фіналізатор дерев'яної ноги");
}
}
public class HandHook // рука-гачок
{
public string Material { get; set; }
public int Usability { get; set; }
public bool Dirty { get; set; }
public bool CanMakeSelfy { get; set; }
public HandHook()
{
Console.WriteLine("конструктор руки-гачка");
}
}
public class EyePatch // наглазник
{
public string Color { get; set; }
public double Size { get; set; }
public bool Elastic { get; set; }
public bool Leather { get; set; }
public EyePatch()
{
Console.WriteLine("конструктор наглазника");
}
}
public class Pirate
{
public string Nickname { get; set; }
public PegLeg Leg { get; set; }
public HandHook Hand { get; set; }
public EyePatch Patch { get; set; }
public Pirate(string nickname)
{
this.Nickname = nickname;
this.Leg = new PegLeg();
this.Hand = new HandHook();
this.Patch = new EyePatch();
Console.WriteLine("пірата створено!");
}
public void CaptureShip()
{
Console.WriteLine($"{Nickname} волає: Готуйтесь до абордажу!");
}
public void SingSong()
{
Console.WriteLine($"{Nickname} співає: \"...Йо-хо-хо, та й ще пляшка рому!!!\"");
}
}
class Program
{
static void Main()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
var p = new Pirate("Капітан Джек Горобець");
p.CaptureShip();
p.SingSong();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment