Java内部类复习

package com.t_06;

import org.junit.Test;

import com.t_06.StaticClass.StaticInnerClass;

/**
 * 一个类的定义放在另一个类的内部,这个类就叫内部类
 * @author Administrator
 *
 */
public class First {
    
    public class Contents{//像这样的,Contents就叫做内部类,内部类了解外部类,并能与之通信
        public void f(){
            System.out.println("In Class First's inner Class Contents method f()");
        }
    }
    @Test
    public void fun(){
        Second second=new Second();
        second.setStr("");
        Second.Contents contents=second.new Contents();//创建内部类对象使用的是外围类对象.new内部类对象的方式
        contents.f();
    }
    /**
     * 此时,内部类是private的,可以它的外围类Painter以外,没人能访问
     * 这样private内部类给类的设计者提供了一种途径,通过这种途径可以完全阻止任何依赖类型的编码,并完全隐藏实现的细节
     */
    @Test
    public void fun2(){
        Painter painter=new Painter();
        Shape shape=painter.getShape();
        shape.paint();
    }
    
    /**
     * 方法内部的类
     * 可以在方法内创建一个类
     * 值得注意的是:方法内部创建的类,不能加访问修饰符
     * 另外方法内部的类也不是在调用方法时才创建的,它们一样被编译了
     */
    public void test1(){
        class Inner{
            public void method(){
                System.out.println("在方法内部创建的类");
            }
        }
    }
    
    @Test
    public void fun3(){
        Painter painter=new Painter();
        Shape shape=painter.getShape1();
        shape.paint();
    }
    
    /**
     * 在implemention1和2中匿名内部类用在字段初始化的地方,这样定义的工厂方法是不是看起来更舒适些?
     * 没感觉!
     */
    @Test
    public void fun4(){
        service(implemention1.factory);
        service(implemention2.factory);
        
        ServiceFactory factory1=implemention1.factory;
        Service service1=factory1.getService();
        service1.method1();
        
        ServiceFactory factory2=implemention2.factory;
        Service service2=factory2.getService();
        service2.method1();
    }
    
     public void service(ServiceFactory factory){  
         Service service = factory.getService();  
         service.method1();
    }  
     @Test
     public void fun5(){
         StaticClass.StaticInnerClass inner=new StaticClass.StaticInnerClass();
         System.out.println(inner.getNum());
     }

}

class Second{
    /**
     * 创建了内部类对象时,他会与创造它的外围对象有了某种联系,于是能访问外围类的所有成员,不需要任何特殊条件。
     */
    private String str;
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    public class Contents{
        public void f(){
            System.out.println("First.str="+str);
        }
    }
}

/**
 * 内部类与向上转型
 * @author Administrator
 *
 */
interface Shape{
    public void paint();
}
class Painter{
    private class InnerShape implements Shape{

        public void paint() {
            System.out.println("painter paint() method");
        }
        
    }
    public Shape getShape(){
        return new InnerShape();
    }
    
    /**
     * 匿名内部类,注意后面要有一个分号,如果匿名内部类中需要使用一个参数,那么该参数一定是final类型的
     * 匿名内部类里面没有构造器,如果想要模仿构造器效果,可以采用代码块{}
     * 
     */
    public Shape getShape1(){
        return new Shape(){

            public void paint() {
                System.out.println("painter paint() method");
            }
            {
                System.out.println("init method!");
            }
            
        };
    }
}
/**
 * 通过匿名内部类改造工厂方法
 *
 */
interface Service{
    public void method1();
}
interface ServiceFactory{
    Service getService();
}
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();
        }
        
    };
    
}
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();
        }
        
    };
    
}

/**
 * 嵌套类
 * static的内部类就叫嵌套类
 * 嵌套类是一个例外,使用嵌套类的时候注意:
 *     嵌套类直接通过new来创建
 *     在嵌套类中,不能像普通内部类一样访问外部类的非static成员
 *  嵌套类中可以有static方法,不同内部类中不允许有这个。
 */

class StaticClass{
    private int num;
    private static int sum=2;
    public static class StaticInnerClass{
        public int getNum(){
            return sum;
        }
    }
}

/**
 * 为什么要内部类
 *     a、内部类提供了某种进入外围类的窗户。
 *     b、每个内部类都能独立继承一个类,无论父类是否已经继承了某个类
 */

 

posted @ 2016-10-30 12:53  guodaxia  阅读(239)  评论(0编辑  收藏  举报