紫风乱写

除了他眼前的屏幕,这个人什么也没看见。
被周围的电脑簇拥着,他只知道他所创造的现实,但又意识到那是虚幻。
他已经超越了技术。也超越了机器。
posts - 62, comments - 93, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

看了一下日历,整整有一周没有写blog了。并不是因为自己不想写了,而是因为很忙很忙的过了一周,估计接下来还有一段时间是没空写blog。昨天在和客户讨论完需求之后,偷空看了看Annotation。不过今天是在是没有时间写blog了。时间啊……

posted @ 2005-09-22 23:50 Justfly Shi 阅读(460) | 评论 (0)编辑 收藏

一个大将被调去做外包了,一个大校去做论文了,两个中尉还没有成长起来。
就我一个司令又要排兵布阵又要上场杀敌又要行政又要应付公文往来还要到处去救火
为啥长春开发人员就这么难招呀,别说大将,连小兵都招不来合格的。
命苦呀

posted @ 2005-09-14 00:30 Justfly Shi 阅读(1089) | 评论 (2)编辑 收藏

范型有两种,类范型和方法范型。他们可以用于一些类似于C++中的模板之类的作用。
这里主要有这么几点要注意:
0、范型类之间的转型
1、范型类的继承
2、范型方法的override
3、关键字 super 和exends的使用。

package cn.justfly.study.tiger.generics;

import java.util.Collection;

/**
 * Sample of defining generics type
 * 
 * @author Justfly Shi 
 * created at 2005-8-25 22:03:09
 
*/

public class Defination<G_T, G_B> {
  
private G_T _t = null;

  
private G_B _b = null;

  
public G_B getB() {
    
return _b;
  }


  
public void setB(G_B b) {
    _b 
= b;
  }

  
  
//Generics Method
  <G_A> G_A abc(G_A a,Defination<G_T, ? extends G_B> f)//keyword extends
  {
    
return a;
  }


  
//class Generics Method
  static <G_F,G_F2> void test(G_F[] a, Collection super G_F> b)// keyword super 
  {
    
for (G_F o : a) {
      b.add(o);
    }

  }


  
public G_T getT() {
    
return _t;
  }


  
public void setT(G_T t) {
    _t 
= t;
  }


  
public Defination(G_T t, G_B b) {
    super();
    _b 
= b;
    _t 
= t;
  }


  
/**
   * @param args
   
*/

  
public static void main(String[] args) {
    Defination
<A, A> d = new Defination<A, A>(new AImp2(), new AImp1());
    printDefination(d);

    
// about extends
    Defination<A, A> right = new SubDefination<A, A>(new AImp2(), new AImp1());
    printDefination(right);
    
// Type mismatch: cannot convert from SubDefination// Defination// Defination// AImp1());

    
// Type mismatch: cannot convert from Defination// Defination// Defination// AImp1());
  }


  
private static void printDefination(Defination<A, A> defination) {
    A t 
= defination.getT();
    A b 
= defination.getB();
    System.
out.println(t.getValue());
    System.
out.println(b.getValue());
  }

}


class SubDefination<G_T, G_J> extends Defination<G_T, G_J> {

  
public SubDefination(G_T t, G_J b) {
    super(t, b);
  }

  
  @Override
  
<G_A> G_A abc(G_A a, Defination<G_T, ? extends G_J> f) {
    
return super.abc(a, f);
  }

  
}


class A {
  String getValue() 
{
    
return "class A";
  }

}


class AImp1 extends A {
  
public String getValue() {
    
return "class AImp1";
  }

}


class AImp2 extends A {
  
public String getValue() {
    
return "class AImp2";
  }

}

最后再推荐一篇中文的范型学习资料
在Eclipse 3.1中体验J2SE 5.0的新特性 : 第三部分 :范型

posted @ 2005-09-13 23:50 Justfly Shi 阅读(1191) | 评论 (2)编辑 收藏

Enum也是java中我比较喜欢的一个改进,虽然使用到的地方并不多。
每一个enum类型都默认的继承了java.lang.Enum虚拟类。
每一个列举实例都是改enum类型的一个实例。

package cn.justfly.study.tiger.enums;

/**
 * Sample code of enum
 * 
 * @author Justfly Shi created at 2005-9-12 23:59:59
 
*/
public enum Gentle {
  WOMAN(
":)"), MAN(":|");
  Gentle(String hello) {
    _hello 
= hello;
  }

  String _hello;

  String sayHello() {
    
return _hello;
  }

  
public static void main(String[] args) {
    System.
out.println(Gentle.MAN.getDeclaringClass());

    Gentle[] allGentles 
= Gentle.values();
    System.
out.println("There are " + allGentles.length + " Gentles");
    
for (Gentle g : allGentles) {
      System.
out.println("index: " + g.ordinal() + " name: " + g.name()
          
+ " HelloSmile: " + g.sayHello());
    }
  }
}

posted @ 2005-09-13 00:02 Justfly Shi 阅读(804) | 评论 (0)编辑 收藏

改进的for循环是Java 5.0中的一个让我很喜欢的改进。它只对数组和实现了java.util.Iterable接口的容器类有效。
package cn.justfly.study.tiger.enhancedfor;

import java.util.ArrayList;
import java.util.Collection;


/**
 * The demo of enhanced for statement
 * it can only used for array and classes implements java.util.Iterable interface
 * @author Justfly Shi
 * created at 2005-8-28 21:42:12
 
*/
public class EnhancedFor {

  
/**
   * @param args
   
*/
  
public static void main(String[] args) {
    
//for array
    int[] intArray={1,2,3,4};
    System.
out.println("printing ints:");
    
for(int i:intArray){
      System.
out.println(i);   
    }
    
    
//for Collection
   Collection<String> list=new ArrayList<String>();
    
for(int i=0;i<4;i++){
      list.add(
"String"+i);
    }
    System.
out.println("print Strings in an Collection:");
    
for(String i:list){
      System.
out.println(i);
    }
    
    
//self-define Iterable
    MyIterable<String> myIte=new MyIterable<String>(list);
    System.
out.println("print Stings in an Iterable");
    
for(String i:myIte){
      System.
out.println(i);
    }
    
  }

}

package cn.justfly.study.tiger.enhancedfor;

import java.util.Collection;
import java.util.Iterator;

/**
 * an self-defined Iterable ,that can be used in enhanced-for statement 
 * @author Justfly Shi
 * created at 2005-8-28 22:09:05
 * @param 
 
*/
public class MyIterable<G_E> implements Iterable<G_E> {
  
private Collection<G_E> _list;

  
public Iterator<G_E> iterator() {
    
return new MyIterator<G_E>(_list.iterator());
  }

  
public MyIterable(Collection<G_E> list) {
    _list 
= list;
  }

  
class MyIterator<G_I> implements Iterator<G_I> {
    
private Iterator<G_I> _ite;

    MyIterator(Iterator
<G_I> ite) {
      _ite 
= ite;
    }

    
public boolean hasNext() {
      
return _ite.hasNext();
    }

    
public G_I next() {
      
return _ite.next();
    }

    
public void remove() {
      _ite.remove();

    }
  }
}

输出结果
printing ints:
1
2
3
4
print Strings in an Collection:
String0
String1
String2
String3
print Stings in an Iterable
String0
String1
String2
String3

posted @ 2005-09-12 23:44 Justfly Shi 阅读(1631) | 评论 (0)编辑 收藏

如下列代码所示

1、传入的可变参数将做为一个数组处理
2、一个函数中只能有一个可变参数列,并且只能在函数参数定义的最后。
package cn.justfly.study.tiger;

/**
 * Sample of using varargs
 * @author Justfly Shi
 * created at 2005-8-31 0:38:19
 
*/
public class Varargs {
  
public void printObjectArgs(Object objects ){
    
for(Object o:objects)// objects is an Object Array
    {
      System.
out.println(o);
    }
    System.
out.println("var count:"+ objects.length);
  }
  
public void printFloats(float fs ){
    
for(float f:fs){
      System.
out.println(f);
    }
    System.
out.println("var count:"+ fs.length);
  }
// only one param can be var args,and should be placed at the end of param list   
//  public void printArgs(Integer integers,String objects  ){}

  
/**
   * @param args
   
*/
  
public static void main(String[] args) {
    Varargs vars
=new Varargs();
    vars.printObjectArgs(
new Integer(1),new Integer(2),3,"abc");
    vars.printFloats(
1.2f,1.3f);
  }

}
输出结果:
1
2
3
abc
var count:4
1.2
1.3
var count:2

posted @ 2005-09-12 23:17 Justfly Shi 阅读(812) | 评论 (0)编辑 收藏

在java5中添加的一个新特性就是static import(静态导入?)通过静态导入我们可以很方便的使用在其他类中定义的函数。如下面的代码所示,我们可以直接的使用java.lang.Math 的min和max以及其他在java.lang.Math中所定义的静态方法,只需要在import中添加一句import static java.lang.Math.*。这是一个很方便的功能。

package cn.justfly.study.tiger;

import 
static java.lang.Math.max;
import 
static java.lang.Math.min;
/**
 * Sample of Static Import
 * @author Justfly Shi
 * created at 2005-9-3 23:41:50
 
*/

public class StaticImport {

  
/**
   * @param args
   
*/

  
public static void main(String[] args) {
   
/*
    * min() and max() are defined in java.lang.Math as static method.
    * but they can now be easily access.
    * 
*/

   
int min=min(3,4);
   System.
out.println("min is : "+min);
   
int max=max(3,4);
   System.
out.println("max is : "+max);

  }


}


但是这个功能却不能滥用。因为它会导致代码的可读性变得很差。考虑一下一个未曾接触过java.lang.Math类的读者来看这段代码。当他读到“int min=min(3,4)”,他会很迷惑,这个min函数到底是在哪里定义的呢?于是他就得去分析import这里。这段代码还好说,只有一个类被静态导入,只需要打开 java.lang.Math的文档就可以直接了解这些方法的相关信息了。但是如果同时静态导入了10个类的情况下呢?如果这些类中有着名字类似(相同)但是行为却不一致的方法的时候呢?比如Person.eat(Food food)和 Animal.eat(Food food)。

那么这个功能该如何用呢?我认为一些常用的工具类、全局变量类等当需要在一个类中多次使用的时候可以导进来,但是对于系统中的模型类或者是用的次数不多的工具类还是不要导入的好。我们需要在自己写代码时的方便和代码本身的可读性间做个权衡。

posted @ 2005-09-11 20:15 Justfly Shi 阅读(1111) | 评论 (0)编辑 收藏

要注意的是,当要被unboxing的封装类为null的时候或未被初始化时会抛出一个nullpoint错误

package cn.justfly.study.tiger;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Sample code of autoBoxing and auto-unboxing 
 * @author Justfly Shi
 * created at 2005-8-28 
 
*/

public class Boxing {

  
/**
   * @param args
   
*/

  
public static void main(String[] args) {
    Collection
<Integer> c = new ArrayList<Integer>();

    
for (int i = 0; i < 5; i++{
      c.add(i);
// autoboxing
    }

    System.
out.println("iterate with Interger");
    
for (Integer i : c) {

      System.
out.println(i + 100);// unboxing
    }


    System.
out.println("iterate with int");
    
for (int i : c)// unboxing in enhanced for
    {

      System.
out.println(i + 100);
    }

    
    Integer i
=null;
    
int j=i;//NullPointerException will be throw here
    System.out.println(j);
  }


}

posted @ 2005-09-11 02:07 Justfly Shi 阅读(573) | 评论 (0)编辑 收藏

小弟我新到贵地,请大家多多照顾。
现有gmail邀请函一些,有需要者请留Email

posted @ 2005-09-11 00:16 Justfly Shi 阅读(437) | 评论 (0)编辑 收藏

仅列出标题
共6页: 上一页 1 2 3 4 5 6