ҳ > 򿪷 > > Java >

javaѧϰ֮· ֮ ͼϰ

2016-08-22

1 Ԫش洢İȫ2 ȡԪʱҪǿת ͣJDK1 5¼ģ͵İȫ⣬ҪԭʱͨһʶʾijԵͻijķֵ͡ʵ

ΪʲôҪз(Generic)?
1. Ԫش洢İȫ
2. ȡԪʱҪǿת
ͣJDK1.5¼ģ͵İȫ⣬ҪԭʱͨһʶʾijԵͻijķֵ͡ʵ
ʱֻҪָҪľͼɡ
JavaͿԱ֤ڱʱûз棬ʱͲClassCastException쳣ͬʱӼࡢ׳

ʹ÷ͣ
͵
interface List class TestGenУT,K,VֵDZʾ͡ʹĸԡTʾTypeд
͵ʵ
һҪָͲֵͣ磺
List strList = new ArrayList();
Iterator iterator = customers.iterator();
Tֻ࣬û䡣

͵ļҪʹã
1.ڼʹ÷
2.Զ巺
3.ͷ
4.ͽӿ

ڷࣩࣨ

1.ʵʱָͣĬΪObject
2.Ͳͬò໥ֵ
3.뼯еĶͱָķһ¡
4.̬вʹķ͡
5.һӿڻ࣬򲻿ɴĶ
6.catchʹ÷
7.ӷ࣬廯
һеΪһضͣgenericsĺ˼롣

Զ巺ࣺ

class Person{ //ʹTͶ
private T info; //ʹTͶһ㷽
public T getInfo(){
return info;
}
public void setInfo(T info){
this.info = info;
}
//ʹTͶ幹
public Person(){}
public Person(T info){
this.info = info;
}
//staticķв
//public static void show(T t){
//}
//try-catchʹ÷Ͷ
//try{}
//catch(T t){}
}

ڷͷ

ҲԱͻܴʱеDzǷͻġڷͷпԶ巺Ͳʱ;Ǵݵ͡
ͷĸʽ[Ȩ] <> ([ͱʶ ]) ׳쳣
public class DAO {
public E get(int id, E e){
E result = null;
return result;
}
}

ͺͼ̳еĹϵ

BAһͣӽӿڣGǾзӿڣGGͣ
磺StringObject࣬ListList

ͨ

1.ʹͨ //ֻ
磺ListMap
ListListListȸַListĸࡣ
2.ȡListĶlistеԪʱԶǰȫģΪlistʵʲôĶObject
3.дlistеԪʱСΪDz֪cԪͣDzӶ
Ψһnull͵ijԱ


Ԫؼ뵽вͰȫģ
Collectionc = new ArrayList();
c.add(new Object()); // ʱ
ΪDz֪cԪͣDzӶ
addͲEΪϵԪ͡Ǵaddκβһδ֪͵ࡣΪDz֪ʲôͣ޷κζȥ
Ψһnull͵ijԱ
һ棬ǿԵget()ʹ䷵ֵֵһδ֪֪ͣһObject

Ƶͨ


з͵õ


(С , Number]ֻΪNumberNumberõ

[Number , )ֻΪNumberNumberõ

ֻΪʵComparableӿڵʵõ

Ӧã

ûʱʹĹϵ磬һпԶһϢԣһ˿иָϢϵʽϢȣ

ϢԵ;ͿͨͽȻֻҪӦϢ༴ɡ

ϰ

package com.atguigu.javae.generic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.junit.Test;

/**
 * Զ巺, 
 * ͺͶ, ͵Ϣջڴʱȷ!
 */
class Person { // Xij, Ȼ,,Ϊֵ...
// ڴʱδָXľ, ;Object
	//private static int X x;
	
	private X info;
	
	public Person() {
	}
	
	public Person(X info) {
		this.info = info;
	}
	
	public void setInfo(X info) {
		try {
			this.info = info;
		//} catch (X e) { // catch ֻܲ׽Throwable ,ʹͲһX
		} catch (Throwable e) { // catch ֻܲ׽Throwable 
			e.printStackTrace();
		}
	}
	
	public X getInfo() {
		return info;
	}
	
	@Override
	public String toString() {
		return "ij, Ϣ[" + info + "]";
	}
	
	/*
	public static void test(X t) { // ̬вʹúͶصķ
		System.out.println(t);
	}*/
	
	// ͷ, ڷֵǰ<>, һҪβдϢ
	public static  Y testY(Y y) { // ͷеķеһ, еķ
		return null;
	}
	
	
}

class Student extends Person { // Student,infoԾǹ̶String
}

public class GenericTest {
	
	// дһ(Collection coll)
	// ڷзеСֵ
	// дһԷ, List,һЩַ, ٴSet, һЩchar, ֱʹϵϷ,ӡ
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Comparable getMin(Collection coll) {
		if (coll.size() == 0) {
			return null;
		}
		Iterator iterator = coll.iterator();
		Comparable min = iterator.next();
		while (iterator.hasNext()) {
			Comparable comp = iterator.next();
			if (comp.compareTo(min) < 0) {
				min = comp;
			}
		}
		return min;
	}
	
	@Test
	public void test7() {
		List list = new LinkedList();
		list.add("xxx");
		list.add("bbb");
		list.add("aaa");
		list.add("ddd");
		System.out.println(getMin(list));
		
		Collection set = new HashSet();
		set.add(&#39;&#39;);
		set.add(&#39;&#39;);
		set.add(&#39;a&#39;);
		set.add(&#39; &#39;);
		System.out.println(getMin(set));
	}
	
	public double sumList(List list) {
		// listͿָNumberΪ͵ļ, òԪ, ǿԻȡԪ,Number
		Iterator iterator = list.iterator();
		double sum = 0;
		while (iterator.hasNext()) {
			Number number = iterator.next();
			sum += number.doubleValue();
		}
		return sum;
	}
	
	public double getMax(Collection coll) {
		Iterator iterator = coll.iterator();
		if (!iterator.hasNext()) {
			throw new RuntimeException("Ϊ,ûֵ");
		}
		Number maxNumber = iterator.next();
		while (iterator.hasNext()) {
			Number tmpNumber = iterator.next();
			if (tmpNumber.doubleValue() > maxNumber.doubleValue()) {
				maxNumber = tmpNumber;
			}
		}
		return maxNumber.doubleValue();
	}
	
	// дһ(Collection coll)
	// ڷзеֵ
	// дһԷ, List,һЩ, ٴSet, һЩfloat, ֱʹϵϷ,ӡ
	

	@Test
	public void test6() {
		ArrayList list1 = new ArrayList();
		list1.add(100);
		list1.add(20);
		list1.add(30);
		list1.add(1000);
		list1.add(7);
		
		System.out.println(getMax(list1));
		
		Set set = new HashSet();
		set.add(234.23f);
		set.add(1234.23f);
		set.add(3234.23f);
		set.add(2334.23f);
		set.add(234.23f);
		
		System.out.println(getMax(set));
	}
	@Test
	public void test5() {
		ArrayList list1 = new ArrayList();
		list1.add(100);
		list1.add(20);
		list1.add(30);
		list1.add(10);
		list1.add(7);
		
		List list2 = new Vector();
		list2.add(3.1);
		list2.add(2.1);
		list2.add(4.1);
		list2.add(5.1);
		list2.add(1.1);
		
		double sum = sumList(list1);
		System.out.println(sum);
		
		sum = sumList(list2);
		System.out.println(sum);
		/*
		List list2 = list; // list2ͿָNumberΪ͵ļ, òԪ, ǿԻȡԪ,Number
		Iterator iterator = list2.iterator();
		while (iterator.hasNext()) {
			Number number = iterator.next();
			sum += number.intValue();
		}
		System.out.println(sum);
		
		list2 = list3;
		*/
	}
	@Test
	public void test4() {
		ArrayList list = new ArrayList();
		list.add(100);
		list.add(20);
		list.add(30);
		list.add(10);
		list.add(7);
		
		List list2 = list;// ֻ
		Object object = list2.get(1);
		System.out.println(object);
	}
	
	@SuppressWarnings("unused")
	@Test
	public void test3() {
		// ʹ?ͨ ֻͨ
		Person person = new Person<>();
		//person.setInfo("abc");
		person.setInfo(null);
		Object object = person.getInfo();
	}
	
	@SuppressWarnings({ "static-access", "rawtypes", "unchecked", "unused" })
	@Test
	public void test2() {
		//Person person = new Person("");
		Person person = new Person(30);
		String string = person.testY("abc");
		System.out.println(string);
		
		ArrayList list = new ArrayList();
		list.add(100);
		list.add(200);
		list.add(50);
		list.add(10);
		
		Integer[] tmp = new Integer[0];
		//Integer[] arr = list.toArray(tmp);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Test
	public void test1() {
		Person person = new Person(true);
		Object valaueObject = person.getInfo();
		System.out.println(valaueObject);
		
		Person person2 = new Person("");
		String name = person2.getInfo();
		System.out.println(name);
		
		Person person3 = new Person(30);
		Integer integer = person3.getInfo();
		System.out.println(integer);
	}
}

ȵƼ