java——判断两个对象是否相等

2019年2月16日23:42:50 发表评论 2,360 views
/**
 * Created by xabcd on 2019/2/16.
 */
class java_or
{
    String name;
    int age;
    public java_or(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    boolean compare(java_or j)
    {
        if(this.name.equals(j.name)&&this.age==j.age)
        {
            return true;
        }
        else{
            return false;
        }
    }
}





/**
 * Created by xabcd on 2019/2/16.
 */
public class test_or
{
    public static void main(String args[])
    {
        java_or or1 = new java_or("张三",23);
        java_or or2 = new java_or("张三",23);
        System.out.print(or1.compare(or2)?"是同一个人":"不是同一个人");
    }
}



结果:
是同一个人

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: