本帖最后由 杜光 于 2013-7-27 15:01 编辑
- package qbb;
- class Person{
- private int age;
- Person(int age){
- this.age = age;
- }
- public boolean equals(Person per){ //覆盖父类的方法
- return this.age == per.age;
- }
- }
- class Car{
-
- }
- public class Test{
- public static void main(String[] args){
- Person per1 = new Person(20);
- Person per2 = new Person(20);
- Person per3 = per1;
- Car car1 = new Car();
- System.out.println(per1.equals(per2));
- System.out.println(per1.equals(per3));
- System.out.println(per1.equals(car1));
- }
- }
复制代码运行结果: true true false
System.out.println(per1.equals(car1));参数传的不对,但为什么可以编译,运行?
|