在千峰“逆战班”学习的第20天
清醒温柔知进退,努力上进且优秀
今天的学习内容内部类中的静态内部类、局部内部类、匿名内部类。Object类中的getClass()方法、hashCode()方法、toString()方法、equals()方法。
中国加油!武汉加油!千峰加油!我自己加油
Question9习题
2. 运行结果为:
null 0
Tom 18
3. 运行结果为:
true
false
4. 运行结果为:
true
false
6. ABCD
7.
package com.qf.day19.question;
public class TestWorker {
public static void main(String[] args) {
Object w1 = new Worker("Tom",22,"13811111111",9500.0);
Object w2 = new Worker("Tony",24,"13822222223",8500.0);
Object w3 = new Worker("Tony",24,"13822222223",8500.0);
System.out.println(w1);
System.out.println(w2.toString());
System.out.println(w3.toString());
System.out.println(w1.equals(w2));
System.out.println(w1.equals(w3));
System.out.println(w2.equals(w3));
}
}
class Worker{
String name;
int age;
String phone;
double salary;
public Worker(){}
public Worker(String name,int age,String phone,double salary){
super();
this.name = name;
this.age = age;
this.phone = phone;
this.salary = salary;
}
public String toString(){
return this.name + "\t" + this.age + "\t" + this.phone + "\t" + this.salary;
}
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.phone.equals(w.phone) && this.salary == w.salary){
return true;
}
return false;
}
}
运行结果:
- ABCD
package com.qf.day19.question;
public class TestLamp {
public static void main(String[] args){
Lamp lamp = new Lamp();
class Red implements Light{
public void shine(){
System.out.println("shine in red");
}
}
Light light = new Red();
lamp.on(light);
Light light1 = new Light(){
public void shine(){
System.out.println("shine in yellow");
}
};
lamp.on(light1);
}
}
interface Light{
void shine();
}
class Lamp{
public void on(Light light){
light.shine();
}
}
运行结果: