BasicKnowledgeCSharp/LessonsAndTasks/Lesson 78 - Наследование интерфейсов, множественное наследование/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

82 lines
1.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
/*
* наследование интерфейсов
*/
interface IWeapon
{
void Fire();
}
/// <summary>
/// Метательное оружие
/// </summary>
interface IThrowingWeapon : IWeapon
{
void Throw();
}
/// <summary>
/// Принцип такой-же как у классов (все что есть в базовом доступно и в наследнике)
/// </summary>
interface ISmall : IWeapon { }
/// <summary>
/// В отличии от классов поддерживают множественное наследование
/// </summary>
interface IBig : IWeapon, ISmall { }
class Gun : IWeapon
{
public void Fire()
=> Console.WriteLine($"{GetType().Name}: GunBom");
}
class LazerGun : IWeapon
{
public void Fire()
=> Console.WriteLine($"{GetType().Name}: LazerGunBom");
}
class Bow : IWeapon// - это НЕ наследование, а реализация интерфейсов
{
public void Fire()
=> Console.WriteLine($"{GetType().Name}: BowBom");
}
/// <summary>
/// Реализуем нож
/// </summary>
class Knife : IThrowingWeapon
{
public void Fire()
=> Console.WriteLine($"{GetType().Name}: KnifeBom");
public void Throw()
=> Console.WriteLine($"{GetType().Name}: KnifeThrow");
}
class Player
{
public void Fire(IWeapon weapon) => weapon.Fire();
public void Throw(IThrowingWeapon throwingWeapon)
=> throwingWeapon.Throw();
}
class Program
{
static void Main()
{
ISmall small;
var player = new Player();
IWeapon[] inventory = { new Gun(), new LazerGun(), new Knife() };
foreach (var item in inventory)
{
player.Fire(item);
Console.WriteLine();
}
player.Throw(new Knife());
Console.ReadKey();
}
}