15章泛型一

-----------------------------------------------------简单泛型--------------------------------------------

public class Holder3<T> {
  private  T a;
  public Holder3(T a) { this.a = a; }
  public void set(T a) { this.a = a; }
  public T get() { return a; }
  public static void main(String[] args) {
    Holder3<Automobile> h3 =
      new Holder3<Automobile>(new Automobile());
    Automobile a = h3.get(); // No cast needed
    // h3.set("Not an Automobile"); // Error
    // h3.set(1); // Error
  }
} ///:~
class Automobile {}

-----------------------------------------元组----------------------------------------------------------


四维元组:

class FourTuple<A,B,C,D>{
 public final A first;
 public final B second;
 public final C third;
 public final D fourth;
 public FourTuple(A a, B b, C c, D d) {
   this.first = a;
   this.second = b;
   this.third = c;
   this.fourth = d;
 }
 public String toString() {
   return "(" + first + ", " + second + ", " +
     third + ", " + fourth + ")";
 }
}

测试类:

public class TupleTest {


  static
  FourTuple<Vehicle,Amphibian,String,Integer> h() {
    return
      new FourTuple<Vehicle,Amphibian,String,Integer>(
        new Vehicle(), new Amphibian(), "hi", 47);
  }
 
  public static void main(String[] args) {
 FourTuple<Vehicle,Amphibian,String,Integer> fourTuple = h();
 //fourTuple.third = "sdkfksdfj";// Compile error: final
    System.out.println(fourTuple);
  
  }

--------------------------------------------------一个堆栈类----------------------------------------------

public class LinkedStack<T> {
  private static class Node<U> {
    U item;
    Node<U> next;
    Node() { item = null; next = null; }
    Node(U item, Node<U> next) {
      this.item = item;
      this.next = next;
    }
    boolean end() { return item == null && next == null; }
  }
  private Node<T> top = new Node<T>(); // End sentinel


  public void push(T item) {
    top = new Node<T>(item, top);
  } 


  public T pop() {
    T result = top.item;
    if(!top.end())
      top = top.next;
    return result;
  }


  public static void main(String[] args) {
    LinkedStack<String> lss = new LinkedStack<String>();
    for(String s : "Phasers on stun!".split(" "))
      lss.push(s);
    String s;
    while((s = lss.pop()) != null)
      System.out.println(s);
  }
}

-------------------------------------------------------随机取元素工具类-----------------------------------

import java.util.ArrayList;
import java.util.Random;


public class RandomList<T> {
ArrayList<T> list = new ArrayList<T>();
public void add(T item){list.add(item);}
Random random = new Random(47);
public T select(){
return list.get(random.nextInt(list.size()));
}
public static void main(String[] args) {
RandomList<String> list = new RandomList<String>();
String[] ss = "2,3,4,5,6".split(",");
for(int i=0;i<ss.length;i++){
list.add(ss[i]);
}
for(int i=0;i<10;i++){
System.out.println(list.select());
}

}
}

-------------------------------------------------------15.3泛型接口(生成器)--------------------------


1、参数化的 Generator接口

     public interface Generator<T> { T next(); } 

2、父类以及子类们

public class Coffee {
  private static long counter = 0;
  private final long id = counter++;
  public String toString() {
    return getClass().getSimpleName() + " " + id;
  }



class Americano extends Coffee {}
class Breve extends Coffee {}
class Cappuccino extends Coffee {} 
class Latte extends Coffee {}
class Mocha extends Coffee {} 

3、生成器类:

import java.util.*;


import net.mindview.util.*;


public class CoffeeGenerator
implements Generator<Coffee>, Iterable<Coffee> {
  private Class[] types = { Latte.class, Mocha.class,
    Cappuccino.class, Americano.class, Breve.class };
  private static Random rand = new Random(47);
  public CoffeeGenerator() {}
  private int size = 0;
//这个是给iterator方法用的
  public CoffeeGenerator(int sz) { size = sz; }
//这个构造器也是给iterator方法用的
  public Coffee next() {
    try {
    Coffee coffee =  (Coffee)
         types[rand.nextInt(types.length)].newInstance();
    System.out.println("我进来了哦哦哦哦哦");
      return coffee;
     
      // Report programmer errors at run time:
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  class CoffeeIterator implements Iterator<Coffee> {
    int count = size;
    public boolean hasNext() { return count > 0; }
    public Coffee next() {
      count--;
      return CoffeeGenerator.this.next();
    }
    public void remove() { // Not implemented
      throw new UnsupportedOperationException();
    }
  };
  public Iterator<Coffee> iterator() {
    return new CoffeeIterator();
  }
  public static void main(String[] args) {
    CoffeeGenerator gen = new CoffeeGenerator();
    for(int i = 0; i < 5; i++)
      System.out.println(gen.next());//这里只是单纯的调用CoffeeGenerator的next()方法,并没有涉及到iterator方法
    for(Coffee c : new CoffeeGenerator(9))

//这里的循环自动调用的iterator方法,自动通过iterator方法进行遍历操作(手先增强for循环和iterator遍历的效果是一样的,也

//就说 增强for循环的内部也就是调用iteratoer实现的,但是增强for循环 有些缺点,例如不能在增强循环里动态的删除集合内容。不能获取下标等。
      System.out.println(c);
  }
 


  
}


以下是我自己模仿list实现的iterator:

import java.util.ArrayList;
import java.util.Iterator;


public class MySelfList<T> {
ArrayList<T> list = new ArrayList<T>();
int size;
public void add(T item){
list.add(item);
size++;
}
public  Iterator<T> iterator(){
return new  MyIterator();
}

private class MyIterator implements Iterator<T>{
private int count;
private int listSize = size;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return count<size;
}


@Override
public T next() {
// TODO Auto-generated method stub

T value = list.get(count);
count++;
return value;
}


@Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}

}
public static void main(String[] args) {
MySelfList<String> list = new MySelfList<String>();
String[] ss = "1,2,3,4,5,6,7,8,9".split(",");
for(int i=0;i<ss.length;i++){
list.add(ss[i]);
}
Iterator itertor = list.iterator();
while(itertor.hasNext()){
System.out.println(itertor.next());
}
}
}

-----------------------------------------------------------15.4泛型方法-------------------------------------

1、对于一个static方法来说,无法访问泛型类的类型参数,无论是否是泛型方法,比如:

class Animals<T>{
T s;
public static<U> void test(){
//T ss;
U t;
}
public static void test2(){
//T ss;
}

}

这两个方法里面都不允许引用T
所以static方法要使用泛型,必须使其成为泛型方法。


2、成为泛型方法很简单:只需将泛型参数列表置于返回值前,像
 public <T> void f(T x) {
    System.out.println(x.getClass().getName());
  }
这样就可以了哦



3、在泛型方法中利用类型参数推断:这里只是赋值的时候能进行自动的类型参数推断,传递参数的话,还是变成object对象,如下:
import java.util.*;


public class New {
  public static <K,V> Map<K,V> map() {
    return new HashMap<K,V>();
  }
  public static <T> List<T> list() {
    return new ArrayList<T>();
  }


  public static void main(String[] args) {
    Map<String, List<String>> sls = New.map();
    List<String> ls = New.list();


  }
}
public class LimitsOfInference {
  static void f(Map<Person, List<? extends Pet>> petPeople) {
 
  }
  public static void main(String[] args) {
 
  // f(New.map()); // Does not compile//这里是object
  }

class Person {}
class Pet{}
class Dog extends Pet{}

3.1、如果是传参的话,只能用显示的类型说明:显示的类型用法:(如果是在定义该方法的内部,在点操作符用this关键字,如果是static方法就在点操作符之前加上类名)

public class ExplicitTypeSpecification {
  static void f(Map<Person, List<Pet>> petPeople) {}
  public static void main(String[] args) {
    f(New.<Person, List<Pet>>map());
//这里正常自动转型
  }
}

4、利用生成器很方便的填充一个collection容器,这在实际中很用用

01、public interface Generator<T> { T next(); } 

02、import java.util.*;
public class CoffeeGenerator
implements Generator<Coffee>{
  private Class[] types = { Latte.class, Mocha.class,
    Cappuccino.class, Americano.class, Breve.class };
  private static Random rand = new Random(47);
  public CoffeeGenerator() {}
  public Coffee next() {
    try {
    Coffee coffee =  
         (Coffee) types[rand.nextInt(types.length)].newInstance();
      return coffee;
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
}

03、public class Coffee {
  private static long counter = 0;
  private final long id = counter++;
  public String toString() {
    return getClass().getSimpleName() + " " + id;
  }

class Americano extends Coffee {}
class Breve extends Coffee {}
class Cappuccino extends Coffee {} 
class Latte extends Coffee {}
class Mocha extends Coffee {} 

04、import java.util.*;
public class Generators {
  public static <T> Collection<T> fill(Collection<T> coll, Generator<T> gen, int n) {
    for(int i = 0; i < n; i++)
      coll.add(gen.next());
    return coll;
  }
//这个就是填充容器的泛型方法
  public static void main(String[] args) {
    Collection<Coffee> coffee = fill(new ArrayList<Coffee>(), new CoffeeGenerator(), 4);
    for(Coffee c : coffee){
      System.out.println(c);
    }
  }
}

5、一个通用的生成器


01、public interface Generator<T> { T next(); } 

02、public class BasicGenerator<T> implements Generator<T> {
  private Class<T> type;
  public BasicGenerator(Class<T> type){ this.type = type; }
  public T next() {
    try {
      // Assumes type is a public class:
      return type.newInstance();
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  // Produce a Default generator given a type token:
  public static <T> Generator<T> create(Class<T> type) {
    return new BasicGenerator<T>(type);
  }
}

以下测试的:

03、public class CountedObject {
  private static long counter = 0;
  private final long id = counter++;
  public long id() { return id; }
  public String toString() { return "CountedObject " + id;}
}

04、public class BasicGeneratorDemo {
  public static void main(String[] args) {
    Generator<CountedObject> gen =
      BasicGenerator.create(CountedObject.class);
    for(int i = 0; i < 5; i++)
      System.out.println(gen.next());
  }
} /* Output:
CountedObject 0
CountedObject 1
CountedObject 2
CountedObject 3
CountedObject 4
*///:~

6、一个set的实用工具,可以进行像数学里多的集合操作运算:

import java.util.*;


public class Sets {
  public static <T> Set<T> union(Set<T> a, Set<T> b) {
    Set<T> result = new HashSet<T>(a);
    result.addAll(b);
    return result;
  }
//这个是取两个set的合集,因为set的本身不能重复,所以不会达到union all 的效果,数学上称并集
  public static <T>
  Set<T> intersection(Set<T> a, Set<T> b) {
    Set<T> result = new HashSet<T>(a);
    result.retainAll(b);
    return result;
  }//这个是取两个set的交集,数学上称交集
  // Subtract subset from superset:
  public static <T> Set<T>
  difference(Set<T> superset, Set<T> subset) {
    Set<T> result = new HashSet<T>(superset);
    result.removeAll(subset);
    return result;
  }//这个是取superset中有的,但是subset中没有的,简单的说就是移除subset包含的元素
  // Reflexive--everything not in the intersection:
  public static <T> Set<T> complement(Set<T> a, Set<T> b) {
    return difference(union(a, b), intersection(a, b));
  }//这两个是取两个元素的全部,除了交集部分
}

01、实际应用类:

枚举类:

public enum Watercolors {
 ONE,TWO,THREE,FOUR,FIVE
}

应用类:

import java.util.EnumSet;
import java.util.Set;

public class WatercolorSets {
  public static void main(String[] args) {
    Set<Watercolors> set1 =
      EnumSet.range(Watercolors.ONE, Watercolors.THREE);
//从一到三的枚举,生成一个set
    Set<Watercolors> set2 =
      EnumSet.range(Watercolors.TWO, Watercolors.FIVE);
    System.out.println("set1: " + set1);
    System.out.println("set2: " + set2);
    System.out.println("union(set1, set2): " + Sets.union(set1, set2));
    Set<Watercolors> subset = Sets.intersection(set1, set2);
    System.out.println("intersection(set1, set2): " + subset);
    System.out.println("difference(set1, subset): " +
    Sets.difference(set1, subset));
    System.out.println("difference(set2, subset): " +
    Sets.difference(set2, subset));
    System.out.println("complement(set1, set2): " +
    Sets.complement(set1, set2));
  }
}

输出的结果:

set1: [ONE, TWO, THREE]
set2: [TWO, THREE, FOUR, FIVE]
union(set1, set2): [FOUR, TWO, THREE, ONE, FIVE]
intersection(set1, set2): [TWO, THREE]
difference(set1, subset): [ONE]
difference(set2, subset): [FOUR, FIVE]
complement(set1, set2): [FOUR, ONE, FIVE]

02、实际应用类:使用set工具类打印出java.util包中的各种Collection类和各种Map类直接的差异,非常的直观:

import java.lang.reflect.*;
import java.util.*;

public class ContainerMethodDifferences {
  static Set<String> methodSet(Class<?> type) {
    Set<String> result = new TreeSet<String>();
    for(Method m : type.getMethods())
      result.add(m.getName());
    return result;
  }
  static void interfaces(Class<?> type) {
    System.out.print("Interfaces in " +
      type.getSimpleName() + ": ");
    List<String> result = new ArrayList<String>();
    for(Class<?> c : type.getInterfaces())
      result.add(c.getSimpleName());
    System.out.println(result);
  }
  static Set<String> object = methodSet(Object.class);
  static { object.add("clone"); }
  static void
  difference(Class<?> superset, Class<?> subset) {
    System.out.print(superset.getSimpleName() +
      " extends " + subset.getSimpleName() + ", adds: ");
    Set<String> comp = Sets.difference(
      methodSet(superset), methodSet(subset));
    comp.removeAll(object); // Don't show 'Object' methods
    System.out.println(comp);
    interfaces(superset);
  }
  public static void main(String[] args) {
    System.out.println("Collection: " +
      methodSet(Collection.class));
    interfaces(Collection.class);
    difference(Set.class, Collection.class);
    difference(HashSet.class, Set.class);
    difference(LinkedHashSet.class, HashSet.class);
    difference(TreeSet.class, Set.class);
    difference(List.class, Collection.class);
    difference(ArrayList.class, List.class);
    difference(LinkedList.class, List.class);
    difference(Queue.class, Collection.class);
    difference(PriorityQueue.class, Queue.class);
    System.out.println("Map: " + methodSet(Map.class));
    difference(HashMap.class, Map.class);
    difference(LinkedHashMap.class, HashMap.class);
    difference(SortedMap.class, Map.class);
    difference(TreeMap.class, Map.class);
  }

-----------------------------------------------------------15.5匿名内部类(泛型)-----------------------

这是一个排队问题的一种实现,包含的是泛型实现的匿名内部类知识

import java.util.*;
import net.mindview.util.*;


class Customer {
//顾客
  private static long counter = 1;
  private final long id = counter++;
  private Customer() {}
  public String toString() { return "Customer " + id; }
  // A method to produce Generator objects:
  public static Generator<Customer> generator() {
    return new Generator<Customer>() {
      public Customer next() { return new Customer(); }
    };
  }
}


class Teller {//出纳员
  private static long counter = 1;
  private final long id = counter++;
  private Teller() {}
  public String toString() { return "Teller " + id; }
  // A single Generator object:
  public static Generator<Teller> generator =
    new Generator<Teller>() {
      public Teller next() { return new Teller(); }
    };
}


public class BankTeller {//银行柜员
  public static void serve(Teller t, Customer c) {
    System.out.println(t + " serves " + c);
  }
  public static void main(String[] args) {
    Random rand = new Random(47);
    Queue<Customer> line = new LinkedList<Customer>();
    Generators.fill(line, Customer.generator(), 15);
    List<Teller> tellers = new ArrayList<Teller>();
    Generators.fill(tellers, Teller.generator, 4);
    for(Customer c : line)
      serve(tellers.get(rand.nextInt(tellers.size())), c);
  }
}

-----------------------------------------------------------15.5构建复杂模型(泛型)--------------------

这个事例展示了使用泛型类型来构建复杂模型,是多么的简单。即使每个类都是作为一个构建块创建的,但是其整个还是包含许多部分,这里构建的模型是一个零售店,它包含走廊、货架、商品

import java.util.*;
import net.mindview.util.*;


class Product {
//产品
  private final int id;
  private String description;
  private double price;
  public Product(int IDnumber, String descr, double price){
    id = IDnumber;
    description = descr;
    this.price = price;
    System.out.println(toString());
  }
  public String toString() {
    return id + ": " + description + ", price: $" + price;
  }
  public void priceChange(double change) {
    price += change;
  }
  public static Generator<Product> generator =
    new Generator<Product>() {
      private Random rand = new Random(47);
      public Product next() {
        return new Product(rand.nextInt(1000), "Test",
          Math.round(rand.nextDouble() * 1000.0) + 0.99);
      }
    };
}


class Shelf extends ArrayList<Product> {// 英 [ʃelf]   美 [ʃɛlf] n. 架子;搁板
  public Shelf(int nProducts) {
    Generators.fill(this, Product.generator, nProducts);
  }
}


class Aisle extends ArrayList<Shelf> {//英 [aɪl]   美 [aɪl]n. 通道,走道
  public Aisle(int nShelves, int nProducts) {
    for(int i = 0; i < nShelves; i++)
      add(new Shelf(nProducts));
  }
}


class CheckoutStand {}//checkout stand 结账台,收款处
class Office {}//办公室


public class Store extends ArrayList<Aisle> {// 英 [stɔː]   美 [stɔr] n.商店  
  private ArrayList<CheckoutStand> checkouts =
    new ArrayList<CheckoutStand>();
  private Office office = new Office();
  public Store(int nAisles, int nShelves, int nProducts) {
    for(int i = 0; i < nAisles; i++)
      add(new Aisle(nShelves, nProducts));
  }
  public String toString() {
    StringBuilder result = new StringBuilder();
    for(Aisle a : this)
      for(Shelf s : a)
        for(Product p : s) {
          result.append(p);
          result.append("\n");
        }
    return result.toString();
  }
  public static void main(String[] args) {
    System.out.println(new Store(14, 5, 10));
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值