下面为什么相等??
- 01 class Person
- 02 {
- 03 String name ;
- 04 int age ;
- 05 Person(String name,int age)
- 06 {
- 07 this.name = name ;
- 08 this.age = age ;
- 09 }
- 10 boolean compare(Person p)
- 11 {
- 12 if(this.name.equals(p.name)&&this.age==p.age)
- 13 {
- 14 return true ;
- 15 }
- 16 else
- 17 {
- 18 return false ;
- 19 }
- 20 }
- 21 }
- 22
- 23 public class TestCompare
- 24 {
- 25 public static void main(String[] args)
- 26 {
- 27 Person p1 = new Person("张三",30);
- 28 Person p2 = new Person("张三",30);
- 29 System.out.println(p1.compare(p2)?"相等,是同一人!":"不相等,不是同一人!");
- 30 }
- 31 }
复制代码
相等,是同一人! |
|