Created
November 24, 2023 13:06
-
-
Save sunmeat/c6e5f674f255b8e527fc02027db1764c to your computer and use it in GitHub Desktop.
assossiation 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
using System; | |
class Person | |
{ | |
public string Name { get; set; } // 1) наличие свойства с типом другого класса/структуры - это ассоциация | |
public string Surname { get; set; } | |
public string SaySomething() // 2) возврат объекта с типом другого класса/структуры - это тоже ассоциация | |
{ | |
string local; // 3) создание локальной переменной/константы с типом другого класса/структуры - это третий вариант ассоциации | |
return "всем уже привет"; | |
} | |
} | |
class Cat | |
{ | |
public string Nick { get; set; } | |
public double Weight { get; set; } | |
public void PlayWith(Person person) // 4) наличие в классе метода с параметром типа другого класса/структуры - это 4 вариант ассоциации | |
{ | |
Console.WriteLine($"Cat {Nick} is playing with his owner {person.Name}"); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
Cat c = new Cat(); | |
c.Nick = "Barsik"; | |
Person p = new Person(); | |
p.Name = "Alex"; | |
c.PlayWith(p); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment