Мой код:
using System;
namespace WorkWithInterface
{
class Program
{
static void Main(string[] args)
{
eCar ecar = new eCar(200, 4, "AUDI", State.Накачены);
Console.ReadKey();
}
}
public abstract class Car
{
public abstract int MaxSpeed { get; set; }
public Car(int maxSpeed) { MaxSpeed = maxSpeed; }
}
class eCar : Car, ICar, ITyre
{
public override int MaxSpeed { get; set; }
public int Wheels { get; set; }
public string Mark { get; set; }
public State State
{
get => State;
set
{
State = value;
StateCar = State == State.Накачены;
if (StateCar)
OnAction += Move;
else
OnAction -= Move;
OnAction?.Invoke();
}
}
private bool StateCar { get; set; }
public Action OnAction { get; set; }
public eCar(int maxSpeed, int wheels, string mark, State state) : base(maxSpeed)
{
MaxSpeed = maxSpeed;
Wheels = wheels;
Mark = mark;
State = state;
OnAction = GetState;
}
public void Move()
=> Console.WriteLine($"{Mark} поехал со скоростью {MaxSpeed / 2}");
public void GetState()
{
Console.WriteLine(StateCar
? $"Электромобиль может ехать. Колеса {State}"
: $"Электромобиль не может ехать. Колеса {State}");
}
}
class gCar : Car, ICar, ITyre
{
public override int MaxSpeed { get; set; }
public int Wheels { get; set; }
public string Mark { get; set; }
public State State
{
get => State;
set
{
State = value;
StateCar = State == State.Накачены;
if (StateCar)
OnAction += Move;
else
OnAction -= Move;
OnAction?.Invoke();
}
}
private bool StateCar { get; set; }
public Action OnAction { get; set; }
public gCar(int maxSpeed, int wheels, string mark, State state) : base(maxSpeed)
{
MaxSpeed = maxSpeed;
Wheels = wheels;
Mark = mark;
State = state;
OnAction = GetState;
}
public void Move()
=> Console.WriteLine($"{Mark} поехал со скоростью {MaxSpeed / 2}");
public void GetState()
{
Console.WriteLine(StateCar
? $"Бензиновый автомобиль может ехать. Колеса {State}"
: $"Бензиновый автомобиль не может ехать. Колеса {State}");
}
}
interface ICar
{
string Mark { get; set; }
void Move();
}
interface ITyre
{
int Wheels { get; set; }
State State { get; set; }
void GetState();
Action OnAction { get; set; }
}
enum State { Проткнуты, Лопнуты, Накачены, Сняты}
}
Ошибка здесь. Вы обращаетесь из свойства к этому же свойству, что приводит к рекурсии и переполнению стека. Создайте поле {private State _state;} и обращайтесь к нему.
public State State
{
get => State;
set
{
State = value;
StateCar = State == State.Накачены;
if (StateCar)
OnAction += Move;
else
OnAction -= Move;
OnAction?.Invoke();
}
}
Продвижение своими сайтами как стратегия роста и независимости