Java内部类总结(下)

五、匿名类
 
匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象。


interface Foo {
	void say();
}
public class lzwCode {
    
	public Foo f = new Foo(){
		public void say() {
			System.out.println("Say Foo!");
		}
	};

	public Foo test() {
		return new Foo(){
			public void say() {
				System.out.println("Test Say Foo!");
			}
		};
	}
	public static void main(String [] args) {
		lzwCode lc = new lzwCode();
		lc.f.say();
		lc.test().say();
	}
}

控制台结果:

Java内部类总结(下)_第1张图片

//经典实例,来自thining in java,有改动
interface Service {
	void mothodA();
	void mothodB();
}

interface ServiceFactory{
	Service getService();
}
//1
class ImplementsActionA implements Service {
	private ImplementsActionA() {
	}

	public void mothodA() {
		System.out.println("ImplementsActionA 实现 mothodA()方法");
	}

	public void mothodB() {
		System.out.println("ImplementsActionA 实现 mothodB()方法");
	}
	
	public static ServiceFactory factory = new ServiceFactory(){
		public Service getService() {
			return new ImplementsActionA();
		}
	};
}
//2
class ImplementsActionB implements Service {
	private ImplementsActionB() {
	}

	public void mothodA() {
		System.out.println("ImplementsActionB 实现 mothodA()方法");
	}

	public void mothodB() {
		System.out.println("ImplementsActionB 实现 mothodB()方法");
	}

	public static ServiceFactory factory = new ServiceFactory(){
		public Service getService() {
			return new ImplementsActionB();
		}
	};
}
//工厂
public class lzwCode {
    
	public static void serviceConsumer(ServiceFactory face) {
		Service ser = face.getService();
		ser.mothodA();
		ser.mothodB();
	}
	public static void main(String [] args) {
		serviceConsumer(ImplementsActionA.factory);
		serviceConsumer(ImplementsActionB.factory);
	}
}
控制台结果:

Java内部类总结(下)_第2张图片

六、静态内部类
 
静态内部类是static class型的内部类,这种内部类特点是:它不能访问外部类的非静态成员。要创建静态内部类对象时候,也不需要外部类对象了,直接可以:
new 外部类名.内部类构造方法

class Outer {
	public static int i = 404;

	protected static class Inner {
		int i = 110;
		String name;
		Inner(String name) {
			this.name = name;
		}
		void sayHello() {
			System.out.println("Hello "+name);
			Outer.i++;
			this.i++;
		}
	}

	public Inner getInner(String name) {
		return new Inner(name);
	}
}
public class lzwCode {
   
	public static void main(String [] args) {
		Outer.Inner inn = new Outer.Inner("梅西");
		inn.sayHello();
		System.out.println(Outer.i);
		System.out.println(inn.i);
	}
}

控制台结果:

Java内部类总结(下)_第3张图片


七、接口内部类
 
接口内部类自动都是public static的,相当于为接口定义了一种变量类型,这在java的设计中就有使用,比如在HashMap中,就有:
static class Entry<K,V> implements Map.Entry<K,V>

interface InterInner {

	void sayHello();
	
	class Inner implements InterInner {
		public void sayHello() {
			System.out.println("这是一个接口内部类");
		}
	}
}
public class lzwCode {
   
	public static void main(String [] args) {
		InterInner.Inner demo = new InterInner.Inner();
		demo.sayHello();
	}
}

控制台结果:

这是一个接口内部类



八、内部类的继承
 
内部类的继承,可以继承内部类,也可以继承外部类。

class Outer {

	public void OuterMothod() {
		System.out.println("outer mothod!");
	}

	class Inner {
		public void sayHello() {
			System.out.println("Hello Barcelona");
		}
	}

	class InnerA extends Outer{
		public void OuterMothod() {
			System.out.println("InnerA mothod!");
		}

		public void readMe() {
			System.out.println("InnerA O(∩_∩)O");
		}
	}

	class InnerB extends Inner {
		public void sayHello() {
			System.out.println("InnerB Hello Barcelona");
		}

		public void readYou() {
			System.out.println("InnerB O(∩_∩)O");
		}
	}
}
public class lzwCode {
   
	public static void main(String [] args) {
		Outer outer = new Outer(); 
		Outer.Inner inn = outer.new Inner();
		inn.sayHello();
        System.out.println("==========1===========");
		Outer.InnerA innA = outer.new InnerA();
		innA.OuterMothod();
		innA.readMe();
		System.out.println("==========2===========");
		Outer.InnerB innB = outer.new InnerB();
		innB.sayHello();
		innB.readYou();
	}
}

控制台结果:

Java内部类总结(下)_第4张图片

你可能感兴趣的:(Java内部类总结(下))