泛型

package com.bjsxt.gen02;

import java.io.Closeable;
import java.io.IOException;

/**
 * 泛型方法 <> 返回类型前面
 * 只能访问对象的信息,不能修改信息
 * @author Administrator
 *
 */
public class TestMethod {

    /**
     * @param args
     */
    public static void main(String[] args) {
        test("a"); //T -->String
    }
    //泛型方法
    public static <T> void test(T a){        
        System.out.println(a);
    }
    // extends <=
    public static <T extends Closeable> void test(T... a){        
        for(T temp:a){
            try {
                if(null!=temp){
                    temp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
package com.bjsxt.gen02;
/**
 * 泛型类:声明时使用泛型
 * 字母:
 * T  Type 表示类型。 
    K V 分别代表键值中的Key Value。 
    E 代表Element。 
 使用时确定类型
 注意:
 1、泛型只能使用引用类型,不能基本类型
 2、泛型声明时字母不能使用 静态属性|静态方法上
 * @author Administrator
 *
 * @param <T>
 */
public class Student<T1,T2> {
    private T1 javaScore;
    private T2 oracleScore;
    
    //泛型声明时不能使用 静态属性|静态方法上
    //private static T1 test;
    
    
    public T1 getJavaScore() {
        return javaScore;
    }




    public void setJavaScore(T1 javaScore) {
        this.javaScore = javaScore;
    }




    public T2 getOracleScore() {
        return oracleScore;
    }




    public void setOracleScore(T2 oracleScore) {
        this.oracleScore = oracleScore;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //使用时指定类型(引用类型)
        Student<String,Integer> stu = new Student<String,Integer> ();
        //1、安全:类型检查
        stu.setJavaScore("优秀");
        //2、省心:类型转换
        int it =stu.getOracleScore(); //自动拆箱
    }

}
package com.bjsxt.gen02;
/**
 * 接口中 泛型字母只能使用在方法中,不能使用在全局常量中
 * @author Administrator
 *
 * @param <T>
 */
public interface Comparator<T> {
    void compare(T t);    
}
package com.bjsxt.gen03;
/**
 * 父类为泛型类
 * 1、属性
 * 2、方法
 * 
 * 要么同时擦除,要么子类大于等于父类的类型,
 * 不能子类擦除,父类泛型
 * 1、属性类型
 *  父类中,随父类而定
 *  子类中,随子类而定
 * 2、方法重写:
 *  随父类而定
 * 
 * 
 * @author Administrator
 *
 * @param <T>
 */
public abstract class Father<T,T1> {
    T name;
    public abstract void test(T t);
}
/**
 * 子类声明时指定具体类型
 * 属性类型为具体类型
 * 方法同理
 */
class Child1 extends Father<String,Integer>{
    String t2;
    @Override
    public void test(String t) {
    }    
}
/**
 * 子类为泛型类 ,类型在使用时确定
 * @author Administrator
 *
 */
class Child2<T1,T,T3> extends Father<T,T1>{
    T1 t2;

    @Override
    public void test(T t) {
        
    }
}
/**
 * 子类为泛型类,父类不指定类型 ,泛型的擦除,使用Object替换
 */
class Child3<T1,T2> extends Father{
    T1 name2;
    @Override
    public void test(Object t) {
        // TODO Auto-generated method stub        
    }
}
/**
 * 子类与父类同时擦除
 */
class Child4 extends Father{
    String name;    
    @Override
    public void test(Object t) {
        
    }
}
/**
 *错误:子类擦除,父类使用泛型
class Child5 extends Father<T,T1>{
    String name;    
    @Override
    public void test(T t) {
        
    }
*/
package com.bjsxt.gen03;
/**
 * 泛型接口:与继承同理
 * 重写方法随父类而定
 *
 * @param <T>
 */
public interface Comparable<T> {
    void compare(T t);
}
//声明子类指定具体类型
class Comp implements Comparable<Integer>{

    @Override
    public void compare(Integer t) {
        // TODO Auto-generated method stub
        
    }
    
}
//擦除
class Comp1 implements Comparable{

    @Override
    public void compare(Object t) {
        // TODO Auto-generated method stub
        
    }
     
}
//父类擦除,子类泛型
class Comp2<T> implements Comparable{

    @Override
    public void compare(Object t) {
        // TODO Auto-generated method stub
        
    }
     
}
//子类泛型>=父类泛型
class Comp3<T> implements Comparable<T>{

    @Override
    public void compare(T t) {
        // TODO Auto-generated method stub
        
    }
     
}
//父类泛型,子类擦除 错误
package com.bjsxt.gen03;
/**
*泛型的擦除
*1、继承|实现声明 不指定类型
*2、使用时 不指定类型 
*统一Object 对待 
*1、编译器警告 消除使用Object
*2、不完全等同于Object ,编译不会类型检查
 * @author Administrator
 *
 * @param <T>
 */
public class Student<T> {
    private T javaScore;
    private T oracleScore;
    
    //泛型声明时不能使用 静态属性|静态方法上
    //private static T1 test;
    
    
    public T getJavaScore() {
        return javaScore;
    }




    public void setJavaScore(T javaScore) {
        this.javaScore = javaScore;
    }




    public T getOracleScore() {
        return oracleScore;
    }




    public void setOracleScore(T oracleScore) {
        this.oracleScore = oracleScore;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Student stu1 = new Student(); 
        //消除警告 使用 Object
        Student<Object> stu = new Student<Object>(); 
        //stu.setJavaScore("af"); //以Object对待
        
        test(stu1); //stu1 相当于Object 但是不完全等同Object
        //擦除,不会类型检查
        //test(stu);
        test1(stu1);
        test1(stu);
        
    }
    
    public static  void test(Student<Integer> a){
        
    }
    public static  void test1(Student<?> a){
        
    }
    }
package com.bjsxt.gen04;

public class Fruit {

}
class Apple extends Fruit{
    
}
package com.bjsxt.gen04;
/**
 * 多态的两种形式
 * @author Administrator
 *
 */
public class FruitApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Fruit f =new Apple();
        test(new Apple());
    }
    //形参使用多态
    public static void test(Fruit f){
        
    }
    //返回类型使用多态
    public static Fruit  test2(){
        return new Apple();
    }

}
package com.bjsxt.gen04;
//泛型类
public class A<T> {

}
 
package com.bjsxt.gen04;
/**
 * 泛型没有多态
 * @author Administrator
 *
 */
public class App {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //A<Fruit> f = new A<Apple>();
        A<Fruit> f =new A<Fruit>();
        //test(new A<Apple>());
    }
    //形参使用多态
    public static void test(A<Fruit> f){
        
    }
    //返回类型使用多态
    public static A<Fruit>  test2(){
        //return (A<Fruit>)(new A<Apple>());
        return null;
    }
}

如果想使用怎么办

package com.bjsxt.gen04;
/**
 * 通配符
 * ?类型不定,使用时确定类型
 * ?使用:声明类型|声明方法上,不能声明类或使用时
 * ? extends : <= 上限  指定类型 子类或自身
 * ? super :>=下限   指定类型 为自身或父类
 * @author Administrator
 *
 */
public class Student<T> {
    T score;
    
    
    
    
    public static void main(String[] args) {
        //只能在声明的时候使用
        Student<?> stu = new Student<String>();
        test(new Student<Integer>());
        
        test2(new Student<Apple>());
        //test3(new Student<Apple>()); //泛型没有多态        
        
        //test4(new Student<Apple>()); //<
         stu  = new Student<Fruit>();;
        //test4(stu); //使用时确定类型
        test4(new Student<Object>());
        test4(new Student<Fruit>());
        
    }
    
    public static void test(Student<?> stu){
        
    }
    public static void test3(Student<Fruit> stu){
        
    }
    // <=
    public static void test2(Student<? extends Fruit> stu){
        
    }
    //>=
    public static void test4(Student<? super Fruit> stu){
        
    }
    
}

 

 

package com.bjsxt.gen04;

public class Bjsxt <T>{
 T stu ;
 
 
 public static void main(String[] args) {
     //泛型的嵌套 
     Bjsxt<Student<String>> room =new  Bjsxt<Student<String>>();
     //从外到内拆分
     room.stu = new Student<String>();
     Student<String> stu = room.stu;
     String score =stu.score;
     System.out.println(score);
}
}

 

 

package com.bjsxt.gen04;
/**
 * 没有泛型数组
 * 声明可以使用,但是创建失败
 * @author Administrator
 *
 */
public class Array {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Integer[]  arr = new Integer[4]; 
        //Student<String>[] arr2 = new Student<String>[10];
        Student<?>[] arr2 = new Student[10];
        
        MyArrayList<String> strList =new MyArrayList<String>();
        strList.add(0, "a");
        String elem =strList.getElem(0);
        System.out.println(elem);
        
        
    
        
    }

}
class MyArrayList<E>{
    //E[] cap =new E[10]; 没有泛型数组
    Object[] cap = new Object[10];
    
    public void add(int idx,E e){
        cap[idx] =e;
    }
    
    
    @SuppressWarnings("unchecked")
    public E[] getAll(){
        return (E[]) cap;
    }
    @SuppressWarnings("unchecked")
    public E getElem(int idx){
        return (E) cap[idx];
    }
}

 

 

 

package com.bjsxt.gen04;

import java.util.ArrayList;
import java.util.List;

/**
 * 1.7中使用泛型,声明一次类型即可
 * 在使用|创建时不用指定类型
 * @author Administrator
 *
 */
public class Test7 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String>  arrList= new ArrayList<String>();
        //List<String>  arrList2= new ArrayList<>();
    }

}
posted @ 2014-10-01 10:55  无名の辈  阅读(284)  评论(0编辑  收藏  举报