??xml version="1.0" encoding="utf-8" standalone="yes"?>国产成人精品一区二区三区四区 ,欧美xxxx免费虐,午夜精品久久久久久久四虎美女版http://www.aygfsteel.com/baogenfly/category/15604.htmlzh-cnWed, 28 Feb 2007 23:17:55 GMTWed, 28 Feb 2007 23:17:55 GMT60[转蝲] JNI初步http://www.aygfsteel.com/baogenfly/articles/88712.html王某?/dc:creator>王某?/author>Tue, 19 Dec 2006 02:49:00 GMThttp://www.aygfsteel.com/baogenfly/articles/88712.htmlhttp://www.aygfsteel.com/baogenfly/comments/88712.htmlhttp://www.aygfsteel.com/baogenfly/articles/88712.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/88712.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/88712.htmlhttp://tb.blog.csdn.net/TrackBack.aspx?PostId=910492

一、去q曾l做q一个JAVA和C通信的项目,用C语言实现某个特定功能函数Q然后在JAVA中调用。最q又有朋友问P于是做q的东西整理成学习笔讎ͼ希望对后来者有所帮助Q呵c?/p>

二、jni例子?/p>

jni-java native interface是java与其他语a本地通信的接口,按照jni的约定,javaE序可以调用其他语言~写的函敎ͼ其他语言也可以调用java实现的方法,本文介绍了一个最单的java调用cҎ的例子。主要由两部分组成,

一个是javaȝ序,另一个是被调用的dllQ由C语言实现Q?/p>

三、编写步?/p>

1、javaȝ?/p>

//文gtestdll.java

public class testdll
{
        static
        {
               System.loadLibrary("goodluck");     //goodluck用的库名,不必加dll后缀
        }  

        /*下面两个函数为C语言实现的方?/
        public native static int get();
        public native static void set(int i);

       //d?br />        public static void main(String[] args)
        {
                testdll test = new testdll();
                test.set(10);                                         //调用了在C中实现的Ҏset
                System.out.println(test.get());          //调用了在C中实现的Ҏget
                System.out.println("hello world!\n");
        }
}

2、得到C所需的头文g

 执行如下命oQ假设jdk的安装\径ؓD:\Java\jdk1.5.0\binQ?/p>

    D:\Java\jdk1.5.0\bin\javac   testdll.java                                         #生成testdll.class文g

   D:\Java\jdk1.5.0\bin\javah   testdll                                                  #Ҏtestdll.class生成testdll.h文g

 

3、用c语言生成所需的dll

(1) 打开vc6, 建立一个空的dll目?/p>

       菜单里?File"->New->Projects->Win32  dynamic link library,目名ؓgookluckQ与java文g中的调用库名保持一_Q第二步里选an empty DLL project.

(2)把前面生成的testdll.h复制到这个项目中Q再新添下面的testdll.cpp文g

#include "testdll.h"

int i = 0;
JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass)
{
        return i;
}

JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j)
{
         i = j;
}

此时~译会报错,因ؓq缺几个头文gQ因此要把下面两个文件复制到目目录中或者vc的头文g目录 (即Microsoft Visual Studio\VC98\Include)?jni.h文g和jni_md.h文gQ在我机器这两个文g原始目录?/p>

D:\Java\jdk1.5.0\include\jni.h

D:\Java\jdk1.5.0\include\win32\jni_md.h

再次~译Q运行通过

4、测试运?/p>

    ?中得到的testdll.dll文g拯到testdll.class所在目录下Q执?/p>

    D:\Java\jdk1.5.0\bin\java testdll

    q行l果?/p>

    10
    hello world!

   说明调用正确?br />

五、几个注意事?/p>

1、System.loadLibrary("goodluck"); 一句,有时在别的情况下会报错,主要原因是程序找不到q个库文件所在的路径Q此时可以检查path环境变量是否包含了合适的路径Q或者把dll文g拯到系l的dll目录下,如C:\WINNT\system32?/p>

2、库文g不必写后~名,写了反而可能错Q因时程序会Ltestdll.dll.dll文gQ自然是不存在了Q?

3、要调用的方法做本地声明Q关键字为native。ƈ且只需要声明,而不需要具体实现。如Q?
      public native static void set(int i);
      public native static int get();

4、函数返回类型尽量用jni中的基本cdQ如整型、字W等Q,最好不要返回自定义的类Q那样在c里有可能会引起错误?/p>

5、在c中写具体实现的时候,我们只关心两个函数原?

JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass);


JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint);

q里JNIEXPORT都是JNI的关键字Q表C此函数是要被java调用的,如果是其他语a调java语言QJNIEXPORTp变成JNIIMPORT了?/p>

JNICALL告诉C~译器参数进栈的方式Q我们不必关?

jint是以JNIZ介JAVA的intcd与本地c语言的int沟通的一U类型,我们当做int使用。其他类型名可以Ljni帮助文.

函数的名U是JAVA_再加上javaE序的package路径再加函数名组成的?/p>

参数中,我们也只需要关心在JAVAE序中存在的参数Q即W三个往后的参数。至于JNIEnv*和jclass我们一般没有必要去它?/p>

知道q些U定后,我们没有javahE序也可以自己手工写出testdll.h文g来,不过ȝ些就是了^O^

六、jni的其他参考资?/p>

jni属于java语言的一部分Q其权威资料自然要到sun的站点上查找(www.sun.com)

以下是我在sun公司|站上找到的一些页?/p>

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html

l出了一个jni的例子,

http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html

l出了完整的jni介绍Q?/p>

另外Qlinux下jni的例子可以参?a >http://www.linuxmine.com/5629.html

七、附javah生成的testdll.h文g?/p>

/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class testdll */

#ifndef _Included_testdll
#define _Included_testdll
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     testdll
 * Method:    get
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_testdll_get
  (JNIEnv *, jclass);

/*
 * Class:     testdll
 * Method:    set
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_testdll_set
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif



]]>
关于hashCodeҎhttp://www.aygfsteel.com/baogenfly/articles/85859.html王某?/dc:creator>王某?/author>Wed, 06 Dec 2006 07:28:00 GMThttp://www.aygfsteel.com/baogenfly/articles/85859.htmlhttp://www.aygfsteel.com/baogenfly/comments/85859.htmlhttp://www.aygfsteel.com/baogenfly/articles/85859.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/85859.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/85859.html 在java.lang.Object的规范中Q对hasCode有如下的U定Q?br /> 1 在一个应用程序执行期_如果一个对象的equalsҎ做比较所用到的信息没有被修改的话Q那么对该对象调用多ơhashCodeҎQ它必须q回相同的整数。在同一E序的多ơ执行过E中Q这个整数方法可以不同?br />2 如果两个对象ҎequalsҎ是相{的Q那么调用这两个对象的hashCodeҎ必须产生同样的整数结果?br />3 如果两个对象ҎequalsҎ是不相等的,那么调用q两个对象的hashCodeҎQ不要求产生不同的整数结果?/font>



]]>
关于对象相等http://www.aygfsteel.com/baogenfly/articles/85849.html王某?/dc:creator>王某?/author>Wed, 06 Dec 2006 07:15:00 GMThttp://www.aygfsteel.com/baogenfly/articles/85849.htmlhttp://www.aygfsteel.com/baogenfly/comments/85849.htmlhttp://www.aygfsteel.com/baogenfly/articles/85849.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/85849.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/85849.html 下面是李sir发给我的
Integer j1 = 127;
Integer j2 = 127;
System.out.println( j1==j2); //True

Integer k1 = 128;
Integer k2 = 128;
System.out.println( k1==k2); //False

Integer w1 = -128;
Integer w2 = -128;
System.out.println( w1==w2); //True

Integer m1 = -129;
Integer m2 = -129;
System.out.println( m1==m2); //False

I've seen a lot of posts in this thread about what is happening on ==

It's simple:

When we do
        Integer i = 127;
autoboxing turns this into:
        Integer i = Integer.valueOf( 127 );

Go look at the implementation of Integer.valueOf(), and you'll find:
    public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
        return new Integer(i);
    }

If the int value is between -128 and 127, it will return a cached Integer value. This way we avoid creating many duplicate Integer objects for those common values. It save's on memory and improves performance.

And, of course, it does mean that Integer i = 127 will always return the same Integer instance. 

再联惛_以前讨论的String问题Q?br />                String a = new String("Hello World");
                String b = "Hello World";
                String c ="Hello World";
q里创徏了几个对象,a==b么?b==c么?

q里只创Z两个新对象,一个是“Hello World”对象,另一个是与“Hello World”有相同值的对象。a b c都是对象引用。其中a==b为fasleQ因为a b指向不同的对象,b==c为trueQ这是因为b c都指向同一个对象。因为String是一个不可变的对象,而String的直接量赋g自动L以前生成了的内容相同的实例赋l引用,若以前没有内容相同的实例存在Q则创徏新实例。故会发生b==c?/font>



]]>
[转蝲]Java 范型http://www.aygfsteel.com/baogenfly/articles/84304.html王某?/dc:creator>王某?/author>Wed, 29 Nov 2006 03:27:00 GMThttp://www.aygfsteel.com/baogenfly/articles/84304.htmlhttp://www.aygfsteel.com/baogenfly/comments/84304.htmlhttp://www.aygfsteel.com/baogenfly/articles/84304.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/84304.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/84304.html 在已发布的Java1.4中在核心代码库中增加了许多新的API(如Loging,正则表达式,NIO){,在最新发布的JDK1.5和即发布的JDK1.6中也新增了许多APIQ其中比较有重大意义的就是Generics(范型Q?br />
  一.什么是Generics?

  Generics可以UC为参数类?parameterized types),q译器来验证从客户端将一U类型传送给某一对象的机制。如Java.util.ArrayList,

  ~译器可以用Generics来保证类型安全?br />在我们深入了解Generics之前,我们先来看一看当前的java 集合框架QCollection)。在j2SE1.4中所有集合的Root Interface是Collection

Collections example without genericity: Example 1


						1 protected void collectionsExample() {
2  ArrayList list = new ArrayList();
3  list.add(new String("test string"));
4  list.add(new Integer(9)); // purposely placed here to create a runtime ClassCastException
5  inspectCollection(list);
6 }
7
8
9 protected void inspectCollection(Collection aCollection) {
10  Iterator i = aCollection.iterator();
11  while (i.hasNext()) {
12   String element = (String) i.next();
13  }
14 }


  以上的样例程序包含的两个ҎQcollectionExampleҎ建立了一个简单的集合cdArrayListQƈ在ArrayList中增加了一个String和一个Integer对象.而在inspecCollectionҎ中,我们q代q个ArrayList用Stringq行Cast。我们看W二个方法,出C一个问?Collection在内部用的是ObjectQ而我们要取出Collection中的对象?需要进行CastQ那么开发者必需用实际的cdq行Cast,像这U向下造型Q编译器?br />
  法进行检?如此一来我们就要冒在代码在q行抛出ClassCastException的危险。我们看inspecCollectionҎQ编译时没有问题Q但在运行时׃抛出ClassCastException异常。所以我们一定要q离q个重大的运行时错误


  ?使用Generics
  从上一章节中的CassCastExceptionq种异常Q我们期望在代码~译时就能够捕捉?下面我们使用范型修改上一章的样例E序?br />//Example 2
						1 protected void collectionsExample() {
2  ArrayList<String> list = new ArrayList<String>();
3  list.add(new String("test string"));
4  // list.add(new Integer(9)); this no longer compiles
5  inspectCollection(list);
6 }
7
8
9 protected void inspectCollection(Collection<String> aCollection) {
10  Iterator<String> i = aCollection.iterator();
11  while(i.hasNext()) {
12   String element = i.next();
13  }
14 }


  从上面第2行我们在创徏ArrayList时用了新语法,在JDK1.5中所有的Collection都加入了Generics的声明。例:
//Example 3
						1 public class ArrayList<E> extends AbstractList<E> {
2  // details omitted...
3  public void add(E element) {
4   // details omitted
5  }
6  public Iterator<E> iterator() {
7   // details omitted
8  }
9 }


  q个E是一个类型变量,q没有对它进行具体类型的定义,它只是在定义ArrayList时的cd占位W?在Example 2中的我们在定义ArrayList的实

  例时用Stringl定在E?当我们用add(E element)Ҏ向ArrayList中增加对象时, 那么像下面的写法一P public void add(String element)Q因为在ArrayList所有方法都会用String来替代E,无论是方法的参数q是q回倹{这时我们在看Example 2中的W四行,~译׃反映出编译错误?br />所以在java中增加Generics主要的目的是Z增加cd安全?br />
  通过上面的简单的例子我们看到使用Generics的好处有Q?br />  1.在类型没有变化时QCollection是类型安全的?br />  2.内在的类型{换优于在外部的h工造型?br />  3.使Java 接口更加强壮,因ؓ它增加了cd?br />  4.cd的匹配错误在~译阶段可以捕捉到Q而不是在代码q行时?br />
  受约束类型变?br />虽然许多Class被设计成GenericsQ但cd变量可以是受限的
public class C1<T extends Number> { }
public class C2<T extends Person & Comparable> { }
W一个T变量必须l承NumberQ第二个T必须l承Person和实现Comparable

  ?Generics Ҏ

  像GenericscMPҎ和构造函C可以有类型参数。方法的参数的返回值都可以有类型参敎ͼq行Generics?br />//Example 4
						1 public <T extends Comparable> T max(T t1, T t2) {
2  if (t1.compareTo(t2) > 0)
3   return t1;
4  else return t2;
5 }


  q里QmaxҎ的参数类型ؓ单一的TcdQ而Tcdl承了ComparableQmax的参数和q回值都有相同的类。下面的Example 5昄了maxҎ的几个约束?br />//Example 5 
						1 Integer iresult = max(new Integer(100), new Integer(200));
2 String sresult = max("AA", "BB");
3 Number nresult = max(new Integer(100), "AAA"); // does not compile


在Example 5W?行参数都为IntegerQ所以返回g是IntegerQ注意返回值没有进行造型?br />在Example 5W?行参数都为StringQ所以返回g是StringQ注意返回值没有进行造型。以上都调用了同一个方法?br />在Example 5W?行生以下编译错误:
Example.java:10: incompatible types
found  : java.lang.Object&java.io.Serializable&java.lang.Comparable<?>
required: java.lang.Number
    Number nresult = max(new Integer(100), "AAA");


  q个错误发生是因为编译器无法定q回值类型,因ؓString和Integer都有相同的超cObject,注意q我们修正了第三行Q这行代码在q行仍然会报错,因ؓ比较了不同的对象?br />
  ?向下兼容
  M一个新的特色在新的JDK版本中出来后Q我们首先关心的是如何于以前~写的代码兼宏V也是说我们编写的Example 1E序不需要Q何的改变可以运?但是~译器会l出一?ROW TYPE"的警告。在JDK1.4中编写的代码如何在JVM1.5中完全兼容运?我们要h工进行一?Type erasure处理q程

  ?通配W?br />
//Example 6
						List<String> stringList = new ArrayList<String>(); //1
List<Object> objectList = stringList ;//2
objectList .add(new Object()); // 3
String s = stringList .get(0);//4


  乍一看,Example

  6是正的。但stringList本意是存放Stringcd的ArrayList,而objectList中可以存入Q何对象,当在W?行进行处理时QstringList也就无法保证是Stringcd的ArrayList,此时~译器不允许q样的事出现Q所以第3行将无法~译?br />
//Example 7
						void printCollection(Collection<Object> c) 
{ for (Object e : c) {
System.out.println(e);
}}


  Example 7的本意是打印所有Collection的对?但是正如Example 6所说的Q编译会报错Q此时就可以用通配W“?”来修改Example 7

//Example 8
						void printCollection(Collection<?> c) 
{ for (Object e : c) {
System.out.println(e);
}}


Example 8中所有Collectioncd可以方便的打印?br />
  有界通配W?<T extends Number>(上界) <T super Number>(下界)

  ?创徏自己的范?br />以下代码来自http://www.java2s.com/ExampleCode/Language-Basics
1.一个参数的Generics
//Example 9(没有使用范型)
						class NonGen {  
  Object ob; // ob is now of type Object
  // Pass the constructor a reference to  
  // an object of type Object
  NonGen(Object o) {  
    ob = o;  
  }  
  // Return type Object.
  Object getob() {  
    return ob;  
  }  
  // Show type of ob.  
  void showType() {  
    System.out.println("Type of ob is " +  
                       ob.getClass().getName());  
  }  
}  
// Demonstrate the non-generic class.  
public class NonGenDemo {  
  public static void main(String args[]) {  
    NonGen iOb;  
    // Create NonGen Object and store
    // an Integer in it. Autoboxing still occurs.
    iOb = new NonGen(88);  
    // Show the type of data used by iOb.
    iOb.showType();
    // Get the value of iOb.
    // This time, a cast is necessary.
    int v = (Integer) iOb.getob();  
    System.out.println("value: " + v);  
    System.out.println();  
    // Create another NonGen object and  
    // store a String in it.
    NonGen strOb = new NonGen("Non-Generics Test");  
    // Show the type of data used by strOb.
    strOb.showType();
    // Get the value of strOb.
    // Again, notice that a cast is necessary.  
    String str = (String) strOb.getob();  
    System.out.println("value: " + str);  
    // This compiles, but is conceptually wrong!
    iOb = strOb;
    v = (Integer) iOb.getob(); // runtime error!
  }  
}
  

//Example 10(使用范型)
						class Example1<T>{
private T t;
Example1(T o){
  this.t=o;
  }
T getOb(){
  return t;
}
void ShowObject(){
  System.out.println("对象的类型是Q?+t.getClass().getName());
}
}
public class GenericsExample1 {

/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  Example1<Integer> examplei=new Example1<Integer>(100);
  examplei.ShowObject();
  System.out.println("对象是:"+examplei.getOb());
  Example1<String> examples=new Example1<String>("Bill");
  examples.ShowObject();
  System.out.println("对象是:"+examples.getOb());
}

}


  我们来看Example 9没有使用范型Q所以我们需要进行造型Q而Example 10我们不需要Q何的造型

2.二个参数的Generics

//Example 11
						class TwoGen<T, V> { 
   T ob1;
   V ob2;
   // Pass the constructor a reference to  
   // an object of type T.
   TwoGen(T o1, V o2) {
     ob1 = o1;
     ob2 = o2;
   }
   // Show types of T and V.
   void showTypes() {
     System.out.println("Type of T is " +
                        ob1.getClass().getName());
     System.out.println("Type of V is " +
                        ob2.getClass().getName());
   }
   T getob1() {
     return ob1;
   }
   V getob2() {
     return ob2;
   }
}

public class GenericsExampleByTwoParam {

/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  TwoGen<Integer, String> tgObj =
       new TwoGen<Integer, String>(88, "Generics");
     // Show the types.
     tgObj.showTypes();
     // Obtain and show values.
     int v = tgObj.getob1();
     System.out.println("value: " + v);
     String str = tgObj.getob2();
     System.out.println("value: " + str);
   }

}


3.Generics的Hierarchy

//Example 12
						class Stats<T extends Number> {  
   T[] nums; // array of Number or subclass
   // Pass the constructor a reference to  
   // an array of type Number or subclass.
   Stats(T[] o) {  
     nums = o;  
   }  
   // Return type double in all cases.
   double average() {  
     double sum = 0.0;
     for(int i=0; i < nums.length; i++)  
       sum += nums[i].doubleValue();
     return sum / nums.length;
   }  
}  
public class GenericsExampleByHierarchy {


/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub

   Integer inums[] = { 1, 2, 3, 4, 5 };
     Stats<Integer> iob = new Stats<Integer>(inums);  
     double v = iob.average();
     System.out.println("iob average is " + v);
     Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
     Stats<Double> dob = new Stats<Double>(dnums);  
     double w = dob.average();
     System.out.println("dob average is " + w);
     // This won't compile because String is not a
     // subclass of Number.
//     String strs[] = { "1", "2", "3", "4", "5" };
//     Stats<String> strob = new Stats<String>(strs);  
//     double x = strob.average();
//     System.out.println("strob average is " + v);
   }  
}
  

  4.使用通配W?br />//Example 14
						class StatsWildCard<T extends Number> {
T[] nums; // array of Number or subclass
// Pass the constructor a reference to
// an array of type Number or subclass.
StatsWildCard(T[] o) {
  nums = o;
}
// Return type double in all cases.
double average() {
  double sum = 0.0;
  for (int i = 0; i < nums.length; i++)
   sum += nums[i].doubleValue();
  return sum / nums.length;
}
// Determine if two averages are the same.
// Notice the use of the wildcard.
boolean sameAvg(StatsWildCard<?> ob) {
  if (average() == ob.average())
   return true;
  return false;
}
}

public class GenericsExampleByWildcard {

/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  Integer inums[] = { 1, 2, 3, 4, 5 };
  StatsWildCard<Integer> iob = new StatsWildCard<Integer>(inums);
  double v = iob.average();
  System.out.println("iob average is " + v);
  Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
  StatsWildCard<Double> dob = new StatsWildCard<Double>(dnums);
  double w = dob.average();
  System.out.println("dob average is " + w);
  Float fnums[] = { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F };
  StatsWildCard<Float> fob = new StatsWildCard<Float>(fnums);
  double x = fob.average();
  System.out.println("fob average is " + x);
  // See which arrays have same average.
  System.out.print("Averages of iob and dob ");
  if (iob.sameAvg(dob))
   System.out.println("are the same.");
  else
   System.out.println("differ.");
  System.out.print("Averages of iob and fob ");
  if (iob.sameAvg(fob))
   System.out.println("are the same.");
  else
   System.out.println("differ.");

}

}


  5.使用边界通配W?br />//Example 15
						class TwoD { 
  int x, y;
  TwoD(int a, int b) {
    x = a;
    y = b;
  }
}
// Three-dimensional coordinates.
class ThreeD extends TwoD {
  int z;
  ThreeD(int a, int b, int c) {
    super(a, b);
    z = c;
  }
}
// Four-dimensional coordinates.
class FourD extends ThreeD {
  int t;
  FourD(int a, int b, int c, int d) {
    super(a, b, c);
    t = d;  
  }
}
// This class holds an array of coordinate objects.
class Coords<T extends TwoD> {
  T[] coords;
  Coords(T[] o) { coords = o; }
}
// Demonstrate a bounded wildcard.
public class BoundedWildcard {
  static void showXY(Coords<?> c) {
    System.out.println("X Y Coordinates:");
    for(int i=0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " +
                         c.coords[i].y);
    System.out.println();
  }
  static void showXYZ(Coords<? extends ThreeD> c) {
    System.out.println("X Y Z Coordinates:");
    for(int i=0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " +
                         c.coords[i].y + " " +
                         c.coords[i].z);
    System.out.println();
  }
  static void showAll(Coords<? extends FourD> c) {
    System.out.println("X Y Z T Coordinates:");
    for(int i=0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " +
                         c.coords[i].y + " " +
                         c.coords[i].z + " " +
                         c.coords[i].t);
    System.out.println();
  }
  public static void main(String args[]) {
    TwoD td[] = {
      new TwoD(0, 0),
      new TwoD(7, 9),
      new TwoD(18, 4),
      new TwoD(-1, -23)
    };
    Coords<TwoD> tdlocs = new Coords<TwoD>(td);    
    System.out.println("Contents of tdlocs.");
    showXY(tdlocs); // OK, is a TwoD
//  showXYZ(tdlocs); // Error, not a ThreeD
//  showAll(tdlocs); // Erorr, not a FourD
    // Now, create some FourD objects.
    FourD fd[] = {
      new FourD(1, 2, 3, 4),
      new FourD(6, 8, 14, 8),
      new FourD(22, 9, 4, 9),
      new FourD(3, -2, -23, 17)
    };
    Coords<FourD> fdlocs = new Coords<FourD>(fd);    
    System.out.println("Contents of fdlocs.");
    // These are all OK.
    showXY(fdlocs);  
    showXYZ(fdlocs);
    showAll(fdlocs);
  }
}



6.ArrayList的Generics
//Example 16
						public class ArrayListGenericDemo {
  public static void main(String[] args) {
    ArrayList<String> data = new ArrayList<String>();
    data.add("hello");
    data.add("goodbye");

    // data.add(new Date()); This won't compile!

    Iterator<String> it = data.iterator();
    while (it.hasNext()) {
      String s = it.next();
      System.out.println(s);
    }
  }
}


7.HashMap的Generics
//Example 17
						public class HashDemoGeneric {
  public static void main(String[] args) {
    HashMap<Integer,String> map = new HashMap<Integer,String>();

    map.put(1, "Ian");
    map.put(42, "Scott");
    map.put(123, "Somebody else");

    String name = map.get(42);
    System.out.println(name);
  }
}


8.接口的Generics
//Example 18
						interface MinMax<T extends Comparable<T>> { 
  T min();
  T max();
}
// Now, implement MinMax
class MyClass<T extends Comparable<T>> implements MinMax<T> {
  T[] vals;
  MyClass(T[] o) { vals = o; }
  // Return the minimum value in vals.
  public T min() {
    T v = vals[0];
    for(int i=1; i < vals.length; i++)
      if(vals[i].compareTo(v) < 0) v = vals[i];
    return v;
  }
  // Return the maximum value in vals.
  public T max() {
    T v = vals[0];
    for(int i=1; i < vals.length; i++)
      if(vals[i].compareTo(v) > 0) v = vals[i];
    return v;
  }
}
public class GenIFDemo {
  public static void main(String args[]) {
    Integer inums[] = {3, 6, 2, 8, 6 };
    Character chs[] = {'b', 'r', 'p', 'w' };
    MyClass<Integer> iob = new MyClass<Integer>(inums);
    MyClass<Character> cob = new MyClass<Character>(chs);
    System.out.println("Max value in inums: " + iob.max());
    System.out.println("Min value in inums: " + iob.min());
    System.out.println("Max value in chs: " + cob.max());
    System.out.println("Min value in chs: " + cob.min());
  }
}


9.Exception的Generics
//Example 20
						interface Executor<E extends Exception> {
    void execute() throws E;
}

public class GenericExceptionTest {
    public static void main(String args[]) {
        try {
            Executor<IOException> e =
                new Executor<IOException>() {
                public void execute() throws IOException
                {
                    // code here that may throw an
                    // IOException or a subtype of
                    // IOException
                }
            };

            e.execute();
        } catch(IOException ioe) {
            System.out.println("IOException: " + ioe);
            ioe.printStackTrace();
        }
    }
}  


]]>
关于booleanhttp://www.aygfsteel.com/baogenfly/articles/76572.html王某?/dc:creator>王某?/author>Sat, 21 Oct 2006 12:40:00 GMThttp://www.aygfsteel.com/baogenfly/articles/76572.htmlhttp://www.aygfsteel.com/baogenfly/comments/76572.htmlhttp://www.aygfsteel.com/baogenfly/articles/76572.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/76572.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/76572.html        当编译器把Java源码~译成字节码Ӟ他会用int或者byte来表Cboolean。在Java虚拟机种Qfalse是由整数0来表C的Q所有非零整数都表示true。涉及boolean值得操作则会使用int。另外,boolean数组是当作byte数组来访问的。但是在“堆”区Q他也可以表CZؓ位域?/font>

]]>
关于重蝲http://www.aygfsteel.com/baogenfly/articles/74175.html王某?/dc:creator>王某?/author>Mon, 09 Oct 2006 13:09:00 GMThttp://www.aygfsteel.com/baogenfly/articles/74175.htmlhttp://www.aygfsteel.com/baogenfly/comments/74175.htmlhttp://www.aygfsteel.com/baogenfly/articles/74175.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/74175.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/74175.html  对于重蝲ҎQoverloaded methodQ的选择是静态的Q而对于被改写的(overwritten methodQ方法的选择是动态的?-----摘自《effective java?br />         q个该怎么来理解呢Q其实就是说当出现承关pLQ到底是调用被改写的Ҏq是调用没有被改写的ҎQ是动态的军_的。是父类对象p用父cL法,是子cd象就调用子类Ҏ。而不编译时的类型。如A的子cLB、CQA[ ] test = new A[ ]{ new AQ)Qnew BQ)Qnew CQ)}Q那么在以后通过test[i]来访问每个对象,则test[2]调用的是C对象改写的方法。对于重载方法来_如果~译时类型就已经定Q在q行时虽然类型不同,但是不媄响重载方法的选择。See 《Effective java》P109。?/font>

]]>
java assert的用法[转蝲]http://www.aygfsteel.com/baogenfly/articles/72222.html王某?/dc:creator>王某?/author>Wed, 27 Sep 2006 02:48:00 GMThttp://www.aygfsteel.com/baogenfly/articles/72222.htmlhttp://www.aygfsteel.com/baogenfly/comments/72222.htmlhttp://www.aygfsteel.com/baogenfly/articles/72222.html#Feedback1http://www.aygfsteel.com/baogenfly/comments/commentRss/72222.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/72222.html assert 是在 J2SE1.4 中引入的新特性, assertion 是在代码中包括的布型状态,E序员认个状态是 true 。一般来?/span> assert 在开发的时候是查程序的安全性的Q在发布的时候通常都不使用 assert 。在 1.4 中添加了 assert 关键字和 java.lang.AssertError cȝ支持?/span>

      首先Q我们有必要从一个例子说?/span> assert

public class AssertTest

{

 public static void main(String[] args)

 {

  AssertTest at = new AssertTest();

  at.assertMe(true);

  at.assertMe(false);

  

 }

 

 private  void assertMe(boolean boo)

 {

  assert boo?true:false;

  System.out.println(true condition);

 }

 

}

E序中包含了 assert 的话Q你要用 javac -source 1.4 xxx.java 来编译,否则~译器会报错的。要惌 assert 得部分运行的话,要?/span> java -ea xxx 来运行,否则包含 assert 得行会被忽略。下面我们运?/span>

javac -source 1.4 AssertTest.java

java -ea AssertTest

看看l果的输出是Q?/span>

true condition

Exception in thread main java.lang.AssertionError

        at AssertTest.assertMe(AssertTest.java:13)

        at AssertTest.main(AssertTest.java:7)

当我们运?/span> at.assertMe(true) 得时候,׃ assert boo?true:false 相当?/span> assert true; 因此没有M问题Q程序往下执行打印出 true condition Q但是执?/span> at.assertMe(false) 的时候相当于 assert false Q这个时候解释器׃抛出 AssertionError 了,E序q止了。大家必L?/span> AssertionError 是承自 Error 得,因此你可以不再程序中 catch 它的Q当然你也可以在E序?/span> catch 它然后程序可以l执行。例如:

public class AssertTest

{

 public static void main(String[] args)

 {

  AssertTest at = new AssertTest();

  try

  {

   at.assertMe(true);

   at.assertMe(false);

  }

  catch(AssertionError ae)

  {

   System.out.println(AsseriontError catched);

  }

  System.out.println(go on);

  

 }

 

 private  void assertMe(boolean boo)

 {

  assert boo?true:false;

  System.out.println(true condition);

 }

 

}

 

    assert q有另外一U表辄方式Q就?/span> assert exp1:exp2; 其中 exp1 是个 boolean q回值得表达式,?/span> exp2 可以是原始的数据cd或者对象都可以例如Q?/span>

   boolean boo = true;

   String str = null;

    assert boo = false Q?/span> str=error;

我们刚开始讲?/span> assert exp1 得Ş式,?/span> exp1 ?/span> false 得时候, AssertionError 得默认构造器会被调用Q但?/span> assert exp1:exp2 q样的Ş式,?/span> exp1 ?/span> true 的时候后?/span> exp2 被或略,如果 false 的话Q后面的表达式的l果会被计算出来q作?/span> AssertionError 得构造器参数。看下面的例子:

public class AssertTest

{

 public static void main(String[] args)

 {

  AssertTest at = new AssertTest();

  at.assertMe(true);

  at.assertMe(false);

  

 }

 

 private  void assertMe(boolean boo)

 {

  String s = null;

  assert boo?true:false:s = hello world;

  System.out.println(true condition);

 }

 

} q行的时候会得到q样的结?/span>

true condition

Exception in thread main java.lang.AssertionError: hello world

        at AssertTest.assertMe(AssertTest.java:14)

        at AssertTest.main(AssertTest.java:7)

Assert 最好不要滥用,原因?/span> assert q不一定都?/span> enable 的,下面两种情况׃应该?/span> assert

1 不要?/span> public 的方法里面检查参数是不是?/span> null 之类的操?/span>

例如 public int get(String s)

   {

       assert s != null;

   }

如果需要检查也最好通过 if s = null 抛出 NullPointerException 来检?/span>

2 不要?/span> assert 来检查方法操作的q回值来判断Ҏ操作的结?/span>   

例如 assert list.removeAll(); q样看v来好像没有问?/span> 但是x如果 assert ?/span> disable 呢,那样他就不会被执行了 所?/span> removeAll() 操作没有被执行   可以q样代替
boolean boo = list.removeAl();

assert boo;



]]>
java里的内部c?/title><link>http://www.aygfsteel.com/baogenfly/articles/71529.html</link><dc:creator>王某?/dc:creator><author>王某?/author><pubDate>Sun, 24 Sep 2006 02:15:00 GMT</pubDate><guid>http://www.aygfsteel.com/baogenfly/articles/71529.html</guid><wfw:comment>http://www.aygfsteel.com/baogenfly/comments/71529.html</wfw:comment><comments>http://www.aygfsteel.com/baogenfly/articles/71529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/baogenfly/comments/commentRss/71529.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/baogenfly/services/trackbacks/71529.html</trackback:ping><description><![CDATA[ <font size="2">         java里的嵌套cL四种Q静态成员类Qstatic member classQ、非静态成员类Qnonstatic member classQ、匿名类Qanonymous classQ和局部类Qlocal classQ?<br /><br />         静态成员类和非静态成员类的区别是Q非静态成员类的每一个实例都隐含着与外围类的一个外围实例(enlosing instanceQ紧密关联在一赗在非静态成员类的实例方法内部,调用外围实例上的Ҏ是有可能的,或者用经q修饰的this指针可以得到一个指向外围实例的引用?strong>如果声明的成员类不访问外围实例,则声明ؓ静态成员类。如果用了非静态成员类Q那么它的每一个实例都包含一个额外的指向外围对象的引用?/strong></font> <p> <strong> <font size="2">以上是《effective java》中的论q。下面是在网上找到的关于匿名cȝ叙述?/font> </strong> </p> <p> <font size="2">匿名c?br />       匿名cL不能有名U的c,所以没办法引用它们。必d创徏Ӟ作ؓnew语句的一部分来声明它们。这p采用另一UŞ式的new语句Q如下所C: <br />new <cL接口> <cȝM><br />       q种形式的new语句声明一个新的匿名类Q它对一个给定的c进行扩展,或者实C个给定的接口。它q创建那个类的一个新实例Qƈ把它作ؓ语句的结果而返回。要扩展的类和要实现的接口是new语句的操作数Q后跟匿名类的主体?br />       如果匿名cd另一个类q行扩展Q它的主体可以访问类的成员、覆盖它的方法等{,q和其他M标准的类都是一L。如果匿名类实现了一个接口,它的M必须实现接口的方法?br />        注意匿名cȝ声明是在~译时进行的Q实例化在运行时q行。这意味着for循环中的一个new语句会创建相同匿名类的几个实例,而不是创建几个不同匿名类的一个实例?br />        从技术上_匿名cd被视为非静态的内部c,所以它们具有和Ҏ内部声明的非静态内部类一L权限和限制?br />        如果要执行的d需要一个对象,但却不值得创徏全新的对象(原因可能是所需的类q于单,或者是׃它只在一个方法内部用)Q匿名类显得非常有用。匿名类其适合在Swing应用E序中快速创Z件处理程序?/font> </p> <img src ="http://www.aygfsteel.com/baogenfly/aggbug/71529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/baogenfly/" target="_blank">王某?/a> 2006-09-24 10:15 <a href="http://www.aygfsteel.com/baogenfly/articles/71529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java的模拟多重?/title><link>http://www.aygfsteel.com/baogenfly/articles/71503.html</link><dc:creator>王某?/dc:creator><author>王某?/author><pubDate>Sat, 23 Sep 2006 15:48:00 GMT</pubDate><guid>http://www.aygfsteel.com/baogenfly/articles/71503.html</guid><wfw:comment>http://www.aygfsteel.com/baogenfly/comments/71503.html</wfw:comment><comments>http://www.aygfsteel.com/baogenfly/articles/71503.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/baogenfly/comments/commentRss/71503.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/baogenfly/services/trackbacks/71503.html</trackback:ping><description><![CDATA[ <p>        实现了这个接口的cd以把对于接口Ҏ的调用,转发C个内部私有类的实例上Q而这个内部私有类扩展了骨架实现类。这Ҏ术被UCؓ模拟多重l承Qsimulated mutiple inheritanceQ。 (以上摘至《effective java》)。这点不是很明白。这怎么p多承扯上关pM。是不是q么看,是内部U有cL受了接口实现cL供的ҎQ同时它又extends了接口的骨架实现c,而骨架实现类也是Ҏ口的一U实玎ͼ所以从q个层面上看Q内部私有类相当于既l承了骨架实现类Q又l承了接口实现类?img alt="" src="http://blog.csdn.net/fckeditor/editor/images/smiley/msn/confused_smile.gif" />Q自己都有些p涂了,望高人指点下?/p> <img src ="http://www.aygfsteel.com/baogenfly/aggbug/71503.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/baogenfly/" target="_blank">王某?/a> 2006-09-23 23:48 <a href="http://www.aygfsteel.com/baogenfly/articles/71503.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java里的l承http://www.aygfsteel.com/baogenfly/articles/71502.html王某?/dc:creator>王某?/author>Sat, 23 Sep 2006 15:46:00 GMThttp://www.aygfsteel.com/baogenfly/articles/71502.htmlhttp://www.aygfsteel.com/baogenfly/comments/71502.htmlhttp://www.aygfsteel.com/baogenfly/articles/71502.html#Feedback0http://www.aygfsteel.com/baogenfly/comments/commentRss/71502.htmlhttp://www.aygfsteel.com/baogenfly/services/trackbacks/71502.html java里的实体cL不支持多l承的,也就是Class A 只能使用一ơextends Q但是接口就不同Q一个接口可以承多个接口。例如:

public interface Singer{

 AudioClip Sing(Song s);

}

public interface Songwriter{

 Song compose(boolean hit);

}

public interface SingerSongwriter extends Singer,Songwriter{

 AudioClip strum();

 void actSensitive();

}



]]>
վ֩ģ壺 | ư| ̩| ʯ| û| | ̺| | | | | | | ٲ| | Դ| | | | | ֬| | Զ| | | ۩| ٷ| ˶| | | γ| ƽ| Ƿ| Ͻ| | ߱| | ־| ƽ| | |