java初学者有道作业题看不懂,谁了帮忙看看了
publicclassMyPoint{privateintx,y;publicMyPoint(){//这里的publicMyPoint是什么意思?上面已经有个public...
public class MyPoint {
private int x, y;
public MyPoint() { //这里的public MyPoint是什么意思?上面已经有个public class MyPoint 了,这边还可以有个同名了吗?
x = 0;
y = 0;
}
public MyPoint(int a) {
x = a;
y = 0;
}
public MyPoint(int a, int b) {
x = a;
y = b;
}
public String toString() {
return "(" + x + "," + y + ")";
}
public boolean equals(MyPoint t) {
if (this.x == t.x && this.y == t.y) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyPoint point = new MyPoint();
MyPoint point1 = new MyPoint(2);
MyPoint point2 = new MyPoint(3);
MyPoint point3 = new MyPoint(1,1);
System.out.println(point.equals(point)); //这个point.equals(point)是什么意思?
System.out.println(point.equals(point)); //这个和上面一个有啥不同吗?
System.out.println(point);
System.out.println(point1);
System.out.println(point2);
System.out.println(point3);
}
} 展开
private int x, y;
public MyPoint() { //这里的public MyPoint是什么意思?上面已经有个public class MyPoint 了,这边还可以有个同名了吗?
x = 0;
y = 0;
}
public MyPoint(int a) {
x = a;
y = 0;
}
public MyPoint(int a, int b) {
x = a;
y = b;
}
public String toString() {
return "(" + x + "," + y + ")";
}
public boolean equals(MyPoint t) {
if (this.x == t.x && this.y == t.y) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyPoint point = new MyPoint();
MyPoint point1 = new MyPoint(2);
MyPoint point2 = new MyPoint(3);
MyPoint point3 = new MyPoint(1,1);
System.out.println(point.equals(point)); //这个point.equals(point)是什么意思?
System.out.println(point.equals(point)); //这个和上面一个有啥不同吗?
System.out.println(point);
System.out.println(point1);
System.out.println(point2);
System.out.println(point3);
}
} 展开
1个回答
展开全部
public class MyPoint 这个是类
public MyPoint() 这个是方法,构造函数,构造函数必须与类同名
equals 是当参数obj引用的对象与当前对象为同一个对象时,就返回true,否则返回false
System.out.println(point.equals(point)); 这句明显跟上一句没区别,写得一模一样,应该是你写错了,可能是想比较point.equals(point1)
public MyPoint() 这个是方法,构造函数,构造函数必须与类同名
equals 是当参数obj引用的对象与当前对象为同一个对象时,就返回true,否则返回false
System.out.println(point.equals(point)); 这句明显跟上一句没区别,写得一模一样,应该是你写错了,可能是想比较point.equals(point1)
更多追问追答
追问
1、public boolean equals(MyPoint t) {
if (this.x == t.x && this.y == t.y) {
return true;
} else {
return false;
}
}
这段程序是什么意思呢?
2、public String toString()和public boolean equals(MyPoint t)也是构造方法吗?
谢谢
追答
if (this.x == t.x && this.y == t.y) { 比较两天对象的x和y属性值,如果都相同,则返回true
构造函数必须与类同名 仔细看这句话
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询