d1:泛型(Generics)
可以接受不同的类型。
按照不同类型生成相应的对象
package com.hyh.jdk;
public class Generic<T> {
private T foo;
public T getFoo() {
return foo;
}
public void setFoo(T foo) {
this.foo = foo;
}
public static void main(String[] args){
Generic<Boolean> foo1= new Generic<Boolean>();
Generic<Integer> foo2= new Generic<Integer>();
foo1.setFoo(new Boolean(true));
Boolean b = foo1.getFoo();
foo2.setFoo(new Integer(10));
Integer i = foo2.getFoo();
//不同类型间不能赋值
foo1 = foo2;
System.out.println(foo1);
}
}
2:泛型继承自某个接口
package com.hyh.jdk;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListGenericFoo<T extends List<String>> {
private T[] fooArray;
public T[] getFooArray() {
return fooArray;
}
public void setFooArray(T[] fooArray) {
this.fooArray = fooArray;
}
public static void main(String[] args) {
ListGenericFoo<LinkedList<String>> foo1 = new
ListGenericFoo<LinkedList<String>>();
ListGenericFoo<ArrayList<String>> foo2 = new
ListGenericFoo<ArrayList<String>>();
}
}
3:用map统计参数的个数
package com.hyh.jdk;
import java.util.Map;
import java.util.TreeMap;
public class TongJi {
public static void main(String[] args) {
// ab ab cd cd c d a b ad
Map<String,Integer> map = new
TreeMap<String,Integer>();
for (String word : args) {
Integer in = map.get(word); //得到对应键的值
map.put(word, in == null ? 1 : in+1);
}
System.out.println(map);
}
}
4:自动类型转化,int在-127--128用基本类型比较,超出范围则比较其包装
类
尽量避免容易混淆。
package com.hyh.jdk;
import java.util.Map;
import java.util.TreeMap;
public class TongJi {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
if (i1 == i2) {
System.out.println("====");
} else {
System.out.println("!===");
}
Integer a = 200;
Integer b = 200;
if (a == b) {
System.out.println("====");
} else {
System.out.println("!===");
}
}
}
可以接受不同的类型。
按照不同类型生成相应的对象
package com.hyh.jdk;
public class Generic<T> {
private T foo;
public T getFoo() {
return foo;
}
public void setFoo(T foo) {
this.foo = foo;
}
public static void main(String[] args){
Generic<Boolean> foo1= new Generic<Boolean>();
Generic<Integer> foo2= new Generic<Integer>();
foo1.setFoo(new Boolean(true));
Boolean b = foo1.getFoo();
foo2.setFoo(new Integer(10));
Integer i = foo2.getFoo();
//不同类型间不能赋值
foo1 = foo2;
System.out.println(foo1);
}
}
2:泛型继承自某个接口
package com.hyh.jdk;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListGenericFoo<T extends List<String>> {
private T[] fooArray;
public T[] getFooArray() {
return fooArray;
}
public void setFooArray(T[] fooArray) {
this.fooArray = fooArray;
}
public static void main(String[] args) {
ListGenericFoo<LinkedList<String>> foo1 = new
ListGenericFoo<LinkedList<String>>();
ListGenericFoo<ArrayList<String>> foo2 = new
ListGenericFoo<ArrayList<String>>();
}
}
3:用map统计参数的个数
package com.hyh.jdk;
import java.util.Map;
import java.util.TreeMap;
public class TongJi {
public static void main(String[] args) {
// ab ab cd cd c d a b ad
Map<String,Integer> map = new
TreeMap<String,Integer>();
for (String word : args) {
Integer in = map.get(word); //得到对应键的值
map.put(word, in == null ? 1 : in+1);
}
System.out.println(map);
}
}
4:自动类型转化,int在-127--128用基本类型比较,超出范围则比较其包装
类
尽量避免容易混淆。
package com.hyh.jdk;
import java.util.Map;
import java.util.TreeMap;
public class TongJi {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
if (i1 == i2) {
System.out.println("====");
} else {
System.out.println("!===");
}
Integer a = 200;
Integer b = 200;
if (a == b) {
System.out.println("====");
} else {
System.out.println("!===");
}
}
}