An example
The following example shows the source code of a Point class:
namespace Graphics
{
/// Class Point models a point in a two-dimensional plane.
///
public class Point
{
/// Instance variable x represents the point's
/// x-coordinate.
private int x;
/// Instance variable y represents the point's
/// y-coordinate.
private int y;
/// Property X represents the point's x-coordinate.
public int X
{
get { return x; }
set { x = value; }
}
/// Property Y represents the point's y-coordinate.
public int Y
{
get { return y; }
set { y = value; }
}
/// This constructor initializes the new Point to
/// (0,0).
public Point() : this(0,0) {}
/// This constructor initializes the new Point to
/// (
,
).
///
xor is the new Point's x-coordinate.
///
yor is the new Point's y-coordinate.
public Point(int xor, int yor) {
X = xor;
Y = yor;
}
/// This method changes the point's location to
/// the given coordinates.
///
xor is the new x-coordinate.
///
yor is the new y-coordinate.
///
public void Move(int xor, int yor) {
X = xor;
Y = yor;
}
/// This method changes the point's location by
/// the given x- and y-offsets.
/// For example:
///
/// Point p = new Point(3,5);
/// p.Translate(-1,3);
///
/// results in p's having the value (2,8).
///
///
///
xor is the relative x-offset.
///
yor is the relative y-offset.
///
public void Translate(int xor, int yor) {
X += xor;
Y += yor;
}
/// This method determines whether two Points have the same
/// location.
///
o is the object to be compared to the current object.
///
/// True if the Points have the same location and they have
/// the exact same type; otherwise, false.
///
///
public override bool Equals(object o) {
if (o == null) {
return false;
}
if (this == o) {
return true;
}
if (GetType() == o.GetType()) {
Point p = (Point)o;
return (X == p.X) && (Y == p.Y);
}
return false;
}
/// Report a point's location as a string.
/// A string representing a point's location, in the form (x,y),
/// without any leading, training, or embedded whitespace.
public override string ToString() {
return "(" + X + "," + Y + ")";
}
/// This operator determines whether two Points have the same
/// location.
///
p1 is the first Point to be compared.
///
p2 is the second Point to be compared.
/// True if the Points have the same location and they have
/// the exact same type; otherwise, false.
///
///
public static bool operator==(Point p1, Point p2) {
if ((object)p1 == null || (object)p2 == null) {
return false;
}
if (p1.GetType() == p2.GetType()) {
return (p1.X == p2.X) && (p1.Y == p2.Y);
}
return false;
}
/// This operator determines whether two Points have the same
/// location.
///
p1 is the first Point to be compared.
///
p2 is the second Point to be compared.
/// True if the Points do not have the same location and the
/// exact same type; otherwise, false.
///
///
public static bool operator!=(Point p1, Point p2) {
return !(p1 == p2);
}
/// This is the entry point of the Point class testing
/// program.
///
This program tests each method and operator, and
/// is intended to be run after any non-trvial maintenance has
/// been performed on the Point class.
public static void Main() {
// class test code goes here
}
}
}
Do'stlaringiz bilan baham: |