接口的实现:
interface Animal {
public void eat();
}
public class Person {
public static void main(String[] args) {
Animal animal = new Animal() {
public void eat() {
System.out.print("person is eating");
}
};
animal.eat();
}
}
抽象类的实现:
abstract class Place{
private String name;
private String history;
public void getName(){
System.out.print("xihu is a place");
}
public Place(){};
public Place(String name,String history){
this.name = name;
this.history = history;
}
}
public class Dog {
public static void main(String[] args) {
//productor without param
Place hangzhou = new Place(){
public void getName(){
System.out.print("hangzhou is a place!");
}
};
hangzhou.getName();
//productor with param
Place xianghu = new Place("xianghu","100"){
public void getHistory(){
System.out.print("xianghu is a beautiful place!");
}
};
xianghu.getName();
}
}