import java.util.Scanner; public class Eext { public static void main(String[] args) { person p1 = new person("lala" , 7); person p2 = new person("koko" , 17); System.out.println("p1和p2比较的结果:" + p1.compareTo(p2)); } } //定义一个person类,里面有name age属性,并提供compareTo比较方法 //用于判断是否和另一个人完全相等,提供测试类, TestPerson用于测试 //名字和年龄完全一样,就返回true 否则返回false class person { String name; int age; //构造器 public person(String name , int age) { this.name = name; this.age = age; } //compareTo 比较方法 public boolean compareTo(person p) { //if (this.name.equals(p.name) && this.age == p.age) { // return true; //} else { // return false; //} return this.name.equals(p.name) && this.age == p.age; } }
标签:name,age,public,person,compareTo,方法,比较,String From: https://www.cnblogs.com/shuqiqi/p/16974230.html