2025-05-12 02:48:54 +03:00
|
|
|
|
/*
|
|
|
|
|
* Author: Nikolay Dvurechensky
|
|
|
|
|
* Site: https://www.dvurechensky.pro/
|
|
|
|
|
* Gmail: dvurechenskysoft@gmail.com
|
|
|
|
|
* Last Updated: 12 мая 2025 02:47:11
|
|
|
|
|
* Version: 1.0.3
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2024-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* наследование интерфейсов
|
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|