一个类的定义放在另一个类的内部,这个类就叫做内部类。
public class First { public class Contents{ public void f(){ System.out.println("In Class First's inner Class Contents method f()"); } } }
public class First { public class Contents{ public void getStr(){ System.out.println("First.str="+str); } } private String str; }
private int num ; public Test2(){ } public Test2(int num){ this.num = num; } private class Inner{ public Test2 getTest2(){ return Test2.this; } public Test2 newTest2(){ return new Test2(); } } public static void main(String [] args){ Test2 test = new Test2(5); Test2.Inner inner = test.new Inner(); Test2 test2 = inner.getTest2(); Test2 test3 = inner.newTest2(); System.out.println(test2.num); System.out.println(test3.num); }
必须是外围类对象.new,而不能是外围类.new
public class First { public class Contents{ public void f(){ System.out.println("In Class First's inner Class Contents method f()"); } public void getStr(){ System.out.println("First.str="+str); } } public static void main(String [] args){ First first = new First(); First.Contents contents = first.new Contents(); contents.f(); } }
public interface Shape { public void paint(); } public class Painter { private class InnerShape implements Shape{ public void paint(){ System.out.println("painter paint() method"); } } public Shape getShape(){ return new InnerShape(); } public static void main(String []args){ Painter painter = new Painter(); Shape shape = painter. getShape(); shape.paint(); } }
public void test(){
class Inner{
public void method(){
System.out.println("在方法内创建的类");
}
}
}
值得注意的是:方法内创建的类,不能加访问修饰符。
另外,方法内部的类也不是在调用方法时才会创建的,它们一样也被编译了(怎么知道的?后面会有讲解)。
6、匿名内部类
public class Painter { public Shape getShape(){ return new Shape(){ public void paint(){ System.out.println("painter paint() method"); } }; } public static void main(String [] args){ Painter painter = new Painter(); Shape shape = painter.getShape(); shape.paint(); } } public interface Shape { public void paint(); }
public class B { public A getA(int num){ return new A(num){ }; } } public class A { private int num; public A(int num){ this.num = num; } public A(){ } }
public class B { public A getA(final int num){ return new A(num){ public int getNum(){ return num; } }; } } public class A { private int num; public A(int num){ this.num = num; } public A(){ } }
public A getA(){ return new A(){ int num = 0; String str; { str = "javaeye"; System.out.println("hello robbin"); } }; }
public interface Service { public void method1(); } public interface ServiceFactory { Service getService(); } public class Implemention1 implements Service{ public void method1(){ System.out.println("In Implemention1 method method1()"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService(){ return new Implemention1(); } }; } public class Implemention2 implements Service { public void method1(){ System.out.println("in Implemention2 method method1()"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService(){ return new Implemention2(); } }; } public class Test { public static void main(String []args){ service(Implemention1.factory); service(Implemention2.factory); ServiceFactory factory1 = Implemention1.factory; Service service1 = factory1.getService(); service1.method1(); ServiceFactory factory2 = Implemention1.factory; Service service2 = factory2.getService(); service2.method1(); } }
public class StaticClass { private int num; private static int sum = 2; private static class StaticInnerClass{ public int getNum(){ //只能访问sum,不能访问num return sum; } } } public class Test { public static void main(String [] args){ //可以直接通过new来创建嵌套类对象 StaticClass.StaticInnerClass inner = new StaticClass.StaticInnerClass(); inner.getNum(); } }
public interface One { public void inOne(); } public interface Two { public void inTwo(); } //两个接口,用普通类就可实现多重继承 public class CommonClass implements One,Two { public void inOne(){ System.out.println("CommonClass inOne() method"); } public void inTwo(){ System.out.println("CommonClass inTwo() method"); } } public abstract class Three { public abstract void inThree(); } public abstract class Four { public abstract void inFour(); } //两个抽象类,使用普通类无法实现多重继承 //使用内部类可以实现 public class Contents extends Three { public void inThree(){ System.out.println("In Contents inThress() method"); } public class InnerFour extends Four{ public void inFour(){ System.out.println("In Contents"); } } }