zoukankan      html  css  js  c++  java
  • Java内部类应该场景

    场景一:当某个类除了它的外部类,不再被其他的类使用时

    场景二:解决一些非面向对象的语句块:try{}catch{}finally{}

    public interface DataManager { 
        public void manageData(); 
    } 
    public class DataTemplate{ 
        public void execute(DataManager dm) { 
            try {
                dm.manageData(); 
            } catch(Exception e) { 
                LoggerAgent.error("GetHeaderData", "getDivisionData", "SQLException: " + e); 
                e.printStackTrace();
            }finally{ 
                manager.close(stmt); 
                manager.releaseConnection(conn); 
            } 
        } 
    } 
    
     
    new DataTemplate().execute(new DataManager() { 
        public void manageData() { 
            String[] divisionData = null; 
            conn = manager.getInstance().getConnection();
            stmt = (OracleCallableStatement)conn.prepareCall("{ Call PM_GET_PRODUCT.HEADER_DIVISION(?, ?) }"); 
            stmt.setLong(1 ,productId.longValue() ); 
            stmt.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);  
            stmt.execute(); 
            ResultSet rs = stmt.getCursor(2);
            int i = 0  
            String strDivision = ""; 
            while( rs.next() ) { 
                strDivision += rs.getString("DIVISION_ID") + ","  
            } 
            int length = strDivision.length();
            if(length != 0 ) { 
                strDivision = strDivision.substring(0,length - 1);    
            } 
            divisionData = StringUtil.split(strDivision, ",");
            map.put("Division", strDivision );
            LoggerAgent.debug("GetHeaderProcess","getDivisionData","getValue+" + strDivision +" " + productId);
    } 
    }); 

    场景之三:一些多算法场合:

    Arrays.sort(emps,new Comparator(){
    Public int compare(Object o1,Object o2)
    {
    return ((Employee)o1).getServedYears()-
    ((Employee)o2).getServedYears(); 
    }
    });

    场景之四:适当使用内部类,使得代码更加灵活和富有扩展性

    package polyFactory; 
    public interface Shape { 
        public void draw();
        public void erase(); 
    } 
    
    package polyFactory; 
    import java.util.HashMap; 
    import java.util.Map; 
    public abstract class ShapeFactory { 
    protected abstract Shape create(); 
    private static Map factories = new HashMap(); 
    public static void addFactory(String id,ShapeFactory f) { 
        factories.put(id,f); 
    } 
    public static final Shape createShape(String id) { 
        if(!factories.containsKey(id)) {
        try { 
            Class.forName("polyFactory."+id); 
        } catch(ClassNotFoundException e) {   
            throw new RuntimeException("Bad shape creation : "+id); 
        } 
    }
    return ((ShapeFactory)factories. get(id)).create();
     }
     } 
      
    package polyFactory; 
    public class Circle implements Shape { 
        public void draw() {  
            System.out.println("the circle is drawing..."); 
        } 
        public void erase() { 
            System.out.println("the circle is erasing..."); 
        } 
        private static class Factory extends ShapeFactory{ 
            protected Shape create() { 
                return new Circle(); 
            } 
        } 
        static {ShapeFactory.addFactory("Circle",new Factory());} 
    } 
    package polyFactory; 
    public class Square implements Shape { 
        public void draw() { 
            System.out.println("the square is drawing..."); 
        } 
        public void erase() { 
            System.out.println("the square is erasing..."); 
        } 
        private static class Factory extends ShapeFactory  { 
            protected Shape create() {
                return new Square(); 
            } 
        } 
        static {ShapeFactory.addFactory("Square",new Factory());} 
    } 
  • 相关阅读:
    bootstrap基本用法
    Maven学习笔记(一)
    Tomcat的安装以及基本配置
    jQuery实现用户头像裁剪插件cropbox.js
    position的用法与心得
    ES6新特性学习(一)
    jQuery mobile 滑动打开面板
    vue-day05----自定义指令(directive)、render和template的区别、mixin混入、Vue.use()、Vue.extend()、Vue.filter()、vue中的数据流向
    我的一个React路由嵌套(多级路由),路由传参之旅
    vue04----watch、slot、响应式原理、set、vue脚手架(vue-cli)、单页面应用和多页面应用、单页面开发首屏加载过慢,白屏如何缓解
  • 原文地址:https://www.cnblogs.com/winson/p/3209077.html
Copyright © 2011-2022 走看看