Основы объектно-ориентированного программирования на языке C# book | Page 188

8 Вправи
public sealed class BinaryExpression: Expression
{ private readonly BinaryOp op; private readonly Expression left, right;
public BinaryOp Op { get { return op; } } public Expression Left { get { return left; } } public Expression Right { get { return right; } }
public BinaryExpression( BinaryOp op, Expression left, Expression right)
{ if( op == null || left == null || right == null) throw new ArgumentNullException(); if( left is EmptyExpression || right is EmptyExpression) throw new ArgumentException(" Binary operation cannot be applied to empty expression "); this. op = op; this. left = left; this. right = right;
}
} public override long Eval() { return op. Eval( left. Eval(), right. Eval());
}
public sealed class UnaryExpression: Expression
{ private readonly UnaryOp op; private readonly Expression arg;
public UnaryOp Op { get { return op; } } public Expression Arg { get { return arg; } }
public UnaryExpression( UnaryOp op, Expression arg) { if( op == null || arg == null) throw new ArgumentNullException(); if( arg is EmptyExpression) throw new ArgumentException(" Unary operation cannot be applied to empty expression "); this. op = op; this. arg = arg;
}
} public override long Eval() { return op. Eval( arg. Eval());
}
public sealed class NumberExpression: Expression, IToken
{ private readonly long value; public long Value { get { return value; } }
188