JAVA中两个成员变量的内存地址相等有什么用哪?能判断出什么 10
packageTest;importjava.util.Objects;publicclassTest01{publicstaticvoidmain(String[]ar...
package Test;
import java.util.Objects;
public class Test01 {
public static void main(String[] args) {
Test02 a=new Test02(1997,12,10);
Test02 a1=new Test02(197,12,10);
boolean b=a.equals(a1);
System.out.println(a+"a的地址");
System.out.println(a1+"a1的地址");
System.out.println(b);
}
}
class Test02{
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public Test02() {
}
public Test02(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean equals(Object o) {
if(this==o){
System.out.println(o+"o的地址");//a1
System.out.println(this+"this的地址");//a
return true;
}
if(o==null||!(o instanceof Test02)){
return false;
}
Test02 a=(Test02)o;
return year==a.year&&month==a.month&&day==a.day;
}
} 展开
import java.util.Objects;
public class Test01 {
public static void main(String[] args) {
Test02 a=new Test02(1997,12,10);
Test02 a1=new Test02(197,12,10);
boolean b=a.equals(a1);
System.out.println(a+"a的地址");
System.out.println(a1+"a1的地址");
System.out.println(b);
}
}
class Test02{
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public Test02() {
}
public Test02(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean equals(Object o) {
if(this==o){
System.out.println(o+"o的地址");//a1
System.out.println(this+"this的地址");//a
return true;
}
if(o==null||!(o instanceof Test02)){
return false;
}
Test02 a=(Test02)o;
return year==a.year&&month==a.month&&day==a.day;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询