Не получается создать универсальный метод - C#
Формулировка задачи:
Создать интерфейс IOperations, классы Vector2D и Vector3D, которые реализуют этот интерфейс, в которых реализованы методы сложения, вычитания векторов, а также умножение на число.
Не получается создать универсальный метод
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
struct Point2D
{
public int x;
public int y;
public Point2D(int x, int y)
{
this.x = x;
this.y = y;
}
}
struct Point3D
{
public int x;
public int y;
public int z;
public Point3D(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
interface IOperations <T>
{
T Addition<T>(T vector1, T vector2) where T : Vector2D, Vector3D;
T Subtraction<T>(T vector1, T vector2) where T : Vector2D, Vector3D;
T Multiply<T>(int num, T vector) where T : Vector2D, Vector3D;
}
class Vector2D : IOperations
{
public Point2D point1;
public Point2D point2;
public Vector2D(Point2D point1, Point2D point2)
{
this.point1 = point1;
this.point2 = point2;
}
public T Addition<T>(T vector1, T vector2) where T : Vector2D, Vector3D
{
vector1.point2 = new Point2D(vector2.point2.x - vector2.point1.x + vector1.point2.x, vector2.point2.y - vector2.point1.y + vector1.point2.y);
return vector1;
}
public T Substraction<T>(T vector1, T vector2) where T : Vector2D, Vector3D
{
vector1.point2 = new Point2D(vector1.point2.x - (vector2.point2.x - vector2.point1.x), vector1.point2.y - (vector2.point2.y - vector2.point1.y)));
return vector1;
}
public T Multiply<T>(int numb, T vector) where T: Vector2D, Vector3D
{
Point2D pnt = new Point2D(vector.point2.x - vector.point1.x, vector.point2.y - vector.point1.y);
vector.point2 = new Point2D(pnt.x * numb, pnt.y * numb);
return vector;
}
}
class Vector3D : IOperations
{
}
class Program
{
static void Main(string[] args)
{
}
}
}Решение задачи: «Не получается создать универсальный метод»
textual
Листинг программы
interface IOperations<T>
{
T Addition(T vector1, T vector2);
T Subtraction(T vector1, T vector2);
T Multiply(int num, T vector);
}
class Vector2D : IOperations <Vector2D>
{
public Point2D point1;
public Point2D point2;
public Vector2D(Point2D point1, Point2D point2)
{
this.point1 = point1;
this.point2 = point2;
}
public Vector2D Addition(Vector2D vector1, Vector2D vector2)
{
// а тут - реализация на Ваше усмотрение:
// return new Vector2D (..., ...);
}
// остальные методы - аналогично