首页 > 其他分享 >ThisCompareDemo

ThisCompareDemo

时间:2024-09-23 14:54:16浏览次数:9  
标签:compare String int age Person4 ThisCompareDemo name


package com.shrimpking.t4;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/15 20:24
 */
class Person4{
    String name;
    int age;

    Person4(String name,int age){
        this.name = name;
        this.age = age;
    }

    //判定相等的方法
    boolean compare(Person4 p){
        if(this.name.equals(p.name) && this.age == p.age){
            return true;
        }else {
            return false;
        }
    }
}

public class ThisCompareDemo
{
    public static void main(String[] args)
    {
        Person4 p1 = new Person4("zhang",30);
        Person4 p2 = new Person4("zhang",30);

        System.out.println(p1.compare(p2) ? "相等,同一个人" : "不等,不是一个人");
    }
}

标签:compare,String,int,age,Person4,ThisCompareDemo,name
From: https://blog.51cto.com/u_15356972/12089378

相关文章