|
| 1 | +package com.practise2; |
| 2 | + |
| 3 | +//--------------->如何设置支持泛型的类 |
| 4 | +//-------------->01.不支持泛型的Stack |
| 5 | +/* |
| 6 | +以Stack栈为例子,如果不使用泛型 |
| 7 | +当需要一个只能放Hero的栈的时候,就需要设计一个HeroStack |
| 8 | +当需要一个只能放Item的栈的时候,就需要一个ItemStack |
| 9 | +* */ |
| 10 | +//HeroStack |
| 11 | +public class p10_fanxing2 { |
| 12 | + |
| 13 | +} |
| 14 | +/*实例代码1 |
| 15 | +package generic; |
| 16 | +import java.util.LinkedList; |
| 17 | +import charactor.Hero; |
| 18 | +public class HeroStack { |
| 19 | + LinkedList<Hero> heros = new LinkedList<Hero>(); |
| 20 | + public void push(Hero h) { |
| 21 | + heros.addLast(h); |
| 22 | + } |
| 23 | + public Hero pull() { |
| 24 | + return heros.removeLast(); |
| 25 | + } |
| 26 | + public Hero peek() { |
| 27 | + return heros.getLast(); |
| 28 | + } |
| 29 | + public static void main(String[] args) { |
| 30 | + HeroStack heroStack = new HeroStack(); |
| 31 | + for (int i = 0; i < 5; i++) { |
| 32 | + Hero h = new Hero("hero name " + i); |
| 33 | + System.out.println("压入 hero:" + h); |
| 34 | + heroStack.push(h); |
| 35 | + } |
| 36 | + for (int i = 0; i < 5; i++) { |
| 37 | + Hero h =heroStack.pull(); |
| 38 | + System.out.println("弹出 hero" + h); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +* */ |
| 43 | + |
| 44 | +/*实例代码2 |
| 45 | +package generic; |
| 46 | +import java.util.LinkedList; |
| 47 | +import property.Item; |
| 48 | +public class ItemStack { |
| 49 | + LinkedList<Item> Items = new LinkedList<Item>(); |
| 50 | + public void push(Item h) { |
| 51 | + Items.addLast(h); |
| 52 | + } |
| 53 | + public Item pull() { |
| 54 | + return Items.removeLast(); |
| 55 | + } |
| 56 | + public Item peek() { |
| 57 | + return Items.getLast(); |
| 58 | + } |
| 59 | + public static void main(String[] args) { |
| 60 | + ItemStack ItemStack = new ItemStack(); |
| 61 | + for (int i = 0; i < 5; i++) { |
| 62 | + Item item = new Item("Item name " + i); |
| 63 | + System.out.println("压入 Item:" + item); |
| 64 | + ItemStack.push(item); |
| 65 | + } |
| 66 | + for (int i = 0; i < 5; i++) { |
| 67 | + Item item =ItemStack.pull(); |
| 68 | + System.out.println("弹出 Item" + item); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +* */ |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +//----------------->02.支持泛型的Stack |
| 77 | +/* |
| 78 | +设计一个支持泛型的栈MyStack |
| 79 | +设计这个类的时候,在类的声明上,加上一个<T>,表示该类支持泛型。 |
| 80 | +T是type的缩写,也可以使用任何其他的合法的变量,比如A,B,X都可以,但是一般约定成俗使用T,代表类型。 |
| 81 | +* */ |
| 82 | +/* |
| 83 | +package generic; |
| 84 | +
|
| 85 | +import java.util.HashMap; |
| 86 | +import java.util.LinkedList; |
| 87 | +import charactor.Hero; |
| 88 | +import property.Item; |
| 89 | +
|
| 90 | +public class MyStack<T> { |
| 91 | + LinkedList<T> values = new LinkedList<T>(); |
| 92 | + public void push(T t) { |
| 93 | + values.addLast(t); |
| 94 | + } |
| 95 | + public T pull() { |
| 96 | + return values.removeLast(); |
| 97 | + } |
| 98 | + public T peek() { |
| 99 | + return values.getLast(); |
| 100 | + } |
| 101 | + public static void main(String[] args) { |
| 102 | + //在声明这个Stack的时候,使用泛型<Hero>就表示该Stack只能放Hero |
| 103 | + MyStack<Hero> heroStack = new MyStack<>(); |
| 104 | + heroStack.push(new Hero()); |
| 105 | + //不能放Item |
| 106 | + heroStack.push(new Item()); |
| 107 | +
|
| 108 | + //在声明这个Stack的时候,使用泛型<Item>就表示该Stack只能放Item |
| 109 | + MyStack<Item> itemStack = new MyStack<>(); |
| 110 | + itemStack.push(new Item()); |
| 111 | + //不能放Hero |
| 112 | + itemStack.push(new Hero()); |
| 113 | + } |
| 114 | +} |
| 115 | +* */ |
| 116 | + |
0 commit comments