fork download
  1. using System;
  2.  
  3. // Базовый класс Object.
  4.  
  5. // Правило: Переопределяйте GetHashCode переопределяя Equals.
  6.  
  7. namespace ClassObject
  8. {
  9. class Point : object
  10. {
  11. protected int x, y;
  12.  
  13. public Point(int xValue, int yValue)
  14. {
  15. x = xValue;
  16. y = yValue;
  17. }
  18.  
  19. public override bool Equals(Object obj)
  20. {
  21. if (obj == null || GetType() != obj.GetType())
  22. return false;
  23.  
  24. Point p = (Point)obj;
  25. return (x == p.x) && (y == p.y);
  26. }
  27.  
  28. public override int GetHashCode()
  29. {
  30. return x ^ y;
  31. }
  32. }
  33.  
  34. class Program
  35. {
  36. static void Main()
  37. {
  38. Point a = new Point(1, 2);
  39. Point b = new Point(1, 2);
  40. Point c = new Point(0, 0);
  41.  
  42. Console.WriteLine("a == b : {0}", a.Equals(b));
  43. Console.WriteLine("a == c : {0}", a.Equals(c));
  44.  
  45. Console.WriteLine("a == b : {0}", Equals(a, b));
  46. Console.WriteLine("a == c : {0}", Equals(a, c));
  47.  
  48. // Delay.
  49. Console.ReadKey();
  50. }
  51. }
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class, interface, or enum expected
using System;
^
Main.java:7: error: class, interface, or enum expected
namespace ClassObject
^
Main.java:9: error: '{' expected
    class Point : object
               ^
Main.java:19: error: ';' expected
        public override bool Equals(Object obj)
                            ^
Main.java:19: error: invalid method declaration; return type required
        public override bool Equals(Object obj)
                             ^
Main.java:28: error: <identifier> expected
        public override int GetHashCode()
                       ^
Main.java:28: error: invalid method declaration; return type required
        public override int GetHashCode()
                            ^
Main.java:52: error: class, interface, or enum expected
}
^
8 errors
stdout
Standard output is empty