`
qq405371160
  • 浏览: 32167 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
单例 设计模式
public class Singleton {
	
	private static volatile Singleton st;

       private Singleton (){}

	public static Singleton getInstance() {
		if (null == st) {
			synchronized (Singleton.class) {
				if (st == null) {
					st = new Singleton();
					return st;
				}
			}
		}
		return st;
	}
}


来自http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom
public class Something {
        private Something() {
        }
 
        private static class LazyHolder {
                public static final Something INSTANCE = new Something();
        }
 
        public static Something getInstance() {
                return LazyHolder.INSTANCE;
        }
}

java 创建缩略图 java java 创建缩略图
public class Thumbnail {
	public static void main(String[] args) throws ImageFormatException, InterruptedException, IOException {
		createThumbnail("D:\\1.png",200,200,100,"D:\\11.jpg");
	}

	private static void createThumbnail(String filename, int thumbWidth,
			int thumbHeight, int quality, String outFilename)
			throws InterruptedException, ImageFormatException, IOException {
		Image image = Toolkit.getDefaultToolkit().getImage(filename);
		MediaTracker mediaTracker = new MediaTracker(new Container());
		mediaTracker.addImage(image, 0);
		mediaTracker.waitForID(0);
		double thumbRatio = (double) thumbWidth / (double) thumbHeight;
		int imageWidth = image.getWidth(null);
		int imageHeight = image.getHeight(null);
		double imageRatio = (double) imageWidth / (double)imageHeight;
		if (thumbRatio < imageRatio)
		{
			thumbHeight = (int) (thumbWidth /imageRatio);
		} else {
			thumbWidth = (int)(thumbHeight * imageRatio);
		}
		BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight,BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2D = thumbImage.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
		quality = Math.max(0, Math.min(quality, 100));
		param.setQuality((float) quality / 100.0f, false);
		encoder.setJPEGEncodeParam(param);
		encoder.encode(thumbImage);
		out.close();
	}
}
Java反射机制访问父类私有成员及私有方法 java
package a.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Children extends Parent
{
    public Children()
    {
        super();
    }

    public static void main(String[] args) throws Exception
    {
        Children c = new Children();
        Field field = getField("a.test.Parent", "name");
        //设置private字段可访问
        field.setAccessible(true);
        String parentName = (String)field.get(c);
        System.out.println("parentName - " + parentName);
        
        field.set(c, "liqiulin2");
        parentName = (String)field.get(c);
        System.out.println("parentName - " + parentName);
        
        Method m = getMethod("a.test.Parent", "getAge");
        //设置private方法可访问
        m.setAccessible(true);
        int parentAge = (Integer)m.invoke(c, null);
        System.out.println("parentAge - " + parentAge);
        
        
        c.getDefName();
        
    }

    public String getDefName() throws Exception
    {
        Method m2 = getMethod("a.test.Parent", "getName", String.class);
        //设置private方法可访问
        m2.setAccessible(true);
        String name = (String)m2.invoke(this, "liqiulin22");
        System.out.println("name - " + name);
        return name;
    }
    
    /**
     * 获取指定类的字段
     * 
     * <pre>
     * 
     * </pre>
     * @param className 完整类名称(包括包名称)
     * @param fieldName 字段名称
     * @return
     * @throws ClassNotFoundException
     * @throws SecurityException
     * @throws NoSuchFieldException
     */
    public static Field getField(String className, String fieldName) throws ClassNotFoundException, SecurityException,
            NoSuchFieldException
    {
        Class ownerClass = Class.forName(className);
        return ownerClass.getDeclaredField(fieldName);
    }

    /**
     * 获取指定类的方法
     * 
     * <pre>
     * 
     * </pre>
     * @param className
     * @param methodName
     * @param parameterTypes
     * @return
     * @throws ClassNotFoundException
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    public static Method getMethod(String className, String methodName, Class... parameterTypes)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException
    {
        Class ownerClass = Class.forName(className);
        return ownerClass.getDeclaredMethod(methodName, parameterTypes);
    }
}


package a.test;

public class Parent
{
    private String name;

    private int age;

    public Parent()
    {
        this.name = "liqiulin";
        this.age = 26;
    }

    private int getAge()
    {
        return this.age;
    }
    
    public String getName(String name)
    {
        return name;
    }
}
Global site tag (gtag.js) - Google Analytics