📄 methodtostringandequals.java
字号:
package unitTwo;
public class MethodtoStringAndequals {
/* Number of instances created */
private static int numberOfInstances = 0;
/* x coordinate of this point */
private int x;
/* y coordinate of this point */
private int y;
/*
* Test driver for class <code>Point</code>.
*
* @param args not used
*/
public static void main(String[] args) {
MethodtoStringAndequals pointOne = new MethodtoStringAndequals(10, 100);
MethodtoStringAndequals pointTwo = new MethodtoStringAndequals(-20, 200);
MethodtoStringAndequals pointThree = new MethodtoStringAndequals(50, -20);
System.out.println("before reset pointOne");
System.out.println("x: " + pointOne.getX());
System.out.println("y: " + pointOne.getY());
pointOne.setX(-20);
pointOne.setY(200);
System.out.println("after reset pointOne");
System.out.println("x: " + pointOne.getX());
System.out.println("y: " + pointOne.getY());
if (pointOne.equals(pointTwo)) {
System.out.println("pointOne and pointTwo are equal");
} else {
System.out.println("pointOne and pointTwo are different");
}
if (pointOne.equals(pointThree)) {
System.out.println("pointOne and pointThree are equal");
} else {
System.out.println("pointOne and pointThree are different");
}
System.out.println(pointOne);
System.out.println(pointTwo);
System.out.println(pointThree);
System.out.println();
System.out.println("Instances before PointTwo is created: " +
MethodtoStringAndequals.getNumberOfInstances());
}
/**
* Creates a <code>Point</code> object and increments the number of
* instances.
*
* @param initialX
* the x coordinate
* @param initialY
* the y coordinate
*/
public MethodtoStringAndequals(int initialX, int initialY) {
this.x = initialX;
this.y = initialY;
MethodtoStringAndequals.numberOfInstances++;
}
/**
* Returns the number of <code>Point</code> instances that have been
* created.
*
* @return the number of <code>Point</code> instances that have been
* created.
*/
public static int getNumberOfInstances() {
return MethodtoStringAndequals.numberOfInstances;
}
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point.
*/
public int getX() {
return this.x;
}
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point.
*/
public int getY() {
return this.y;
}
/**
* Modifies the x coordinate of this point.
*
* @param newX
* the new x coordinate
*/
public void setX(int newX) {
this.x = newX;
}
/**
* Modifies the y coordinate of this point.
*
* @param newY
* the new y coordinate
*/
public void setY(int newY) {
this.y = newY;
}
/**
* Overrides {@link Object#toString()}.
* <p>
* Returns a string representation of this <code>Point</code> object.
* </p>
*
* @return a string representation of this <code>Point</code> object.
*/
public String toString() {
return "(" + getX() + "," + getY() + ")";
}
/**
* Overrides {@link Object#equals(Object)}.
* <p>
* Two <code>Point</code> objects are equal if their x and y
* coordinates are the same.
* </p>
*
* @param object object which this <code>Point</code> is
* compared.
* @return <code>true</code> if the x and y coordinates of
* this <code>Point</code> are equal to the x and y
* coordinates of the argument; <code>false</code>
* otherwise.
*/
public boolean equals(Object object) {
if (object instanceof MethodtoStringAndequals) {
MethodtoStringAndequals point = (MethodtoStringAndequals) object;
return point.getX() == getX() && point.getY() == getY();
} else {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -