Hello! I have a C # code that RightTriangle:Point class and its derivatives, including the RightTriangle:Point class. I created a link that, under certain conditions, should be equated to different classes (all of them are derived from Point ), and this link should call methods (simple Get / Set) and also make a comparison ( Equals , GetHashCode , == GetHashCode != Redefined for each derived class). The problem is that it trite does not work. The medium writes that there are no methods for the child classes on this link, the comparison, obviously, also fails, yielding false for all cases.

 Point obj1; obj1 = rtr1; // rtr1 имеет класс RightTriangle:Point obj1.GetSideA(); // не работает, но у RightTriangle есть метод GetSideA RightTriangle obj2; obj2 = rtr1; obj2.GetSideA(); // теперь работает, но я не знаю класс obj2 заранее 

upd: read MSDN, did not find anything for my problem. So far, I have implemented the algorithm with the help of a ton of disgusting constructions on if -s, but I really wouldn’t want to leave it that way, because the meaning of the whole OOP in my work is then completely lost.

  • What is the expected result if you call the RightTriangle class RightTriangle , and the object will not actually be the RightTriangle class (just the Point )? - VladD
  • Well, in the sense of the PLO. inheriting a triangle from a point doesn't seem like a good idea. The triangle is not a point. - VladD
  • 2
    Does Point :) have a GetSideA () function? In my opinion, you want to implement polymorphism, only through the anus. - Mirdin
  • 2
    You made a working decision, but wrong. A point MUST NOT have a method that it cannot even implement in theory. I'm not even sure how such code should look, because the question is too narrow. If possible, describe why you are trying to call everything from an object with a base class type (full stop). - Monk
  • one
    “You can't argue with a teacher” is a bad position. It is possible and necessary to argue with teachers with arguments. - VladD

1 answer 1

 class Point { public int X; public int Y; override bool Equals(object obj) { var p2 = obj as Point; return p2 != null && p2.X == X && p2.Y == Y; } } class Circle: Point { public int R; override bool Equals(object obj) { var c2 = obj as Circle; return c2 != null && c2.X == X && c2.Y == Y && c2.R == R; } } class Triangle: Point { public int X2; public int Y2; public int X3; public int Y3; override bool Equals(object obj) { var t2 = obj as Triangle; return t2 != null && t2.X == X && t2.Y == Y && t2.X2 == X2 && t2.Y2 == Y2 && t2.X3 == X3 && t2.Y3 == Y3; } } Point p = new Circle(); var circle = p as Circle; if (circle != null) circle.R = 10; 
  • And a triangle from a point how to express? =) I’m stuck on this strange requirement. - Monk
  • @Monk added a triangle - Serj-Tm
  • Hmm, interesting implementation. Technically, it seems and seems to be true, except that the logic turns out to be strange. - Monk
  • @Monk Teaching initial task. Logic - at least: on the allocation of common between types and writing this in the form of code. - Serj-Tm
  • Judging by the fact that the author writes in the comments - he doesn’t really solve it. - Monk