Issue of parent-class reference object in C#



  • Hello, everyone! I've got a code on the C#, says class. Point and its derivatives, including class RightTriangle:Point♪ I've created a reference that needs to be compared to different classes under certain conditions (all of them are made from Pointand this reference requires methods (simply Get/Set) and also comparisons (EqualsGetHashCode==!= Reassigned for each production class. Problem is, it doesn't work banally. Wednesday says that there are no methods of daughter classes on this reference, but the comparison obviously doesn't pass by giving false All cases.

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

    RightTriangle obj2;
    obj2 = rtr1;
    obj2.GetSideA(); // теперь работает, но я не знаю класс obj2 заранее

    I read MSDN, I found nothing on my problem. So far, I've been doing algorithm with a ton of disgusting nation designs. if- Oh, but I really don't want to leave that because the meaning of all the PLOs in my work is losing.



  • 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; 
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2