Visual Basic : Imports System
Class Point Inherits Object Protected x, y As Integer
Public Sub New() Me.x = 0 Me.y = 0 End Sub 'New
Public Sub New(ByVal X As Integer, ByVal Y As Integer) Me.x = X Me.y = Y End Sub 'New
Public Overrides Function Equals(ByVal obj As Object) As Boolean 'Check for null and compare run-time types. If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then Return False End If Dim p As Point = CType(obj, Point) Return x = p.x AndAlso y = p.y End Function 'Equals
Public Overrides Function GetHashCode() As Integer Return x ^ y End Function 'GetHashCode End Class 'Point
Class Point3D Inherits Point Private z As Integer
Public Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal Z As Integer) Me.x = X Me.y = Y Me.z = Z End Sub 'New
Public Overrides Function Equals(ByVal obj As Object) As Boolean Return MyBase.Equals(obj) AndAlso z = CType(obj, Point3D).z End Function 'Equals
Public Overrides Function GetHashCode() As Integer Return MyBase.GetHashCode() ^ z End Function 'GetHashCode End Class 'Point3D
Class [MyClass] Public Shared Sub Main() Dim point2D As New Point(5, 5) Dim point3Da As New Point3D(5, 5, 2) Dim point3Db As New Point3D(5, 5, 2)
If Not point2D.Equals(point3Da) Then Console.WriteLine("point2D does not equal point3Da.") End If If Not point3Db.Equals(point2D) Then Console.WriteLine("Likewise, point3Db does not equal point2D.") End If If point3Da.Equals(point3Db) Then Console.WriteLine("However, point3Da equals point3Db.") End If
End Sub 'Main End Class '[MyClass] ' ---------------------------------- ' Output should be: ' ' point2D does not equal point3Da. ' Likewise, point3Db does not equal point2D. ' However, point3Da equals point3Db.
C# : using System;
class Point: Object { protected int x, y;
public Point() { this.x = 0; this.y = 0; }
public Point(int X, int Y) { this.x = X; this.y = Y; }
public override bool Equals(Object obj) { //Check for null and compare run-time types. if (obj == null || GetType() != obj.GetType()) return false; Point p = (Point)obj; return (x == p.x) && (y == p.y); }
public override int GetHashCode() { return x ^ y; } }
class Point3D: Point { int z;
public Point3D(int X, int Y, int Z) { this.x = X; this.y = Y; this.z = Z; }
public override bool Equals(Object obj) { return base.Equals(obj) && z == ((Point3D)obj).z; }
public override int GetHashCode() { return base.GetHashCode() ^ z; } }
class MyClass {
public static void Main() { Point point2D = new Point(5, 5); Point3D point3Da = new Point3D(5, 5, 2); Point3D point3Db = new Point3D(5, 5, 2);
if (!point2D.Equals(point3Da)) { Console.WriteLine("point2D does not equal point3Da."); } if (!point3Db.Equals(point2D)) { Console.WriteLine("Likewise, point3Db does not equal point2D."); } if (point3Da.Equals(point3Db)) { Console.WriteLine("However, point3Da equals point3Db."); }
} } // ---------------------------------- // Output should be: // // point2D does not equal point3Da. // Likewise, point3Db does not equal point2D. // However, point3Da equals point3Db. |