- null 0
Tom 18 - true
false - true
false - ABCD
相当于覆盖object中的toString、equals方法
源代码:
package day20.t1;
public class TestQ9_6 {
public static void main(String[] args) {
Worker w1=new Worker("Tom",19,5000.0);
System.out.println(w1.toString());
Worker w2=new Worker("jack",20,5000.0);
Worker w3=new Worker("Tom",19,5000.0);
System.out.println(w1.equals(w3));
System.out.println(w2.equals(w3));
}
}
class Worker{
String name;
int age;
double salary;
public Worker(){}
public Worker(String name,int age,double salary){
this.name=name;
this.age=age;
this.salary=salary;
}
@Override
public String toString() {
return name + ", " + age + ", " + salary ;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(this.getClass() != obj.getClass() ){
return false;
}
Worker w=(Worker)obj;
if(this.name.equals(w.name) && this.age==w.age && this.salary==w.salary ){
return true;
}
return false;
}
}
运行效果图:
9.
ABCD
源代码:
package day20.t2;
public class TestQ9_10 {
public static void main(String[] args) {
Lamp lamp=new Lamp();
class RedLight implements Light{
@Override
public void shine() {
System.out.println("shine in red");
}
}
lamp.on(new RedLight());
Light l = new Light(){
public void shine(){
System.out.println("shine in yellow");
}
};
lamp.on(l);
}
}
interface Light{
void shine();
}
class Lamp{
public void on(Light light){
light.shine();
}
}
运行效果图: