您的位置:首页 > 编程语言 > Java开发

Java的内部类的创建和实现演示

2012-11-01 16:15 211 查看
Java的内部类

创建内部类

//创建内部类
public class OuterClass
{
	class InnerClass{		//内部类
		public void getStr()
		{
			System.out.println("String");
		}	
	}	
	public InnerClass getSelector(){
		return new InnerClass();
	}
	public static void main(String[] args)
	{
		OuterClass fx = new OuterClass();
		fx.getSelector().getStr();
	}
}




链接到外部类

//链接到外部类
interface Selector
{
	boolean end();

	Object current();

	void next();
}
public class InnerClass
{
	private Object[] item;
	private int next = 0;

	
	public InnerClass(int size)
	{
		item = new Object[size];
	}

	public void add(Object x)
	{
		if (next < item.length)
		{
			item[next++] = x;
		}
	}
	//内部类
	//内部类可以链接到外部类,
	//内部类拥有其外部类的所有元素的访问权
	private class SequenceSelector implements Selector
	{
		private int i = 0;

		public Object current()
		{
			return item[i];
		}

		public boolean end()
		{
			return i == item.length;
		}

		public void next()
		{
			if (i < item.length)
			{
				i++;
			}
		}
	}
	public static void main(String[] args)
	{
		InnerClass inner = new InnerClass(10);
		for(int i=0;i<10;i++)
		{
			inner.add(i);
		}
		Selector selector = inner.new SequenceSelector();
		while(!selector.end())
		{
			System.out.println(selector.current());
			selector.next();
		}
	}
}




内部类使用this与.new

//这个是通过this引用外部类的成员
public class InnerClass2
{
	void f(){System.out.println("~~~~~~~~~~~~~~~~~~f()");}
	public class Inner
	{
		public InnerClass2 outer()
		{
			return InnerClass2.this;
		}
	}
	public Inner inner(){
		
		return new Inner();
	}
	public static void main(String[] args)
	{
		InnerClass2 inner = new InnerClass2();
		InnerClass2.Inner dt = inner.inner();
                  //InnerClass2.Inner dt = inner.new Inner();
		dt.outer().f();
		 
	}
}




内部类向上转型

interface Fruit
{
	void sayFruit();
}
// 内部类向上转型例子
class FruitSubClass
{
	private class Apple implements Fruit
	{
		public void sayFruit()
		{
			System.out.print("苹果");
		}
	}

	// 在这里是向上转型的操作
	public Fruit getApple()
	{
		return new Apple();
	}
}
public class Parce1
{
	public static void main(String[] args)
	{
		FruitSubClass f = new FruitSubClass();
		Fruit fruit = f.getApple();
		fruit.sayFruit();
	}
}




在方法和作用域内的内部类

//在方法上的内部类
interface Destinaltion
{
	void readLabel();
}
public class Prace2
{
	public Destinaltion destinaltion(String s)
	{
		class PDestination implements Destinaltion
		{
			private String label;

			public PDestination(String whereTo)
			{
				label = whereTo;
			}

			public void readLabel()
			{
				System.out.println(label);
			}
		}
		//PDdestination对象只能是通过destination方法访问,它之外的方法都不能访问
		return new PDestination(s);
	}
	public static void main(String[] args)
	{
		Prace2 prace2 = new Prace2();
		Destinaltion dest = prace2.destinaltion("label");
		dest.readLabel();
	}
}
//在作用域上的内部类
interface CP3
{
	void sayLiner();
}
public class Prace3
{
	CP3 p3 = null;
	public void methos1(boolean b)
	{
		if (b)
		{
			class ClassPrace3 implements CP3
			{
				String str = null;

				public void sayLiner()
				{
					System.out.println("ClassPrace3");
				}
			}
			// 创建new ClassPrace3()是在if的作用域内的
			p3 = new ClassPrace3();
			p3.sayLiner();
		}
	}
	public static void main(String[] args)
	{
		Prace3 prace3 = new Prace3();
		prace3.methos1(true);
	}
}




匿名内部类

public class Prace4
{
	public Destinaltion destinaltion()
	{
		return new Destinaltion()
		{
			public void readLabel()
			{
				System.out.print("匿名内部类");
			}
		};
	}

	public static void main(String[] args)
	{
		Prace4 p4 = new Prace4();
		Destinaltion d = p4.destinaltion();
		d.readLabel();
	}
}
//对匿名内部类的一个简化
public class Prace5

{
	public class MyDestination implements Destinaltion
	{
		public void readLabel()
		{
			System.out.print("匿名内部类");
		}
	}
	
	public Destinaltion destinaltion()
	{
		return new MyDestination();
	}

	public static void main(String[] args)
	{
		Prace5 p5 = new Prace5();
		Destinaltion d = p5.destinaltion();
		d.readLabel();
	}
}


在匿名内部类时初始化字段

//给匿名内部类中的字段初始化
public class Prace6
{
	public Destinaltion destinaltion(final String str)
	{
		return new Destinaltion()
		{
			public void readLabel()
			{
				System.out.println(str);
			}
		};
	}

	public static void main(String[] args)
	{
		Prace6 p6 = new Prace6();
		p6.destinaltion("给匿名内部类中的字段初始化").readLabel();
	}

给匿名内部类的构造器初始化参数

//给匿名内部类的构造器初始化参数
abstract class Base{
	public Base(int i)
	{
		System.out.println("输出的i="+i);
	}
	public abstract void f();
}

public class Prace7
{
	//这里的i不需要定义为final类型
	public Base getBase(int i)
	{
		return new Base(i)
		{
			public void f(){};
		};
	}
	
	public static void main(String[] args)
	{
		Prace7 p7 = new Prace7();
		p7.getBase(10);
	}
}


再访工厂方法

//再访工厂方法
interface Service
{
	void method1();

	void methos2();
}
interface ServiceFactory
{
	Service getService();
}
class Implemention1 implements Service
{
	public void method1()
	{
		System.out.println("Implemention1's Method1");
	}

	public void methos2()
	{
		System.out.println("Implemention1's methos2");
	}
	public static ServiceFactory factory = new ServiceFactory()
	{
		public Service getService()
		{
			return new Implemention1();
		}
	};
}
class Implemention2 implements Service
{
	public void method1()
	{
		System.out.println("Implemention2's Method1");
	}

	public void methos2()
	{
		System.out.println("Implemention2's methos2");
	}
	public static ServiceFactory factory = new ServiceFactory()
	{
		public Service getService()
		{
			return new Implemention2();
		}
	};
}
public class Prace8
{
	
	public void ServiceCustomer(ServiceFactory serviceFactory)
	{
		Service service = serviceFactory.getService();
		service.method1();
		service.methos2();
	}
	
	public static void main(String[] args)
	{
		Prace8 p8 = new Prace8();
		p8.ServiceCustomer(Implemention1.factory);
		p8.ServiceCustomer(Implemention2.factory);
	}
}


闭包与回调

interface Incrementable{
	void increment();
}
class Callee1 implements Incrementable
{
	private int i=0;
	public void increment()
	{
		i++;
		System.out.println(i);
	}
	
}

class MyIncrement
{
	public void increment(){System.out.println("Other operation");}
	static void f(MyIncrement mi){mi.increment();};
}

class Callee2 extends MyIncrement
{
	private int i=0;
	public void increment()
	{
		i++;
		System.out.println(i);
	}
	private class Closure implements Incrementable
	{

		public void increment()
		{
			Callee2.this.increment();
		}
		
	}
	Incrementable getCallbackReferentce()
	{
		return new Closure();
	}
}
class Caller
{
	private Incrementable callbackReference;
	Caller(Incrementable cbh)
	{
		callbackReference = cbh;
	}
	//内部类 调用内部类的increment()
	//内部类的incremnt里的方法在调用外部类的increment()
	void go(){callbackReference.increment();}
}

public class Prace9
{
 
	public static void main(String[] args)
	{
		Callee1 c1 = new Callee1();
		Callee2 c2 = new Callee2();
		MyIncrement.f(c2);		
		Caller caller1 = new Caller(c1);
		Caller caller2 = new Caller(c2.getCallbackReferentce());		
		//caller1.go();
		caller2.go();
		caller2.go();
		caller2.go();
	}
}






总结:

为什么要使用内部类?

1.内部类提供了某种进入外部类的窗口

2.每个内部类都能独立地继承自一个(接口的)实现,所以无论外部类是否已经继承了某个(接口的)实现,对于内部灯都没有影响

3.内部类可以实现多重继承(如果拥有的是抽象类或具体,而不是接口,可以使用内部类实现多重继承)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: