package top.qaqaq.java.P313;
import java.util.Objects;
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("人:吃饭");
}
public void walk() {
System.out.println("人:走路");
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
return age == other.age && Objects.equals(name, other.name);
}
// @Override
// public boolean equals(Object obj) {
// if(obj == this) {
// return true;
// }
//
// if(obj instanceof Person) {
// Person p = (Person)obj;
// return this.age == p.age && this.name.equals(p.name);
// }
//
// return false;
// }
}
package top.qaqaq.java.P313;
public class Man extends Person {
public Man(String name, int age) {
super(name, age);
}
public void earnMoney() {
System.out.println("男人负责挣钱养家");
}
public void eat() {
System.out.println("男人多吃肉,长肌肉");
}
public void walk() {
System.out.println("男人霸气的走路");
}
}
package top.qaqaq.java.P313;
import org.junit.Test;
public class ReviewTest {
//关于toString()
@Test
public void test3() {
String s = "abc";
s = null;
System.out.println(s);
System.out.println("********************");
System.out.println(s.toString());//出现NullPointerException
}
//区别手动写的和自动生成的equals()
@Test
public void test2() {
Person p = new Person("Tom", 12);
Man m = new Man("Tom", 12);
System.out.println(p.equals(m));
}
//数组也作为Object类的子类出现,可以调用Object类中声明的方法
@Test
public void test1() {
int[] arr = new int[] {1,2,3};
print(arr);
System.out.println(arr.getClass().getSuperclass());
}
public void print(Object obj) {
System.out.println(obj);
}
}