Inheritance

mail@pastecode.io avatar
unknown
csharp
a month ago
2.9 kB
3
Indexable
Never
internal class Program
{
	private static void Main(string[] args)
	{
		User[] users = new User[]
		{
			new User(0, "User1@mail.com", "User 1", "123456"),
			new User(1, "User2@mail.com", "User 2", "123456"),
			new Moderator(1, "mod@mail.com", "mod1", "12345"),
			new Admin(0, "admin@mail.com", "admin1", "123456", 10)
		};

		foreach (var user in users)
		{
			// IF-ELSE VARIANT
			//
			//if (user is Admin)
			//{
			//	Console.Write("[ADMIN]: ");
			//}
			//else if (user is Moderator)
			//{
			//	Console.Write("[MODER]: ");
			//}

			switch (user)
			{
				case Admin:
					Console.Write("[ADMIN]: ");
					break;
				case Moderator:
					Console.Write("[MODER]: ");
					break;
				default:
					break;
			}

			Console.WriteLine(user.Name);
		}

		// DEMONSTRATION
		//
		//int number1 = 100;
		//byte number2 = 50;

		//number1 = number2; // upcasting
		//number2 = (byte)number1; // downcasting

		Admin admin = new Admin(0, "admin@mail.com", "admin1", "123456", 10);
		Moderator moderator = new Moderator(1, "mod@mail.com", "mod1", "12345");

		moderator.Invite();

		//moderator = admin; // upcasting
		//admin = (Admin)moderator; // downcasting

		//admin.GetStatus();
		//Console.WriteLine(admin.Level);

		//Console.WriteLine(moderator.Name);

		//Console.WriteLine(admin.Id);
		//Console.WriteLine(admin.Name);
		//Console.WriteLine(admin.Password);
		//Console.WriteLine(admin.Level);

		//admin.ShowData();
		//admin.GetStatus();
		//admin.Kick();


		//Console.WriteLine(moderator.Id);
		//Console.WriteLine(moderator.Name);
		//Console.WriteLine(moderator.Password);

		//moderator.ShowData();
		//moderator.Kick();
	}
}

public class User
{
	public int Id { get; set; }
	public string Email { get; set; }
	public string Name { get; set; }
	public string Password { get; set; }

	public User(int id, string email, string name, string password)
	{
		Id = id;
		Email = email;
		Name = name;
		Password = password;
	}

	public void ShowData()
	{
		Console.WriteLine($"Name: {Name}");
		Console.WriteLine($"Email: {Name}");
	}

	public void Invite()
	{
		Console.WriteLine("Пользователь приглашает");
	}
}

public class Moderator : User
{
	public Moderator(int id, string email, string name, string password) : base(id, email, name, password)
	{

	}

	public void Invite()
	{
		Console.WriteLine("Модератор приглашает");
	}
}

public class Admin : Moderator
{
	public int Level { get; set; }

	public Admin(int id, string email, string name, string password, int level) : base(id, email, name, password)
	{
		Level = level;
	}

	public void GetStatus()
	{
		Console.WriteLine("Сейчас всё ок");
	}
}

public class Animal
{
	public Animal()
	{

	}
}
Leave a Comment