??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲人AV永久一区二区三区久久,亚洲大尺码专区影院,亚洲最大无码中文字幕http://www.tkk7.com/RR00/category/2691.html不要埋头苦干Q要学习Q学习,再学习。。。。? <br> powered by <font color='orange'>R.Zeus</font>zh-cnTue, 27 Feb 2007 10:37:16 GMTTue, 27 Feb 2007 10:37:16 GMT60Explore Java's static nested classes and inner classeshttp://www.tkk7.com/RR00/articles/78932.htmlR.ZeusR.ZeusFri, 03 Nov 2006 07:42:00 GMThttp://www.tkk7.com/RR00/articles/78932.htmlhttp://www.tkk7.com/RR00/comments/78932.htmlhttp://www.tkk7.com/RR00/articles/78932.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78932.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78932.html 

Explore Java's static nested classes and inner classes


by  David Petersheim  |  More from David Petersheim  |  Published: 8/12/05

Starting with JDK 1.1, Java provided the ability to create nested classes. A nested class is defined inside another class. There are two types of nested classes: static nested classes and inner classes.

A static nested class is declared inside another class with the static keyword or within a static context of that class. A static class has no access to instance-specific data.

An inner class is a nonstatic class declared inside another class. It has access to all of its enclosing class's instance data, including private fields and methods. Inner classes may access static data but may not declare static members unless those members are compile time constants.

Deciding which type of nested class to use depends on the data type your nested class needs to access. If you need access to instance data, you'll need an inner class. If you don't need access to instance data, a static nested class will suffice.

The most common use of inner classes is as event handlers for GUI-based applications. These classes are usually declared anonymously and as needed for each component that requires an event handler. The advantage of using an inner class for event handling is that you can avoid large If/else statements to decide which component is being handled. Each component gets its own event handler, so each event handler knows implicitly the component for which it's working.

The snippet below creates an anonymous inner class to handle the events created by an application's OK button.

Button btn = new Button("Ok");
btn.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            okClicked();
        }
    }
);

The advantage of a static nested class is that it doesn't need an instance of the containing class to work. This can help you reduce the number of objects your application creates at runtime.

The semantics for creating instances of nested classes can be confusing. Below is a simple class that defines a static nested class and an inner class. Pay special attention to the main method, where an instance of each instance class is created.

// creating an instance of the enclosing class
NestedClassTip nt = new NestedClassTip();


// creating an instance of the inner class requires
// a reference to an instance of the enclosing class
NestedClassTip.NestedOne nco = nt.new NestedOne();


// creating an instance of the static nested class
// does not require an instance of the enclosing class
NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();



public class NestedClassTip {
    private String name = "instance name";
    private static String staticName = "static name";

    public static void main(String args[]) {
        NestedClassTip nt = new NestedClassTip();

        NestedClassTip.NestedOne nco = nt.new NestedOne();

        NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();
    }

    class NestedOne {
        NestedOne() {
            System.out.println(name);
            System.out.println(staticName);
        }
    }

    static class NestedTwo {
        NestedTwo() {
            System.out.println(staticName);
        }
    }
}

Nested classes can be confusing, but once you understand their purpose and get used to the semantics, there isn't a lot to them. If you'd like to learn more about the details of nested classes, check out the Java Language Specification.

 



R.Zeus 2006-11-03 15:42 发表评论
]]>
String.intern()http://www.tkk7.com/RR00/articles/78242.htmlR.ZeusR.ZeusTue, 31 Oct 2006 04:05:00 GMThttp://www.tkk7.com/RR00/articles/78242.htmlhttp://www.tkk7.com/RR00/comments/78242.htmlhttp://www.tkk7.com/RR00/articles/78242.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78242.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78242.html

R.Zeus 2006-10-31 12:05 发表评论
]]>
Performance: Hibernate startup timehttp://www.tkk7.com/RR00/articles/78093.htmlR.ZeusR.ZeusMon, 30 Oct 2006 09:22:00 GMThttp://www.tkk7.com/RR00/articles/78093.htmlhttp://www.tkk7.com/RR00/comments/78093.htmlhttp://www.tkk7.com/RR00/articles/78093.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78093.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78093.htmlFrom: Performance: Hibernate startup time

The following tips hints how to make Hibernate startup faster.

Only add the mapping files you need!

If you are running some few JUnit tests for a 400++ classes project you probably don't hit every class in those tests and thus do not need to add all those hbm.xml's to the Configuration. Go look at Hibernate's test suite on how you could let your TestCase decide what classes should be defined in the mapping.

Use serialized XML documents when configuring Configuration

When building the configuration 40-60% of the time is used by the XML parsers and Dom4j to read up the XML document. Significant performance increases can be done by serializing the Document object's to disc once, and afterwards just add them to the configuration by deserializing them first.

In the current cvs we have an experimental Configuration.addCachableFile() method that can be used as inspiration for previous Hibernate versions.

public Configuration addCachableFile(String xmlFile) throws MappingException {        
        try {
            File file = new File(xmlFile);
            File lazyfile = new File(xmlFile + ".bin");
            org.dom4j.Document doc = null; 
            List errors = new ArrayList();
            if(file.exists() && lazyfile.exists() && file.lastModified()<lazyfile.lastModified()) {
                log.info("Mapping lazy file: " + lazyfile.getPath());
                ObjectInputStream oip = null;
                oip = new ObjectInputStream(new FileInputStream(lazyfile));
                doc = (org.dom4j.Document) oip.readObject();
                oip.close(); 
            } else {
                doc = xmlHelper.createSAXReader(xmlFile, errors, entityResolver).read( file );
                log.info("Writing lazy file to " + lazyfile);
                ObjectOutputStream oup = new ObjectOutputStream(new FileOutputStream(lazyfile));
                oup.writeObject(doc);
                oup.flush();
                oup.close();
            }
            
            if ( errors.size()!=0 ) throw new MappingException( "invalid mapping", (Throwable) errors.get(0) );
            add(doc);
            return this;
        }
        catch (Exception e) {
            log.error("Could not configure datastore from file: " + xmlFile, e);
            throw new MappingException(e);
        }
    }

Disable Hibernates usage of cglib reflection optimizer

Put the following line in hibernate.properties:

hibernate.cglib.use_reflection_optimizer=false

It will make Hibernate start faster since it does not try to build cglib-enhanced objects to access getter/setters.

Note: It will have in impact on overall runtime performance since Hibernate will be forced to use standard JDK reflection for access. So it is most useful during development. (You will also get better error messages in some situations when the optimizer is disabled ;)


  NEW COMMENT

Serializing the Configuration object
04 May 2004, 12:37
luish
Another approach would be to serialize the whole Configuration 
object. What do you think about this? I have submitted a patch to  
the Jira to make the Configuration Serializable (see bugs 492 and 
147).
 
addLazyFile() not there.
06 Jul 2004, 11:50
gstamp
I can't fine addLazyFile() in CVS.  Is it still supposed to be there?
 
Hibernate3 feature
31 Aug 2004, 11:13
gavin
Try the Hibernate3 module (or just the alpha release)
 
Information update?
30 Mar 2005, 07:54
gruberc
The information on this page does not seem to be correct any
more. With Hibernate 3.0rc1, there is no Configuration.addLazyFile()
any more, but addCacheableFile(). How should it be used?
 
lazy
06 Mar 2006, 05:09
steckemetz
If you have terrible problems with startup time and
do NOT need certain features like:

* proxy objects
* lazy loading

or if you are using the stateless session,
then you can disable lazyness on class level like:

<class name="myClass" table="myTable" lazy="false">

The default is true and forces byte code generation of
some proxy class which takes a lot of time.
Perhaps some hibernate guru can tell us, which other
features will be disabled by this.
 
I solve it.
02 Aug 2006, 00:12
cm4ever
The Hibernate Configuration module implement is very bad.
I write a module to realize the dynamic loading mapping files.
But other function I can't resolve...

Hibernate Dynamic Module
This project is only a module of Hibernate http://www.hibernate.org Read
mapping file until insert/update/delete/select the persistent class in
Hibernate.
http://sourceforge.net/projects/hbn-dyn-mod/


R.Zeus 2006-10-30 17:22 发表评论
]]>
an easy example for Serializable from webhttp://www.tkk7.com/RR00/articles/78083.htmlR.ZeusR.ZeusMon, 30 Oct 2006 08:40:00 GMThttp://www.tkk7.com/RR00/articles/78083.htmlhttp://www.tkk7.com/RR00/comments/78083.htmlhttp://www.tkk7.com/RR00/articles/78083.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78083.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78083.html         Configuration configuration=null;
        try {
            Configuration configurationSerializable = new Configuration();
            FileOutputStream fos = new FileOutputStream("serial");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(configurationSerializable);
            oos.flush();
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        try {
            FileInputStream fis = new FileInputStream("serial");
            ObjectInputStream ois = new ObjectInputStream(fis);
            configuration = (Configuration) ois.readObject();
            ois.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if(configuration!=null)
        {
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        callBack.doing(session);
        transaction.commit();
        }
    }
when will Configuration serialize and why it's some field seted transient?the example will be error because some field transient is null after serialize.



R.Zeus 2006-10-30 16:40 发表评论
]]>
HashMap and TreeMaphttp://www.tkk7.com/RR00/articles/78080.htmlR.ZeusR.ZeusMon, 30 Oct 2006 08:32:00 GMThttp://www.tkk7.com/RR00/articles/78080.htmlhttp://www.tkk7.com/RR00/comments/78080.htmlhttp://www.tkk7.com/RR00/articles/78080.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78080.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78080.htmlHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

public class TreeMap<K,V>
    extends AbstractMap<K,V>
    implements SortedMap<K,V>, Cloneable, java.io.Serializable

Obviously,TreeMap store elements sorted.But for the chinese word ,the default Comparator don't  do it rightly,so you must write your Comparator .this is an example from web:

importjava.text.CollationKey;

import java.text.Collator;

import java.util.Comparator;

 

/**

 *@author www.inspiresky.com

 *

 */

publicclass CollatorComparator implements Comparator {

Collator collator = Collator.getInstance();

 

publicint compare(Object element1, Object element2) {

    CollationKey key1 = collator.getCollationKey(element1.toString());

    CollationKey key2 = collator.getCollationKey(element2.toString());

    return key1.compareTo(key2);

}

}


  to use this CollatorComparator ,use TreeMap constructor
Mothod :

 public TreeMap(Comparator<? super K> c) {
        this.comparator = c;
    }




R.Zeus 2006-10-30 16:32 发表评论
]]>
Java access specifiers ---protectedhttp://www.tkk7.com/RR00/articles/78045.htmlR.ZeusR.ZeusMon, 30 Oct 2006 06:17:00 GMThttp://www.tkk7.com/RR00/articles/78045.htmlhttp://www.tkk7.com/RR00/comments/78045.htmlhttp://www.tkk7.com/RR00/articles/78045.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/78045.htmlhttp://www.tkk7.com/RR00/services/trackbacks/78045.htmlSometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general. That’s what protected does. protected also gives package access—that is, other classes in the same package may access protected elements.



R.Zeus 2006-10-30 14:17 发表评论
]]>
析Java中Datecȝ应用http://www.tkk7.com/RR00/articles/74033.htmlR.ZeusR.ZeusMon, 09 Oct 2006 04:18:00 GMThttp://www.tkk7.com/RR00/articles/74033.htmlhttp://www.tkk7.com/RR00/comments/74033.htmlhttp://www.tkk7.com/RR00/articles/74033.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/74033.htmlhttp://www.tkk7.com/RR00/services/trackbacks/74033.html 创徏一个日期对?
让我们看一个用系l的当前日期和时间创Z个日期对象ƈq回一个长整数的简单例? q个旉通常被称为Java 虚拟?JVM)L环境的系l时?
import java.util.Date; 

public class DateExample1 {
    public static void main(StringQ] args) { //自己替换Q]
    // Get the system date/time
    Date date = new Date();

    System.out.println(date.getTime());
    }
}


在星期六, 2001q??9? 下午大约?:50的样? 上面的例子在pȝ输出讑֤上显C的l果?1001803809710. 在这个例子中,值得注意的是我们使用了Date 构造函数创Z个日期对? q个构造函数没有接受Q何参? 而这个构造函数在内部使用了System.currentTimeMillis() Ҏ来从pȝ获取日期.

那么, 现在我们已经知道了如何获取从1970q??日开始经历的毫秒C. 我们如何才能以一U用h白的格式来显C个日期呢? 在这里类java.text.SimpleDateFormat 和它的抽象基c?java.text.DateFormat 派得上用场?


日期数据的定制格?
假如我们希望定制日期数据的格? 比方星期?9?29?2001q? 下面的例子展CZ如何完成q个工作:

import java.text.SimpleDateFormat; 
import java.util.Date;

public class DateExample2 {

    public static void main(StringQ] args) { //自己替换Q]

        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("EEEE-MMMM-dd-yyyy");

        Date date = new Date();

        System.out.println(bartDateFormat.format(date));
    }
}


只要通过向SimpleDateFormat 的构造函C递格式字W串"EEE-MMMM-dd-yyyy", 我们p够指明自己想要的格式. 你应该可以看? 格式字符串中的ASCII 字符告诉格式化函C面显C日期数据的哪一个部? EEEE是星? MMMM是月, dd是日, yyyy是年. 字符的个数决定了日期是如何格式化?传?EE-MM-dd-yy"会显C?Sat-09-29-01. 请察看Sun 公司的Web 站点获取日期格式化选项的完整的指示.

文本数据解析成日期对象
假设我们有一个文本字W串包含了一个格式化了的日期对象, 而我们希望解析这个字W串q从文本日期数据创徏一个日期对? 我们再ơ以格式化字W串"MM-dd-yyyy" 调用SimpleDateFormatc? 但是q一? 我们使用格式化解析而不是生成一个文本日期数? 我们的例? 昄在下? 解析文本字W串"9-29-2001"q创Z个gؓ001736000000 的日期对?

例子E序:

import java.text.SimpleDateFormat; 
import java.util.Date;

public class DateExample3 {

    public static void main(StringQ]args) { //自己替换Q]
        // Create a date formatter that can parse dates of
        // the form MM-dd-yyyy.
        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("MM-dd-yyyy");

        // Create a string containing a text date to be parsed.
        String dateStringToParse = "9-29-2001";

        try {
            // Parse the text version of the date.
            // We have to perform the parse method in a
            // try-catch construct in case dateStringToParse
            // does not contain a date in the format we are expecting.
            Date date = bartDateFormat.parse(dateStringToParse);

            // Now send the parsed date as a long value
            // to the system output.
            System.out.println(date.getTime());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}


使用标准的日期格式化q程
既然我们已经可以生成和解析定制的日期格式? 让我们来看一看如何用内建的格式化过E? Ҏ DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过E? 在下面的例子? 我们获取了四个内建的日期格式化过E? 它们包括一个短? 中等? 长的, 和完整的日期格式.

import java.text.DateFormat; 
import java.util.Date;

public class DateExample4 {

    public static void main(StringQ] args) { //自己替换Q]
        Date date = new Date();

        DateFormat shortDateFormat =
        DateFormat.getDateTimeInstance(
        DateFormat.SHORT,
        DateFormat.SHORT);

        DateFormat mediumDateFormat =
        DateFormat.getDateTimeInstance(
        DateFormat.MEDIUM,
        DateFormat.MEDIUM);

        DateFormat longDateFormat =
        DateFormat.getDateTimeInstance(
        DateFormat.LONG,
        DateFormat.LONG);

        DateFormat fullDateFormat =
        DateFormat.getDateTimeInstance(
        DateFormat.FULL,
        DateFormat.FULL);

        System.out.println(shortDateFormat.format(date));
        System.out.println(mediumDateFormat.format(date));
        System.out.println(longDateFormat.format(date));
        System.out.println(fullDateFormat.format(date));
    }
}




注意我们在对 getDateTimeInstance的每ơ调用中都传递了两个? W一个参数是日期风格, 而第二个参数是时间风? 它们都是基本数据cdint(整型). 考虑到可L? 我们使用了DateFormat cL供的帔R: SHORT, MEDIUM, LONG, ?FULL. 要知道获取时间和日期格式化过E的更多的方法和选项, LSun 公司Web 站点上的解释.

q行我们的例子程序的时? 它将向标准输备输Z面的内容:
9/29/01 8:44 PM
Sep 29, 2001 8:44:45 PM
September 29, 2001 8:44:45 PM EDT
Saturday, September 29, 2001 8:44:45 PM EDT


R.Zeus 2006-10-09 12:18 发表评论
]]>
DATE and TIMESTAMP http://www.tkk7.com/RR00/articles/74029.htmlR.ZeusR.ZeusMon, 09 Oct 2006 03:54:00 GMThttp://www.tkk7.com/RR00/articles/74029.htmlhttp://www.tkk7.com/RR00/comments/74029.htmlhttp://www.tkk7.com/RR00/articles/74029.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/74029.htmlhttp://www.tkk7.com/RR00/services/trackbacks/74029.html DATE

The DATE data type stores date and time information. For each DATE value, Oracle stores the following information: year, month, day, hour, minute, and second.

The date value can be specified as an ANSI date literal, an Oracle date literal or can be converted from a character or numeric value with the TO_DATE function. The ANSI date literal contains no time portion, and must be specified in the format 'YYYY-MM-DD'. The default date format for an Oracle date literal can be changed by the initialization parameter NLS_DATE_FORMAT.

Date data can range from January 1, 4712 BC to December 31, 9999.

If a date value is specified without a time component, then the default time is 12:00:00 AM. If a date value is specified without a date, then the default date is the first day of the current month.

---------------------------------------------------------------------------------------------------------------------------------------------------

TIMESTAMP
[(fractional_seconds_precision)]

The TIMESTAMP data type is an extension of the DATE data type. For each TIMESTAMP value, Oracle stores the following information: year, month, day, hour, minute, second and fraction of second.

fractional_seconds_precision optionally specifies the number of digits in the fractional part of second and can be a number in the range 0 to 9. The default is 6.

The TIMESTAMP data type is available in Oracle 9i Release 1 (9.0.1) or later.



R.Zeus 2006-10-09 11:54 发表评论
]]>
Java中在E序中设|代理服务器http://www.tkk7.com/RR00/articles/73823.htmlR.ZeusR.ZeusSun, 08 Oct 2006 07:04:00 GMThttp://www.tkk7.com/RR00/articles/73823.htmlhttp://www.tkk7.com/RR00/comments/73823.htmlhttp://www.tkk7.com/RR00/articles/73823.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/73823.htmlhttp://www.tkk7.com/RR00/services/trackbacks/73823.html ?Java 中代理服务器的基本设|是通过讄pȝ属性来完成的。而代理服务器的验证则是通过讄 Http h头来完成的?
  下面的是一个简单的例子供大家参考:
  
  // Ҏ地址 url 打开 Http q接
  HttpURLConnection con = (HttpURLConnection)( new URL( url ) ).openConnection();
  if (proxy.hasProxy()) {
  // 注意Q?如果 proxySet ?false Ӟ依然讄?proxyHost ?proxyPortQ代理设|仍会v作用?
  // 如果 proxyPort 讄有问题,代理讄不会起作用?
  System.getProperties().put( "proxySet", "true" );
  System.getProperties().put( "proxyHost", proxy.getProxyHost() );
  System.getProperties().put( "proxyPort", String.valueOf( proxy.getProxyPort() ) );
  
  // 如果需要代理服务器验证Q在 Http h头中加入 Proxy-Authorization _
  // 格式为: "Basic " + ("代理服务器用户名Q密??BASE64 ~码)
  if (proxy.needAuth()) {
  con.setRequestProperty( "Proxy-Authorization", "Basic " + Encoder.base64Encode( proxy.getProxyUser() + ":" + proxy.getProxyPass() ) );
  }
  }


R.Zeus 2006-10-08 15:04 发表评论
]]>
mappings.setDefaultLazy(dlNode == null || dlNode.getValue().equals("true"));http://www.tkk7.com/RR00/articles/73081.htmlR.ZeusR.ZeusSat, 30 Sep 2006 10:07:00 GMThttp://www.tkk7.com/RR00/articles/73081.htmlhttp://www.tkk7.com/RR00/comments/73081.htmlhttp://www.tkk7.com/RR00/articles/73081.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/73081.htmlhttp://www.tkk7.com/RR00/services/trackbacks/73081.html1st:   mappings.setDefaultLazy(dlNode == null || dlNode.getValue().equals("true"));
2ed: mappings.setAutoImport((aiNode == null) ? true : "true".equals(aiNode.getValue()));

the first is seems a bit more effective than the second but less readable ,hence we choose the second!
bad programmer write code readed by machine and by contraries good programmer write code readed by human!

R.Zeus 2006-09-30 18:07 发表评论
]]>
about callback and decorator patternhttp://www.tkk7.com/RR00/articles/72855.htmlR.ZeusR.ZeusFri, 29 Sep 2006 08:17:00 GMThttp://www.tkk7.com/RR00/articles/72855.htmlhttp://www.tkk7.com/RR00/comments/72855.htmlhttp://www.tkk7.com/RR00/articles/72855.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/72855.htmlhttp://www.tkk7.com/RR00/services/trackbacks/72855.htmldecorator is using when one object has may ways to do pre-init and finally work.

R.Zeus 2006-09-29 16:17 发表评论
]]>
refill of if-else or switchhttp://www.tkk7.com/RR00/articles/72633.htmlR.ZeusR.ZeusThu, 28 Sep 2006 09:30:00 GMThttp://www.tkk7.com/RR00/articles/72633.htmlhttp://www.tkk7.com/RR00/comments/72633.htmlhttp://www.tkk7.com/RR00/articles/72633.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/72633.htmlhttp://www.tkk7.com/RR00/services/trackbacks/72633.html

R.Zeus 2006-09-28 17:30 发表评论
]]>
another method equals abstract factory method pattern http://www.tkk7.com/RR00/articles/72630.htmlR.ZeusR.ZeusThu, 28 Sep 2006 09:20:00 GMThttp://www.tkk7.com/RR00/articles/72630.htmlhttp://www.tkk7.com/RR00/comments/72630.htmlhttp://www.tkk7.com/RR00/articles/72630.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/72630.htmlhttp://www.tkk7.com/RR00/services/trackbacks/72630.htmlnested public static interface .
I found this when i read the org.hibernate.hql.ast.util.NodeTraverser.by the way,the command pattern is somewhat like those way.

R.Zeus 2006-09-28 17:20 发表评论
]]>
use abstract class or interface as new instancehttp://www.tkk7.com/RR00/articles/72529.htmlR.ZeusR.ZeusThu, 28 Sep 2006 04:29:00 GMThttp://www.tkk7.com/RR00/articles/72529.htmlhttp://www.tkk7.com/RR00/comments/72529.htmlhttp://www.tkk7.com/RR00/articles/72529.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/72529.htmlhttp://www.tkk7.com/RR00/services/trackbacks/72529.html
interface=new interface(or abtract class)
{
//in this rigion ,implements all the method needed(e.g. abstract method,method in interface)
.......
}

this use in the case that one interface has many implements which  just be used once ,so this way is a lazy but good way.

this way called "Anonymous class";

R.Zeus 2006-09-28 12:29 发表评论
]]>
Mappings for many propertieshttp://www.tkk7.com/RR00/articles/71802.htmlR.ZeusR.ZeusMon, 25 Sep 2006 09:39:00 GMThttp://www.tkk7.com/RR00/articles/71802.htmlhttp://www.tkk7.com/RR00/comments/71802.htmlhttp://www.tkk7.com/RR00/articles/71802.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/71802.htmlhttp://www.tkk7.com/RR00/services/trackbacks/71802.htmlSessionFactory.How Configuration get the properties analyzed in HbmBinder .The bridge is the Mappings .Configuration
had many private property fields,e.g.  classes,collections,tables,imports and so on.when call HbmBinder to work,Configuration first createMappings with its property fields then pass the Mappings  to the HbmBinder .In HbmBinder ,what Mappings   had done is also the  Configuration 's field done.

e.g. Configuration  has a field "imports" ,it gone with the Mappings as a parameter in Mappings constructor to HbmBinder .
in HbmBinder class ,HbmBinder use Mappings .addImport() to operate it and at the same time point to the Configuration's "imports".This is correlative to java's clone mechanism.

this gives us a exquisite way to deal with more properties among several class.





R.Zeus 2006-09-25 17:39 发表评论
]]>
使用for 代替 whilehttp://www.tkk7.com/RR00/articles/70328.htmlR.ZeusR.ZeusMon, 18 Sep 2006 08:08:00 GMThttp://www.tkk7.com/RR00/articles/70328.htmlhttp://www.tkk7.com/RR00/comments/70328.htmlhttp://www.tkk7.com/RR00/articles/70328.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/70328.htmlhttp://www.tkk7.com/RR00/services/trackbacks/70328.htmlin my opinion, when useing  "for" clause ,the local variable's range are restricted in the "for" clause not in all method range.this is better than in "while" clause.

R.Zeus 2006-09-18 16:08 发表评论
]]>
q代中的wile和if要注?/title><link>http://www.tkk7.com/RR00/articles/70306.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 18 Sep 2006 07:22:00 GMT</pubDate><guid>http://www.tkk7.com/RR00/articles/70306.html</guid><wfw:comment>http://www.tkk7.com/RR00/comments/70306.html</wfw:comment><comments>http://www.tkk7.com/RR00/articles/70306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/RR00/comments/commentRss/70306.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/RR00/services/trackbacks/70306.html</trackback:ping><description><![CDATA[看了一个下午,l于扑ֈbugQP代要注意是用whileq是if语句?img src ="http://www.tkk7.com/RR00/aggbug/70306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/RR00/" target="_blank">R.Zeus</a> 2006-09-18 15:22 <a href="http://www.tkk7.com/RR00/articles/70306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using the java.lang.reflect.Modifier Classhttp://www.tkk7.com/RR00/articles/69394.htmlR.ZeusR.ZeusWed, 13 Sep 2006 08:10:00 GMThttp://www.tkk7.com/RR00/articles/69394.htmlhttp://www.tkk7.com/RR00/comments/69394.htmlhttp://www.tkk7.com/RR00/articles/69394.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/69394.htmlhttp://www.tkk7.com/RR00/services/trackbacks/69394.htmlUsing the java.lang.reflect.Modifier Class
You know that you can extract constructors, methods, and variables from a Java class file. Generally, when you use

	Field fields[] = c.getDeclaredFields( );
where c is initialized using

	Class c = Class.forName(className);
and print the fields array, you get all the elements declared in the class.

However, suppose you want to restrict the output to all fields other than those declared as private. In this case, you would use the following code, where the Modifier class is a part of the java.lang.reflect package:


if(!Modifier.isPrivate(fields[i].getModifiers( )){
	System.out.println(fields[i]+"\n");
}
Using Modifier.isPrivate() ensures that the private variables are not printed.

The same can be used for methods as well.

author: MS Sridhar


R.Zeus 2006-09-13 16:10 发表评论
]]>
ListIterator in hibernatehttp://www.tkk7.com/RR00/articles/69392.htmlR.ZeusR.ZeusWed, 13 Sep 2006 08:01:00 GMThttp://www.tkk7.com/RR00/articles/69392.htmlhttp://www.tkk7.com/RR00/comments/69392.htmlhttp://www.tkk7.com/RR00/articles/69392.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/69392.htmlhttp://www.tkk7.com/RR00/services/trackbacks/69392.html  ListIterator liter = fromClause.getFromElements().listIterator( fromClause.getFromElements().size() );
  while ( liter.hasPrevious() ) {
            log.debug("add previous");
            orderedFromElements.add( liter.previous() );
  }

R.Zeus 2006-09-13 16:01 发表评论
]]>
Java中ThreadLocal的设计与使用http://www.tkk7.com/RR00/articles/64067.htmlR.ZeusR.ZeusThu, 17 Aug 2006 02:43:00 GMThttp://www.tkk7.com/RR00/articles/64067.htmlhttp://www.tkk7.com/RR00/comments/64067.htmlhttp://www.tkk7.com/RR00/articles/64067.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/64067.htmlhttp://www.tkk7.com/RR00/services/trackbacks/64067.html
  一、ThreadLocal是什?/strong>

  ThreadLocalq是一个线E的本地实现版本Q它q不是一个ThreadQ而是thread local variableQ线E局部变量)。也许把它命名ؓThreadLocalVar更加合适。线E局部变量(ThreadLocalQ其实的功用非常单,是为每一个用该变量的线E都提供一个变量值的副本Q是每一个线E都可以独立地改变自q副本Q而不会和其它U程的副本冲H。从U程的角度看Q就好像每一个线E都完全拥有该变量。线E局部变量ƈ不是Java的新发明Q在其它的一些语a~译器实玎ͼ如IBM XL FORTRANQ中Q它在语a的层ơ提供了直接的支持。因为Java中没有提供在语言层次的直接支持,而是提供了一个ThreadLocal的类来提供支持,所以,在Java中编写线E局部变量的代码相对比较W拙Q这也许是线E局部变量没有在Java中得到很好的普及的一个原因吧?/p>



  二、ThreadLocal的设?/strong>

  首先看看ThreadLocal的接口:

Object get() ;

// q回当前U程的线E局部变量副?protected Object initialValue(); // q回该线E局部变量的当前U程的初始?/p>

void set(Object value);

// 讄当前U程的线E局部变量副本的?/p>


  ThreadLocal?个方法,其中值得注意的是initialValue()Q该Ҏ是一个protected的方法,昄是ؓ了子c重写而特意实现的。该Ҏq回当前U程在该U程局部变量的初始|q个Ҏ是一个gq调用方法,在一个线E第1ơ调用get()或者set(Object)时才执行Qƈ且仅执行1ơ。ThreadLocal中的实实现直接q回一个nullQ?/p>

protected Object initialValue() { return null; }

  ThreadLocal是如何做Cؓ每一个线E维护变量的副本的呢Q其实实现的思\很简单,在ThreadLocalcM有一个MapQ用于存储每一个线E的变量的副本。比如下面的CZ实现Q?/p>

public class ThreadLocal

{

private Map values = Collections.synchronizedMap(new HashMap());

public Object get()

{

Thread curThread = Thread.currentThread();

Object o = values.get(curThread);

if (o == null && !values.containsKey(curThread))

{

o = initialValue();

values.put(curThread, o);

}

return o;

}


public void set(Object newValue)

{

values.put(Thread.currentThread(), newValue);

}


public Object initialValue()

{

return null;

}

}


当然Q这q不是一个工业强度的实现Q但JDK中的ThreadLocal的实现M思\也类g此?/p>


  三、ThreadLocal的?/strong>

  如果希望U程局部变量初始化其它|那么需要自己实现ThreadLocal的子cdƈ重写该方法,通常使用一个内部匿名类对ThreadLocalq行子类化,比如下面的例子,SerialNumcMؓ每一个类分配一个序P


public class SerialNum

{

// The next serial number to be assigned


private static int nextSerialNum = 0;

private static ThreadLocal serialNum = new ThreadLocal()

{

protected synchronized Object initialValue()

{

return new Integer(nextSerialNum++);

}

};


public static int get()

{

return ((Integer) (serialNum.get())).intValue();

}

}


  SerialNumcȝ使用非常地单,因ؓget()Ҏ是static的,所以在需要获取当前线E的序号Ӟ单地调用Q?/p>

int serial = SerialNum.get();

卛_?/p>


  在线E是zd的ƈ且ThreadLocal对象是可讉K的时Q该U程持有一个到该线E局部变量副本的隐含引用Q当该线E运行结束后Q该U程拥有的所以线E局部变量的副本都将失效Qƈ{待垃圾攉器收集?br />
四、ThreadLocal与其它同步机制的比较

  ThreadLocal和其它同步机制相比有什么优势呢QThreadLocal和其它所有的同步机制都是Z解决多线E中的对同一变量的访问冲H,在普通的同步机制中,是通过对象加锁来实现多个线E对同一变量的安全访问的。这时该变量是多个线E共享的Q用这U同步机刉要很l致地分析在什么时候对变量q行dQ什么时候需要锁定某个对象,什么时候释放该对象的锁{等很多。所有这些都是因为多个线E共享了资源造成的。ThreadLocal׃另一个角度来解决多线E的q发讉KQThreadLocal会ؓ每一个线E维护一个和该线E绑定的变量的副本,从而隔M多个U程的数据,每一个线E都拥有自己的变量副本,从而也没有必要对该变量进行同步了。ThreadLocal提供了线E安全的׃n对象Q在~写多线E代码时Q可以把不安全的整个变量装qThreadLocalQ或者把该对象的特定于线E的状态封装进ThreadLocal?/p>


  ׃ThreadLocal中可以持有Q何类型的对象Q所以用ThreadLocal get当前U程的值是需要进行强制类型{换。但随着新的Java版本Q?.5Q将模版的引入,新的支持模版参数的ThreadLocalcd从中受益。也可以减少强制cd转换Qƈ一些错误检查提前到了编译期Q将一定程度地化ThreadLocal的用?/p>


  五、ȝ

  当然ThreadLocalq不能替代同步机Ӟ两者面向的问题领域不同。同步机制是Z同步多个U程对相同资源的q发讉KQ是Z多个U程之间q行通信的有效方式;而ThreadLocal是隔d个线E的数据׃nQ从Ҏ上就不在多个U程之间׃n资源Q变量)Q这样当然不需要对多个U程q行同步了。所以,如果你需要进行多个线E之间进行通信Q则使用同步机制Q如果需要隔d个线E之间的׃n冲突Q可以用ThreadLocalQ这极大地化你的程序,使程序更加易诅R简z?/p>

R.Zeus 2006-08-17 10:43 发表评论
]]>
java MD5法q回数字型字?/title><link>http://www.tkk7.com/RR00/articles/18191.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 04 Nov 2005 12:20:00 GMT</pubDate><guid>http://www.tkk7.com/RR00/articles/18191.html</guid><wfw:comment>http://www.tkk7.com/RR00/comments/18191.html</wfw:comment><comments>http://www.tkk7.com/RR00/articles/18191.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/RR00/comments/commentRss/18191.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/RR00/services/trackbacks/18191.html</trackback:ping><description><![CDATA[&lt;P&gt;常有人问及MD5法Z有些E序片断q回完全数字型结果而有些返回数字与字母的؜合字丌Ӏ?amp;lt;/P&gt; &lt;P&gt;其实两种q回l果只是因ؓ加密l果的不同显CŞ式,Blog中已l有.Net的实玎ͼ在此附加JAVA实现Q供参考?amp;lt;/P&gt; &lt;P&gt;JAVA的标准类库理Z功能也很强大Q但׃虚拟?q行时的实现太多Q加之版本差异,有些代码在不同环境下q行会出现奇怪的异常l果Q尤其以涉及字符集的操作为甚?amp;lt;/P&gt; &lt;P&gt;package com.bee.framework.common;&lt;/P&gt; &lt;P&gt;import java.security.MessageDigest;&lt;/P&gt; &lt;P&gt;public class MD5Encrypt {&lt;BR&gt;&amp;nbsp; public MD5Encrypt() {&lt;BR&gt;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp; private final static String[] hexDigits = {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;0&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;8&quot;, &quot;9&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;};&lt;/P&gt; &lt;P&gt;&amp;nbsp; /**&lt;BR&gt;&amp;nbsp;&amp;nbsp; * 转换字节数组?6q制字串&lt;BR&gt;&amp;nbsp;&amp;nbsp; * @param b 字节数组&lt;BR&gt;&amp;nbsp;&amp;nbsp; * @return 16q制字串&lt;BR&gt;&amp;nbsp;&amp;nbsp; */&lt;BR&gt;&amp;nbsp; public static String byteArrayToString(byte[] b) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; StringBuffer resultSb = new StringBuffer();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; b.length; i++) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //resultSb.append(byteToHexString(b[i]));//若用本函数转换则可得到加密l果?6q制表示Q即数字字母混合的Ş?amp;lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; resultSb.append(byteToNumString(b[i]));//使用本函数则q回加密l果?0q制数字字串Q即全数字Ş?amp;lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return resultSb.toString();&lt;BR&gt;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp; private static String byteToNumString(byte b) {&lt;/P&gt; &lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int _b = b;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (_b &amp;lt; 0) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _b = 256 + _b;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return String.valueOf(_b);&lt;BR&gt;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp; private static String byteToHexString(byte b) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int n = b;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (n &amp;lt; 0) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n = 256 + n;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int d1 = n / 16;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int d2 = n % 16;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return hexDigits[d1] + hexDigits[d2];&lt;BR&gt;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp; public static String MD5Encode(String origin) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String resultString = null;&lt;/P&gt; &lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; resultString = new String(origin);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; resultString =&lt;BR&gt;byteArrayToString(md.digest(resultString.getBytes()));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex) {&lt;/P&gt; &lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return resultString;&lt;BR&gt;&amp;nbsp; }&lt;/P&gt; &lt;P&gt;&amp;nbsp; public static void main(String[] args) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MD5Encrypt md5encrypt = new MD5Encrypt();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(MD5Encode(&quot;10000000&quot;));&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;&lt;/P&gt;<img src ="http://www.tkk7.com/RR00/aggbug/18191.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/RR00/" target="_blank">R.Zeus</a> 2005-11-04 20:20 <a href="http://www.tkk7.com/RR00/articles/18191.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中涉及byte、short和charcd的运操?/title><link>http://www.tkk7.com/RR00/articles/18186.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 04 Nov 2005 11:56:00 GMT</pubDate><guid>http://www.tkk7.com/RR00/articles/18186.html</guid><wfw:comment>http://www.tkk7.com/RR00/comments/18186.html</wfw:comment><comments>http://www.tkk7.com/RR00/articles/18186.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/RR00/comments/commentRss/18186.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/RR00/services/trackbacks/18186.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=0 cellPadding=0 width="98%" align=center border=0> <TBODY> <TR> <TD>Java中涉及byte、short和charcd的运操作首先会把这些D{换ؓintcdQ然后对intcdD行运,最后得到intcd的结果。因此,如果把两个bytecd值相加,最后会得到一个intcd的结果。如果需要得到bytecdl果Q必dq个intcd的结果显式{换ؓbytecd。例如,下面的代码会D~译p|Q? <P align=left><FONT style="BACKGROUND-COLOR: #f5f5dc">class BadArithmetic {<BR>  <BR>    static byte addOneAndOne() {<BR>        byte a = 1;<BR>        byte b = 1;<BR>        byte c = (a + b);<BR>        return c;<BR>    }<BR>}</FONT></P> <P align=left></P> <P align=left>当遇CqC码时Qjavac会给出如下提C:</P> <P align=left><FONT color=#ff0000>type.java:6: possible loss of precision<BR>found   : int<BR>required: byte<BR>                        byte c = (a + b);<BR>                                    ^<BR>1 error<BR></FONT></P> <P align=left>Z对这U情况进行补救,必须把a + b所获得的intcdl果昑ּ转换为bytecd。代码如下:</P> <P align=left><FONT style="BACKGROUND-COLOR: #f5f5dc">class GoodArithmetic {<BR>  <BR>    static byte addOneAndOne() {<BR>        byte a = 1;<BR>        byte b = 1;<BR>        byte c = (byte)(a + b);<BR>        return c;<BR>    }<BR>}</FONT></P> <P align=left>该操作能够通过javac的编译,q生GoodArithmetic.class文g?BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">char ccc=98;<BR>System.err.println( ccc);//out put is <FONT color=#ff0000>b</FONT></FONT><BR></P></TD></TR></TBODY></TABLE><img src ="http://www.tkk7.com/RR00/aggbug/18186.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/RR00/" target="_blank">R.Zeus</a> 2005-11-04 19:56 <a href="http://www.tkk7.com/RR00/articles/18186.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>File.createNewFile()http://www.tkk7.com/RR00/articles/18021.htmlR.ZeusR.ZeusThu, 03 Nov 2005 14:01:00 GMThttp://www.tkk7.com/RR00/articles/18021.htmlhttp://www.tkk7.com/RR00/comments/18021.htmlhttp://www.tkk7.com/RR00/articles/18021.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/18021.htmlhttp://www.tkk7.com/RR00/services/trackbacks/18021.htmlFile lockFile = new File(indexPath);
P.p("a"+lockFile.createNewFile()+"");
lockFile.createNewFile()
true if the named file does not exist and was successfully created; false if the named file already exists


lucene里利用这个进行锁定一些什么东西!

R.Zeus 2005-11-03 22:01 发表评论
]]>
Generics in J2SE 5.0Q翻译)http://www.tkk7.com/RR00/articles/10378.htmlR.ZeusR.ZeusWed, 17 Aug 2005 13:56:00 GMThttp://www.tkk7.com/RR00/articles/10378.htmlhttp://www.tkk7.com/RR00/comments/10378.htmlhttp://www.tkk7.com/RR00/articles/10378.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/10378.htmlhttp://www.tkk7.com/RR00/services/trackbacks/10378.html原文Q?A >http://www.onjava.com/pub/a/onjava/2005/07/06/generics.html
作者:Budi Kurniawan
译Q?A href="mailto:di_feng_ro@hotmail.com">di_feng_ro@hotmail.com

     泛型是J2SE 5.0最重要的特性。他们让你写一个type(cL接口Q和创徏一个实例通过传递一个或多个引用cd。这个实例受限于只能作用于这些类型。比如,在java 5Qjava.util.List 已经被泛化。当建立一个list对象Ӟ你通过传递一个javacd建立一个List实例Q此list实例只能作用于所传递的cd。这意味着如果你传递一个String ,此List实例只能拥有String对象Q如果你传递一个IntegerQ此实例只能存贮Integer对象。除了创建参数化的类型,你还能创建参数化的函数?BR>     泛型的第一个好处是~译时的严格cd查。这是Collections framework最重要的特炏V此?泛型消除了绝大多数的cd转换。在JDK 5.0之前Q当你用Collections frameworkӞ你不得不q行cd转换?BR>     本文教你如何操作泛型类型。它的第一部分是“没有泛型的日子”,先让我们回忆老版本JDK的不ѝ然后,举一些泛型的例子。在讨论完语法以及有界泛型的使用之后Q文章最后一章将解释如何写泛型?/P>

 


  没有泛型的日?/STRONG>

     所有的javac都源自java.lang.Object,q意味着所有的JAVA对象能{换成Object。因此,在之前的JDK的版本中Q很多Collections framework的函数接受一个Object参数。所以,collections是一个能持有M对象的多用途工P但带来了不良的后果?BR>     举个单的例子Q在JDK 5.0的之前版本中Q类List的函数add接受一个Object参数Q?BR>
 public boolean add(java.lang.Object element)

 所以你能传递Q何类型给add。这是故意这么设计的。否则,它只能传递某U特定的对象Q这样就会出现各UListcdQ如QStringList, EmployeeList, AddressList{?BR>     add通过Object传递能带来好处,现在我们考虑get函数(q回List中的一个元?.如下是JDK 5之前版本的定义:
 
public java.lang.Object get(int index) throws IndexOutOfBoundsException

 
getq回一个Object.不幸的事情从此开始了.假如你储存了两个String对象在一个List?

List stringList1 = new ArrayList();
stringList1.add("Java 5");
stringList1.add("with generics");

当你想从stringList1取得一个元素时,你得C一个Object.Z操作原来的类型元?你不得不把它转换String?BR>
String s1 = (String) stringList1.get(0);

但是,假如你曾l把一个non-String对象加入stringList1?上面的代码会抛出一个ClassCastException.
   有了泛型,你能创徏一个单一用途的List实例.比如,你能创徏一个只接受String对象的List实例,另外一个实例只能接受Employee对象.q同样适用于Collections framework中的其他cd.

 

泛型入门


   像一个函数能接受参数一?一个泛型类也能接受参数.q就是一个泛型类l常被称Z个parameterized type的原?但是不像函数?)传递参?泛型cL?lt;>传递参数的.声明一个泛型类和声明一个普通类没有什么区?只不q你把泛型的变量攑֜<>?
   比如,在JDK 5?你可以这样声明一个java.util.List :  List<E> myList;E UCؓtype variable.意味着一个变量将被一个类型替?替代type variable的值将被当作参数或q回cd.对于List接口来说,当一个实例被创徏以后,E 被当作一个add或别的函数的参数.E 也会使get或别的参数的q回?下面是add和get的定?

boolean add<E o>
E get(int index)

NOTE:一个泛型在声明或例C时允许你传递特定的type variable: E.除此之外,如果E是个c,你可以传递子c;如果E是个接口Q你可以传递实现接口的c;

-----------------------------译者添?-------------------
 List<Number> numberList= new ArrayList<Number>();
   numberList.add(2.0);
   numberList.add(2);
-----------------------------译者添?-------------------

如果你传递一个Stringl一个ListQ比如:

List<String> myList;

那么mylist的add函数接受一个String作ؓ他的参数Q而get函数返回一个String.因ؓq回了一个特定的cdQ所以不用类型{化了?/P>

NOTEQ根据惯例,我们使用一个唯一的大写字目表CZ个type variable。ؓ了创Z个泛型类Q你需在声明时传递同L参数列表。比如,你要惛_Z个ArrayList来操作String Q你必须把String攑֜<>
中。如Q?BR>
List<String> myList = new ArrayList<String>();

再比如,java.util.Map 是这么定义的Q?BR>
public interface Map<K,V>

K用来声明map密钥(KEY)的类型而V用来表示?VALUE)的类型。put和values是这么定义的Q?BR>
V put(K key, V value)
Collection<V> values()

NOTE:一个泛型类不准直接的或间接的是java.lang.Throwable的子cR因为异常是在run time抛出?所以它不可能预a什么类型的异常在compile time抛出.

列表1的例子将比较List在JDK 1.4 和JDK1.5的不?BR>

package com.brainysoftware.jdk5.app16;

import java.util.List;

import java.util.ArrayList;

 

public class GenericListTest {

  public static void main(String[] args) {

    // in JDK 1.4

    List stringList1 = new ArrayList();

    stringList1.add("Java 1.0 - 5.0");

    stringList1.add("without generics");

    // cast to java.lang.String

    String s1 = (String) stringList1.get(0);

    System.out.println(s1.toUpperCase());

 

    // now with generics in JDK 5

    List<String> stringList2 = new ArrayList<String>();

    stringList2.add("Java 5.0");

    stringList2.add("with generics");

    // no need for type casting

    String s2 = stringList2.get(0);

    System.out.println(s2.toUpperCase());

  }

}

在列?中,stringList2是个泛型cR声明List<String>告诉~译器List的实例能接受一个String对象。当Ӟ在另外的情况中,你能新徏能接受各U对象的List实例。注意,当从List实例中返回成员元素时Q不需要对象{化,因ؓ他返回的了你惌的类型,也就是String.

NOTE:泛型的类型检?type checking)是在compile time完成?

      最让h感兴的事情是,一个泛型类型是个类型ƈ且能被当作一个type variable。比如,你想你的List储存lists of Strings,你能通过把List<String>作ؓ他的type variable来声明List。比如:

List<List<String>> myListOfListsOfStrings;

要从myList中的W一个List重新取得StringQ你可以q么用:

String s = myListOfListsOfStrings.get(0).get(0);

下一个列表中的ListOfListsTestcȝ范了一个ListQ命名ؓlistOfListsQ接受一个String List作ؓ参数?BR>package com.brainysoftware.jdk5.app16;

import java.util.ArrayList;

import java.util.List;

public class ListOfListsTest {

  public static void main(String[] args) {

    List<String> listOfStrings = new ArrayList<String>();

    listOfStrings.add("Hello again");

    List<List<String>> listOfLists = new ArrayList<List<String>>();

    listOfLists.add(listOfStrings);

    String s = listOfLists.get(0).get(0);

    System.out.println(s); // prints "Hello again"

  }

}

另外Q一个泛型类型接受一个或多个type variable。比如,java.util.Map有两个type variables。第一个定义了密钥QkeyQ的cdQ第二个定义了|value)的类型。下面的例子讲教我们如何使用个一个泛型Map.
package com.brainysoftware.jdk5.app16;

import java.util.HashMap;

import java.util.Map;

public class MapTest {

  public static void main(String[] args) {

    Map<String, String> map = new HashMap<String, String>();

    map.put("key1", "value1");

    map.put("key2", "value2");

    String value1 = map.get("key1");

  }

}
在这个例子中Q重新得C个key1代表的String|我们不需要Q何类型{换?/P>

 

没有参数的情况下使用泛型


    既然在J2SE 5.0中收集类型已l泛型化Q那么,原来的用这些类型的代码如何呢Q很q运Q他们在JAVA 5中将l箋工作Q因Z能用没有参数的泛型cd。比如,你能l箋像原来一样用List接口Q正如下面的例子一栗?BR>
List stringList1 = new ArrayList();
stringList1.add("Java 1.0 - 5.0");
stringList1.add("without generics");
String s1 = (String) stringList1.get(0);


一个没有Q何参数的泛型cd被称为raw type。它意味着q些为JDK1.4或更早的版本而写的代码将l箋在java 5中工作?/P>

管如此Q一个需要注意的事情是,JDKQ编译器希望你用带参数的泛型类型。否则,~译器将提示警告Q因Z认ؓ你可能忘了定义type variables。比如,~译上面的代码的时候你会看C面这些警告,因ؓW一个List被认为是 raw type?/P>

Note: com/brainysoftware/jdk5/app16/GenericListTest.java
        uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


当你使用raw typeӞ如果你不想看到这些警告,你有几个选择来达到目的:

1.~译时带上参?source 1.4
2.使用@SupressWarnings("unchecked")注释
3.更新你的代码Q用List<Object>. List<Object>的实例能接受Mcd的对象,像是一个raw type List。然而,~译器不会发脾气?/P>

 

使用 Q?通配W?/STRONG>

   前面提过Q如果你声明了一个List<aType>, 那么q个List对aType起作用,所以你能储存下面这些类型的对象Q?BR>1.一个aType的实?BR>2.它的子类的实?如果aType是个c?
3.实现aType接口的类实例(如果aType是个接口)

但是Q请注意Q一个泛型本w是个JAVAcdQ就像java.lang.String或java.io.File一栗传递不同的type variablel泛型可以创Z同的JAVAcd。比如,下面例子中list1和list2引用了不同的cd对象?BR>
List<Object> list1 = new ArrayList<Object>();
List<String> list2 = new ArrayList<String>();


list1指向了一个type variables为java.lang.Objects 的List而list2指向了一个type variables为String 的List。所以传递一个List<String>l一个参CؓList<Object>的函数将Dcompile time错误。下面列表可以说明:

package com.brainysoftware.jdk5.app16;
import java.util.ArrayList;
import java.util.List;
 
public class AllowedTypeTest {
  public static void doIt(List<Object> l) {
  }
  public static void main(String[] args) {
    List<String> myList = new ArrayList<String>();
    // q里生一个错?BR>    doIt(myList);
  }
}

上面的代码无法编译,因ؓ你试图传递一个错误的cdl函数doIt。doIt的参数是List<Object>二你传递的参数是List<String>?/P>

可以使用 Q?通配W解册个难题。List<?> 意味着一个对M对象起作用的List。所以,doIt可以改ؓQ?BR> 
public static void doIt(List<?> l) {}

    在某些情况下你会考虑使用 ? 通配W。比如,你有一个printList函数Q这个函数打C个List的所有成员,你想让这个函数对Mcd的List起作用时。否则,你只能篏ȝzȝ写很多printList的重载函数。下面的列表引用了?? 通配W的printList函数?/P>

package com.brainysoftware.jdk5.app16;
import java.util.ArrayList;
import java.util.List;
 
public class WildCardTest {
 
  public static void printList(List<?> list) {
    for (Object element : list) {
      System.out.println(element);
    }
  }
  public static void main(String[] args) {
    List<String> list1 = new ArrayList<String>();
    list1.add("Hello");
    list1.add("World");
    printList(list1);
 
    List<Integer> list2 = new ArrayList<Integer>();
    list2.add(100);
    list2.add(200);
    printList(list2);
  }
}
q些代码说明了在printList函数中,List<?>表示各种cd的List对象。然而,h意,在声明的时候?? 通配W是不合法的Q像q样Q?BR>
List<?> myList = new ArrayList<?>(); // 不合?/FONT>

如果你想创徏一个接收Q何类型对象的ListQ你可以使用Object作ؓtype variableQ就像这P

List<Object> myList = new ArrayList<Object>();


在函C使用界限通配W?/P>

在之前的章节中,你学会了通过传递不同的type variables来创Z同JAVAcd的泛型,但ƈ不考虑type variables之间的承关pR在很多情况下,你想一个函数有不同的List参数。比如,你有一个函数getAverageQ他q回了一个List中成员的q_倹{然而,如果你把List<Number>作ؓetAverage的参敎ͼ你就没法传递List<Integer> 或List<Double>参数Q因为List<Number>和List<Integer> 和List<Double>不是同样的类型。你能用raw type 或用通配W,但这h法在compile timeq行安全cd查,因ؓ你能传递Q何Q何类型的ListQ比如List<String>的实例。你可以使用List<Number>作ؓ参数Q但是你只能传递List<Number>l函数。但q样׃你的函数功能减少Q因Z可能更多的时候要操作List<Integer>或List<Long>Q而不是List<Number>?/P>

J2SE5.0增加了一个规则来解决了这U约束,q个规则是允许你定义一个上?upper bound) type variable.在这U方式中Q你能传递一个类型或它的子类。在上面getAverage函数的例子中Q你能传递一个List<Number>或它的子cȝ实例Q比如List<Integer> or List<Float>?/P>

使用上界规则的语法这么定义的QGenericType<? extends upperBoundType>. 比如Q对getAverage函数的参敎ͼ你可以这么写List<? extends Number>. 下面例子说明了如何用这U规则?/P>

package com.brainysoftware.jdk5.app16;
import java.util.ArrayList;
import java.util.List;
public class BoundedWildcardTest {
  public static double getAverage(List<? extends Number> numberList)
  {
    double total = 0.0;
    for (Number number : numberList)
      total += number.doubleValue();
    return total/numberList.size();
  }
 
  public static void main(String[] args) {
    List<Integer> integerList = new ArrayList<Integer>();
    integerList.add(3);
    integerList.add(30);
    integerList.add(300);
    System.out.println(getAverage(integerList)); // 111.0
    List<Double> doubleList = new ArrayList<Double>();
    doubleList.add(3.0);
    doubleList.add(33.0);
    System.out.println(getAverage(doubleList)); // 18.0
  }
}
׃有了上界规则Q上面例子中的getAverage函数允许你传递一个List<Number> 或一个type variable是Q何java.lang.Number子类的List?/P>


下界规则

关键字extends定义了一个type variable的上界。通过使用super关键字,我们可以定义一个type variable的下界,管通用的情况不多。比如,如果一个函数的参数是List<? super Integer>Q那么意味着你可以传递一个List<Integer>的实例或者Q何java.lang.Integer的超c?superclass)?/P>


创徏泛型c?/P>


前面的章节主要说明了如何使用泛型类Q特别是Collections framework中的cR现在我们开始学习如何写自己的泛型类?/P>

基本上,除了声明一些你惌使用的type variables外,一个泛型类和别的类没有什么区别。这些type variables位于cd后面?lt;>中。比如,下面的Point是个泛型类。一个Point对象代表了一个系l中的点Q它有横坐标和纵坐标。通过使Point泛型化,你能定义一个点实例的精程度。比如,一个Point对象需要非常精,你能把Double作ؓtype variable。否则,Integer 够了?/P>

package com.brainysoftware.jdk5.app16;
public class Point<T> {
  T x;
  T y;
  public Point(T x, T y) {
    this.x = x;
    this.y = y;
  }
  public T getX() {
    return x;
  }
  public T getY() {
    return y;
  }
  public void setX(T x) {
    this.x = x;
  }
  public void setY(T y) {
    this.y = y;
  }
}

在这个例子中QT是Point的type variable 。T是getX和getY的返回值类型,也是setX和setY的参数类型。此外,构造函数结合两个T参数?/P>

使用pointcd像用别的类一栗比如,下面的例子创Z两个Point对象Qponint1和point2。前者把Integer作ؓtype variableQ而后者把Double作ؓtype variable?BR>
Point<Integer> point1 = new Point<Integer>(4, 2);
point1.setX(7);
Point<Double> point2 = new Point<Double>(1.3, 2.6);
point2.setX(109.91);

ȝ

泛型使代码在compile time有了更严格的cd查。特别是在Collections framework中,泛型有两个作用。第一Q他们增加了Ҏ集类?collection typesQ在compile time的类型检查,所以收集类所能持有的cd对传递给它的参数cd起了限制作用。比如你创徏了一个持有strings的java.util.List实例Q那么他将不能接受Integers或别的类型。其ơ,当你从一个收集中取得一个元素时Q泛型消除了cd转换的必要?/P>

泛型能够在没有type variable的情况下使用Q比如,作ؓraw types。这些措施让Java 5之前的代码能够运行在JRE 5中。但是,Ҏ的应用程序,你最好不要用raw typesQ因Z后Java可能不支持他们?/P>


你已l知道通过传递不同类型的type variablel泛型类可以产生不同的JAVAcd。就是说List<String>和List<Object>的类型是不同的。尽String是java.lang.Object。但是传递一个List<String>l一个参数是List<Object>的函C参数会生编译错误(compile errorQ。函数能?? 通配W其接受Q何类型的参数。List<?> 意味着Mcd的对象?/P>

最后,你已l看C写一个泛型类和别的一般JAVAcL有什么区别。你只需要在cd名称后面?lt;>中声明一pd的type variablesp了。这些type variables是q回值类型或者参数类型。根据惯例,一?BR>type variable用一个大写字母表C?/P>

R.Zeus 2005-08-17 21:56 发表评论
]]>
Generics in J2SE 5.0http://www.tkk7.com/RR00/articles/10046.htmlR.ZeusR.ZeusSat, 13 Aug 2005 10:31:00 GMThttp://www.tkk7.com/RR00/articles/10046.htmlhttp://www.tkk7.com/RR00/comments/10046.htmlhttp://www.tkk7.com/RR00/articles/10046.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/10046.htmlhttp://www.tkk7.com/RR00/services/trackbacks/10046.html阅读全文

R.Zeus 2005-08-13 18:31 发表评论
]]>
read properties from filehttp://www.tkk7.com/RR00/articles/9862.htmlR.ZeusR.ZeusThu, 11 Aug 2005 17:04:00 GMThttp://www.tkk7.com/RR00/articles/9862.htmlhttp://www.tkk7.com/RR00/comments/9862.htmlhttp://www.tkk7.com/RR00/articles/9862.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/9862.htmlhttp://www.tkk7.com/RR00/services/trackbacks/9862.html      before taday,I always read properties from file use by useing classes defined ,It is good luck for me to find the existent class to use when I read source of  log4j

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

public class test {

 //static Logger logger = Logger.getLogger(test.class);

 private static ClassLoader getTCL() throws IllegalAccessException,
   InvocationTargetException {

  // Are we running on a JDK 1.2 or later system?
  Method method = null;
  try {
   method = Thread.class.getMethod("getContextClassLoader", null);
  } catch (NoSuchMethodException e) {
   // We are running on JDK 1.1
   return null;
  }

  return (ClassLoader) method.invoke(Thread.currentThread(), null);
 }

 static public URL getResource(String resource) {
  ClassLoader classLoader = null;
  URL url = null;

  try {
   classLoader = getTCL();
  } catch (IllegalAccessException e) {
   // TODO 自动生成 catch ?BR>   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO 自动生成 catch ?BR>   e.printStackTrace();
  }

  url = classLoader.getResource(resource);

  return url;

 }

 public static void main(String argv[]) {
  //  BasicConfigurator.configure();
  //  logger.debug("Hello world.");
  //  logger.info("What a beatiful day.");
  
  Properties props = new Properties();
  java.net.URL configURL;
  try {
   configURL = getResource("b.txt");
   props.load(configURL.openStream());
   String value = props.getProperty("a");
   System.out.print(value);
  } catch (MalformedURLException e) {
   // TODO 自动生成 catch ?BR>   e.printStackTrace();
  } catch (IOException e1) {
   // TODO 自动生成 catch ?BR>   e1.printStackTrace();
  }
 }
}

in properties file,# is remark



R.Zeus 2005-08-12 01:04 发表评论
]]>
վ֩ģ壺 Ʒ޾Ʒ| ģ߹ۿ| þƷww47| ŮѾƷëƬ| ѿwwwƵ| ݺady޾Ʒ| ŮڵƵվ| ޾Ʒ߹ۿ| ԲľƷƵѿ| Ʒձ777| ޹㽶þþþþ| ѿһһƬ| av| վѹۿ| þþþAV| һvһ| ޵һ| һƬѲ| ߹ۿ| þþƷһ| ˳վ| AVXXX鶹| ޾ƷþþþAV鶹| ߹ۿƵ| AVŮһ| ޻ɫƬѿ| avרԭ| ͵޾Ʒҳ65ҳ| xxxxձ| ɫþƷƵ| Ʒһ| 91avѹۿ| AVվ߹ۿ| þþƷ޾Ʒ| 1000ֹ߽ѹۿƵ| ŷպĶ| ۺϾƷվ߹ۿ| 100018Ƶ | ޾Ʒɫһ| ߹ۿվ| ߹ۿ|