Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active December 4, 2025 21:04
Show Gist options
  • Select an option

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

Select an option

Save sunmeat/deeaeff05a49d321b126ab38c59c010d to your computer and use it in GitHub Desktop.
inheritance simple example c#
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