Last active
December 4, 2025 21:04
-
-
Save sunmeat/deeaeff05a49d321b126ab38c59c010d to your computer and use it in GitHub Desktop.
inheritance simple example c#
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.Text; | |
| class Person | |
| { | |
| public string Name { get; set; } | |
| public string Lastname { get; set; } | |
| public int Age { get; set; } | |
| public Person() : this("Іван", "Іваненко", 25) | |
| { | |
| Console.WriteLine("конструктор Person без параметрів"); | |
| } | |
| public Person(string name, string lastname, int age) | |
| { | |
| Console.WriteLine("конструктор Person з параметрами"); | |
| Name = name; | |
| Lastname = lastname; | |
| Age = age; | |
| } | |
| public override string ToString() | |
| { | |
| return Name + " " + Lastname + ", вік: " + Age; | |
| } | |
| } | |
| class Policeman : Person | |
| { | |
| class PoliceCap { } | |
| class PoliceBadge { } | |
| PoliceCap furazhka = new(); | |
| PoliceBadge znachok = new(); | |
| public string Rank { get; set; } // звання | |
| public Policeman() | |
| { | |
| Console.WriteLine("конструктор Policeman без параметрів"); | |
| Rank = "молодший лейтенант"; | |
| } | |
| public Policeman(string name, string lastname, int age, string zvannya) : base(name, lastname, age) | |
| { | |
| Console.WriteLine("конструктор Policeman з параметрами"); | |
| Rank = zvannya; | |
| } | |
| public override string ToString() | |
| { | |
| return Name + " " + Lastname + ", вік: " + Age + "\nзвання: " + Rank; | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| var p = new Policeman("Іван", "Франко", 37, "старший сержант"); | |
| Console.WriteLine(p); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment