public class Demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/* Anonymous inner class: It is the simplified way of writing inner class.
Premise: There is a class or interface The class
here can be a concrete class or an abstract class.
Format:
new class name or interface name () { override method; }
What is the essence?
It is a subclass anonymous object that inherits the class or implements the interface. */
/*Cat cat = new Cat();
cat.eat();*/
//1. Anonymous inner class: the class that implements the abstract class
//The abstract class points to the anonymous inner class object
Animal dog = new Animal(){
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("Dogs eat meat...");
}
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("Sleep on your stomach...");
}
};
dog.eat();
dog.sleep();
SpiderMan sMan = new SpiderMan();
sMan.eat();
//The interface cannot be new
//Anonymous inner class: implement the interface
//The interface points to the anonymous inner class object
Man hipHopMan = new Man(){
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("hipHopMan eats chicken legs...");
}
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("侧着睡。。。");
}
};
hipHopMan.eat();
hipHopMan.sleep();
}
}
interface Man{
public void eat();
public void sleep();
}
class SpiderMan implements Man{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("Spider Man 吃钢铁...");
}
@Override
public void sleep() {
// TODO Auto-generated method stub
}
}
abstract class Animal{
public abstract void eat();
public abstract void sleep();
}
class Cat extends Animal{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("猫吃鱼...");
}
@Override
public void sleep() {
// TODO Auto-generated method stub
}
}