import java.util.Objects;
public class TestObject {
public static void main(String[] args) {
Employee e1 = new Employee(1001,"张三");
Employee e2 = new Employee(1001,"张三");
System.out.println(e1);
System.out.println(e1==e2);
System.out.println(e1.equals(e2));
}
}
class Employee extends Object{
int id;
String name;
public Employee(int id,String name){
this.id = id;
this.name = name;
}
public String toString(){
return "雇员编号:"+id+",姓名:"+name;
}
public boolean equals(Object o){
if (this==o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id == employee.id;
}
public int hashCode() {
return Objects.hash(id);
}
}
```java
public class TestSuper {
public static void main(String[] args) {
Child c = new Child();
c.show();
}
}
class Parent {
int num = 300;
public void show(){
System.out.println("父类中,show()");
}
public Parent(){
System.out.println("初始化父类对象!");
}
}
class Child extends Parent{
int num = 1000;
public Child(){
super();
System.out.println("初始化子类对象!");
}
@Override
public void show() {
System.out.println("子类中,show()");
super.show();
System.out.println("子类中,num:"+num);
System.out.println("父类的num:"+super.num);
}
}