//定义一个接口,并使用匿名内部类方式创建接口的实例
// Graph.java
public interface Graph
{
void draw();
{
public void draw(){ System.out.println("Triangle()::draw()"); }
}
{
void f(Graph graph)
{
graph.draw();
}
public static void main(String[] args)
{
GraphTest gt = new GraphTest();
gt.f(new Graph() {
public void draw() { System.out.println("匿名内部类");} });
gt.f (new Triangle());
}
}
// Graph.java
public interface Graph
{
void draw();
}
// Triangle.java
class Triangle implements Graph{
public void draw(){ System.out.println("Triangle()::draw()"); }
}
// GraphTest.java
public class GraphTest{
void f(Graph graph)
{
graph.draw();
}
public static void main(String[] args)
{
GraphTest gt = new GraphTest();
gt.f(new Graph() {
public void draw() { System.out.println("匿名内部类");} });
gt.f (new Triangle());
}
}