Не получается создать универсальный метод - C#

Узнай цену своей работы

Формулировка задачи:

Создать интерфейс IOperations, классы Vector2D и Vector3D, которые реализуют этот интерфейс, в которых реализованы методы сложения, вычитания векторов, а также умножение на число. Не получается создать универсальный метод
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. struct Point2D
  9. {
  10. public int x;
  11. public int y;
  12. public Point2D(int x, int y)
  13. {
  14. this.x = x;
  15. this.y = y;
  16. }
  17. }
  18. struct Point3D
  19. {
  20. public int x;
  21. public int y;
  22. public int z;
  23. public Point3D(int x, int y, int z)
  24. {
  25. this.x = x;
  26. this.y = y;
  27. this.z = z;
  28. }
  29. }
  30. interface IOperations <T>
  31. {
  32. T Addition<T>(T vector1, T vector2) where T : Vector2D, Vector3D;
  33. T Subtraction<T>(T vector1, T vector2) where T : Vector2D, Vector3D;
  34. T Multiply<T>(int num, T vector) where T : Vector2D, Vector3D;
  35. }
  36.  
  37. class Vector2D : IOperations
  38. {
  39. public Point2D point1;
  40. public Point2D point2;
  41. public Vector2D(Point2D point1, Point2D point2)
  42. {
  43. this.point1 = point1;
  44. this.point2 = point2;
  45. }
  46. public T Addition<T>(T vector1, T vector2) where T : Vector2D, Vector3D
  47. {
  48. vector1.point2 = new Point2D(vector2.point2.x - vector2.point1.x + vector1.point2.x, vector2.point2.y - vector2.point1.y + vector1.point2.y);
  49. return vector1;
  50. }
  51. public T Substraction<T>(T vector1, T vector2) where T : Vector2D, Vector3D
  52. {
  53. vector1.point2 = new Point2D(vector1.point2.x - (vector2.point2.x - vector2.point1.x), vector1.point2.y - (vector2.point2.y - vector2.point1.y)));
  54. return vector1;
  55. }
  56. public T Multiply<T>(int numb, T vector) where T: Vector2D, Vector3D
  57. {
  58. Point2D pnt = new Point2D(vector.point2.x - vector.point1.x, vector.point2.y - vector.point1.y);
  59. vector.point2 = new Point2D(pnt.x * numb, pnt.y * numb);
  60. return vector;
  61. }
  62. }
  63. class Vector3D : IOperations
  64. {
  65. }
  66.  
  67. class Program
  68. {
  69. static void Main(string[] args)
  70. {
  71. }
  72. }
  73. }

Решение задачи: «Не получается создать универсальный метод»

textual
Листинг программы
  1.     interface IOperations<T>
  2.     {
  3.         T Addition(T vector1, T vector2);
  4.         T Subtraction(T vector1, T vector2);
  5.         T Multiply(int num, T vector);
  6.     }
  7.  
  8.     class Vector2D : IOperations <Vector2D>
  9.     {
  10.  
  11.         public Point2D point1;
  12.         public Point2D point2;
  13.         public Vector2D(Point2D point1, Point2D point2)
  14.         {
  15.             this.point1 = point1;
  16.             this.point2 = point2;
  17.         }
  18.         public Vector2D Addition(Vector2D vector1, Vector2D vector2)
  19.         {
  20.              // а тут - реализация на Ваше усмотрение:
  21.              // return new Vector2D (..., ...);
  22.         }
  23.  
  24.         // остальные методы - аналогично

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 3.923 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы