日期:2014-05-20 浏览次数:20876 次
class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public boolean compare(Person per){ Person p1=this; Person p2=per; if(p1==p2){ return true; } if(p1.name.equals(p2.name)&&p1.age==p2.age){ return true; }else{ return false; } } } public class Demo{ public static void main(String []args){ Person p1=new Person("sunxiaolong",35); Person p2=new Person("nixiaohuan",26); if(p1.compare(p2)){ System.out.println("想等"); }else{ System.out.println("不相等"); } } }
------解决方案--------------------
javac Demo.java
java Demo
结果是不相等
------解决方案--------------------
//基本语法错误,
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public boolean compare(Person per) { Person p1 = this; Person p2 = per; if (p1 == p2) { return true; } if (p1.name.equals(p2.name) && p1.age == p2.age) { return true; } else { return false; } } } public class Demo { public static void main(String[] args) { Person p1 = new Person("sunxiaolong", 35); Person p2 = new Person("nixiaohuan", 26); if (p1.compare(p2)) { System.out.println("想等"); } else { System.out.println("不相等"); } } }
------解决方案--------------------
public static void main(String []args)
public static void main(String[] args)
自己看看。
------解决方案--------------------
public class Demo前面少了一个“}”。
------解决方案--------------------
class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public boolean compare(Person per){ Person p1=this; Person p2=per; if(p1==p2){ return true; } if(p1.name.equals(p2.name)&&p1.age==p2.age){ return true; }else{ return false; } }}//括号加在这里 最后的去掉 public class Demo{ public static void main(String[] args){ Person p1=new Person("sunxiaolong",35); Person p2=new Person("nixiaohuan",26); if(p1.compare(p2)){ System.out.println("想等"); } else{ System.out.println("不相等"); } } }