Skip to content

Commit 0d39b80

Browse files
committed
test
1 parent 3886aed commit 0d39b80

File tree

3 files changed

+129
-12
lines changed

3 files changed

+129
-12
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+

src/com/practise2/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ final 关键字
1515

1616
内部类
1717

18+
泛型
19+

笔记和学习方法/06_02_lambda表达式.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
> //平时的写法
2626
> @Test
2727
> public void test1() {
28-
> Runnable runnable = new Runnable() {
29-
>
28+
> Runnable runnable = new Runnable() {
3029
> @Override
3130
> public void run() {
32-
> System.out.println("线程启动了");
31+
> System.out.println("线程启动了");
3332
> }
3433
> };
3534
> runnable.run();
@@ -44,25 +43,25 @@
4443
> Runnable runnable = ()->System.out.println("线程启动了");
4544
> runnable.run();
4645
> }
47-
> ```
48-
>
49-
> ```java
46+
> ```
47+
>
48+
>```java
5049
> public static void main(String[] args) {
51-
> new Thread(new Runnable() {
50+
> new Thread(new Runnable() {
5251
> @Override
5352
> public void run() {
5453
> System.out.println("匿名内部类的执行");
5554
> }
5655
> }).start();
57-
> }
56+
> }
5857
> //设计匿名内部类的目的,就是为了方便程序猿将代码作为数据来传递。但是你会发现,这个对象看起来是很多余的,所以我们不想传入对象,只想传入行为。
5958
>
6059
> new Thread(()->{
61-
> System.out.println("lambda代替内部类");
62-
> }).start();
60+
> System.out.println("lambda代替内部类");
61+
> }).start();
6362
> //和实现某接口的对象不同,我们传入了一段代码块--一个没有名字的函数。->将参数和表达式主体分开,左边是参数,右边是方法体。
6463
> ```
65-
>
64+
>
6665
6766
6867
@@ -76,7 +75,7 @@
7675
> (String first, String second)
7776
> -> first.length() - second.length()
7877
>
79-
> //这 就 是 你 看 到 的 第 一 个 表 达 式。 lambda 表达式就是一个代码块, 以及必须传人
78+
> //这 就 是 你 看 到 的 第 一 个 表 达 式。 lambda 表达式就是一个代码块, 以及必须传入
8079
> //代码的变量规范。
8180
> ```
8281
>

0 commit comments

Comments
 (0)