Thinking In Patterns
2014-11-03 23:18
134 查看
首先要用到静态类的概念:静态类是指在一个类的内部,又定义了一个静态的内部类。内部类是指只在该类中使用到内部类,static指的是随着类的加载就会被调用,而不是类对象的创建。点击打开链接
静态内部类的用法包括如下:
(1) 使用静态内部类设置主程序入口
静态内部类的用法包括如下:
(1) 使用静态内部类设置主程序入口
public class MainInStaticClass { static class Main{ public static void main(String[] args){ new MainInStaticClass().print(); } public void print(){ System.out.println("main in static inner class"); } } public class TestMain { public static void main(String[] args) { new MainInStaticClass.Main.main(); } }
//: singleton:PoolManager.java package singleton; import java.util.*; public class PoolManager { private static class PoolItem { boolean inUse = false; Object item; PoolItem(Object item) { this.item = item; } } private ArrayList items = new ArrayList(); public void add(Object item) { items.add(new PoolItem(item)); } static class EmptyPoolException extends Exception {} public Object get() throws EmptyPoolException { for(int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem)items.get(i); if(pitem.inUse == false) { pitem.inUse = true; return pitem.item; } } // Fail early: throw new EmptyPoolException(); // return null; // Delayed failure } public void release(Object item) { for(int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem)items.get(i); if(item == pitem.item){ pitem.inUse = false; return; } } throw new RuntimeException(item + " not found"); } } ///:~ //: singleton:ConnectionPoolDemo.java package singleton; import junit.framework.*; interface Connection { Object get(); void set(Object x); } class ConnectionImplementation implements Connection { public Object get() { return null; } public void set(Object s) {} } class ConnectionPool { // A singleton private static PoolManager pool = new PoolManager(); public static void addConnections(int number) { for(int i = 0; i < number; i++) pool.add(new ConnectionImplementation()); } public static Connection getConnection() throws PoolManager.EmptyPoolException { return (Connection)pool.get(); } public static void releaseConnection(Connection c) { pool.release(c); } } public class ConnectionPoolDemo extends TestCase { static { ConnectionPool.addConnections(5); } public void test() { Connection c = null; try { c = ConnectionPool.getConnection(); } catch (PoolManager.EmptyPoolException e) { throw new RuntimeException(e); } c.set(new Object()); c.get(); ConnectionPool.releaseConnection(c); } public void test2() { Connection c = null; try { c = ConnectionPool.getConnection(); } catch (PoolManager.EmptyPoolException e) { throw new RuntimeException(e); } c.set(new Object()); c.get(); ConnectionPool.releaseConnection(c); } public static void main(String args[]) { junit.textui.TestRunner.run(ConnectionPoolDemo.class); } }
相关文章推荐
- Thinking in patterns
- thinking in java test4.3练习(1)(2)(3)(4)(5)用三元操作符和按位操作来显示二进制
- Note of 《thinking in java》5
- 学习总结-Thinking In Java Chapter 13 strings
- Reading "Thinking in Java" #5
- Windows-to-Linux roadmap: Part 1. Thinking in Linux
- java学习计划(Thinking in Java 3th)
- 《Thinking in UML》读书笔记 4 : 用例(Use Case)
- 读书笔记-Thinking in C++-第7章 函数重载和默认参数Function Overloading &Default Arguments
- Thinking in Java 4th - 第8章 多态
- 《Thinking In Algorithm》09.彻底理解递归
- Thinking in java 读书笔记(七.2:内部类:闭包和控制框架)
- Thinking in Java 笔记
- 《thinking-in-java》读书笔记-第15章-泛型(一)
- Thinking In Java 之多线程 5
- patterns in data
- AS3 Strategy策略模式翻译(Design Patterns in ActionScript)
- Thinking in AJAX(三) —— AJAX框架汇总
- Function Overloading & Default Arguments(Chapter 7 of Thinking in C++)
- 【ThinkingInC++】24、基本对象,用struct写一个类