锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
3銆佸叧閿瓧 super 鍜宔xends鐨勪嬌鐢ㄣ?br />
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";
}
}
鏈鍚庡啀鎺ㄨ崘涓綃囦腑鏂囩殑鑼冨瀷瀛︿範璧勬枡
鍦‥clipse 3.1涓綋楠孞2SE 5.0鐨勬柊鐗規?: 絎笁閮ㄥ垎 錛氳寖鍨?/a>
/**
* 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());
}
}
}
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);
}
}
}
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
1銆佷紶鍏ョ殑鍙彉鍙傛暟灝嗗仛涓轟竴涓暟緇勫鐞?br />2銆佷竴涓嚱鏁頒腑鍙兘鏈変竴涓彲鍙樺弬鏁板垪錛屽茍涓斿彧鑳藉湪鍑芥暟鍙傛暟瀹氫箟鐨勬渶鍚庛?br />
/**
* 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);
}
}
2
3
abc
var count:4
1.2
1.3
var count:2
浣嗘槸榪欎釜鍔熻兘鍗翠笉鑳芥互鐢ㄣ傚洜涓哄畠浼氬鑷翠唬鐮佺殑鍙鎬у彉寰楀緢宸傝冭檻涓涓嬩竴涓湭鏇炬帴瑙﹁繃java.lang.Math綾葷殑璇昏呮潵鐪嬭繖孌典唬鐮併傚綋浠栬鍒?ldquo;int min=min(3,4)”錛屼粬浼氬緢榪鋒儜錛岃繖涓猰in鍑芥暟鍒板簳鏄湪鍝噷瀹氫箟鐨勫憿錛熶簬鏄粬灝卞緱鍘誨垎鏋恑mport榪欓噷銆傝繖孌典唬鐮佽繕濂借錛屽彧鏈変竴涓被琚潤鎬佸鍏ワ紝鍙渶瑕佹墦寮 java.lang.Math鐨勬枃妗e氨鍙互鐩存帴浜嗚В榪欎簺鏂規硶鐨勭浉鍏充俊鎭簡銆備絾鏄鏋滃悓鏃墮潤鎬佸鍏ヤ簡10涓被鐨勬儏鍐典笅鍛紵濡傛灉榪欎簺綾諱腑鏈夌潃鍚嶅瓧綾諱技錛堢浉鍚岋級浣嗘槸琛屼負鍗翠笉涓鑷寸殑鏂規硶鐨勬椂鍊欏憿錛熸瘮濡侾erson.eat(Food food)鍜?Animal.eat(Food food)銆?/p>
閭d箞榪欎釜鍔熻兘璇ュ浣曠敤鍛紵鎴戣涓?strong>涓浜涘父鐢ㄧ殑宸ュ叿綾匯佸叏灞鍙橀噺綾葷瓑褰撻渶瑕佸湪涓涓被涓嬈′嬌鐢ㄧ殑鏃跺欏彲浠ュ榪涙潵錛屼絾鏄?font color="#ff0000">瀵逛簬緋葷粺涓殑妯″瀷綾?/strike>鎴栬呮槸鐢ㄧ殑嬈℃暟涓嶅鐨勫伐鍏風被榪樻槸涓嶈瀵煎叆鐨勫ソ銆傛垜浠渶瑕佸湪鑷繁鍐欎唬鐮佹椂鐨勬柟渚垮拰浠g爜鏈韓鐨勫彲璇繪ч棿鍋氫釜鏉冭 銆?/p>