接口丶匿名类丶向上造型

public interface Animal {
	void eat();
	void move();
}


public class Cat implements Animal{

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("Cat is eating!");
		
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub
		System.out.println("Cat is moving");		
	}
	
}


public class Dog implements Animal{

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("Dog is eating");
		
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub
		System.out.println("Dog is moving");		
	}

}



public class Test {

	public static void main(String[] args) {		
		haveLunch(new Cat());
		haveLunch(new Dog());
		haveLunch(new Animal() {
			
			@Override
			public void move() {
				// TODO Auto-generated method stub
				System.out.println("你走个锤子");
				
			}
			
			@Override
			public void eat() {
				// TODO Auto-generated method stub
				System.out.println("你吃个锤子");				
			}
		});
	}
	
	public static void haveLunch(Animal a) {
		a.eat();
	}

}

测试:
接口丶匿名类丶向上造型_第1张图片

你可能感兴趣的:(java学习)