内部类与匿名内部类实现接口

package com.lwf.thinking.eight;
interface Constants{
	void getName();
}
public class AnonymousInner {
	public Constants cont(){
		return new Constants(){
			public void getName(){
				System.out.println("cont");
			}
		};
	}
	
	private  class Inner implements Constants{
		public void getName(){
			System.out.println("Inner");
		}
	}
	
	public Constants getConstans(){
		Inner inner = new Inner();
		return inner;
	}
	
	
	public static void main(String[] args) {
		AnonymousInner out = new AnonymousInner();
		//匿名内部类实现
		Constants c = out.cont();
		c.getName();
		//内部类显式实现
		Constants c2 = out.getConstans();
		c2.getName();
		
	}
} 

 输出结果: cont
                 Inner

你可能感兴趣的:(C++,c,C#)