Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4f270e1

Browse filesBrowse files
committed
跟新
1 parent b15a1c2 commit 4f270e1
Copy full SHA for 4f270e1

11 files changed

+252-133Lines changed: 252 additions & 133 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Chapter18_ClassLoadAndReflection/GenericTypeFactory.java‎

Copy file name to clipboardExpand all lines: src/Chapter18_ClassLoadAndReflection/GenericTypeFactory.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public static Object getInstance2(String clsName){
3636
//加入泛型
3737
/*
3838
* 直接通过 类对象,反射得到类实例
39+
*
3940
*/
41+
// 修饰符,泛型类型,返回值,方法名(参数)
4042
public static <F> F getInstance3(Class<F> cls){
4143
try{
4244
return cls.newInstance();
Collapse file

‎src/LambdaLearn/MyFunction.java‎

Copy file name to clipboardExpand all lines: src/LambdaLearn/MyFunction.java
-28Lines changed: 0 additions & 28 deletions
This file was deleted.
Collapse file

‎src/LambdaLearn/doubleColonTest.java‎

Copy file name to clipboardExpand all lines: src/LambdaLearn/doubleColonTest.java
+50-15Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@
4141
* 为了让编译器帮助我们确保一个接口满足FI的要求(也就是说有且仅有一个抽象方法)
4242
*/
4343

44-
public class doubleColonTest {
44+
public class doubleColonTest implements Functional3{
4545

4646
public static Functional3 testMethodReference(){
47-
System.out.println("测试::方法引用");
47+
System.out.println("测试::方法引用1");
4848
return null;
4949
}
5050

5151
public static Functional3<?, ?> testMethodReference2(){
52-
System.out.println("测试::方法引用");
52+
System.out.println("测试::方法引用2");
5353
return (a,b)->12;
5454
}
5555

5656
public static String 中文(){
57-
System.out.println("测试::方法引用");
57+
System.out.println("测试::方法引用3");
5858
return null;
5959
}
6060

@@ -63,18 +63,33 @@ public void testInvodeMethod(Functional3<String,Integer> f3){
6363
int b = 1;
6464
f3.sex(a, b);
6565
}
66+
67+
public void testInvodeMethod2(Functional<String> f3){
68+
}
69+
6670
public int testMethod(String s,Integer i){
6771
return 1;
6872
}
6973

7074
public static void main(String[] args) {
75+
doubleColonTest thisClass = new doubleColonTest();
76+
//类强制转换为函数式接口测试
77+
doubleTest2<String> d2 = new doubleTest2<String>();
78+
Functional<String> f = (Functional<String>)(d2::method1);
79+
f.judge("hahahaha");
80+
//不用强制类型转化了。编译器自动识别转换
81+
thisClass.testInvodeMethod2(d2::method1);
7182

72-
/**有空再来修改2017.1**/
73-
doubleColonTest thisClass = new doubleColonTest();
74-
// Functional3 s = thisClass ::testMethodReference();
75-
thisClass.testInvodeMethod((a,b)->a);
76-
// thisClass.testInvodeMethod(doubleColonTest::testMethod);
77-
83+
/**有空再来修改2017.1**/
84+
//::表示一个implement函数式接口的对象调用重写的方法。
85+
// Functional3 s = thisClass::testMethodReference();
86+
Functional3 s2 = thisClass.testMethodReference();
87+
Functional3 s3 = thisClass::sex;
88+
Functional3 s4 = (a,b)->{
89+
return a;
90+
};
91+
Functional3 s5 = (a,b)->a;
92+
// doubleColonTest s6 = thisClass::sex; // 报错
7893
/*
7994
* 需要注意的是,函数式接口的名称并不是 lambda 表达式的一部分。
8095
* 那么问题来了,对于给定的 lambda表达式,它的类型是什么?
@@ -99,11 +114,12 @@ public static void main(String[] args) {
99114
* 当 lambda 的参数有两个而且它的类型可以被推导得知时,该参数列表外面的括号 !不可以! 被省略:
100115
*/
101116
Functional3<String,Boolean> ft = (a,b)->a ;
117+
//获得一个Functional对象,重写了函数式接口的方法
102118
Functional<String> f4 = a -> {
103119
System.out.println("傻逼");
104120
return a;
105121
};
106-
f4.judge("lala");
122+
System.out.println(f4.judge("lala"));
107123

108124
/**
109125
* lambda表达式对值封闭,对变量开放:lambda expressions close over values, not variables,
@@ -121,10 +137,6 @@ public void print(){
121137
System.out.println("这是一个打印");
122138
}
123139

124-
// public Object usePrint(doubleColonTest d){
125-
// return doubleColonTest::print; //错误方法
126-
// }
127-
128140
public void forBlock(){//局部变量,全局变量,显示参数,隐式参数,匿名类,匿名内部类,类型擦除跟重载,编译类型,运行时类型???????????
129141
// int i = 0;
130142
// int sum = 0;
@@ -133,6 +145,13 @@ public void print(){
133145
// }
134146
}
135147

148+
@Override
149+
public Object sex(Object t, Object k) {
150+
System.out.println("sex");
151+
// TODO Auto-generated method stub
152+
return null;
153+
}
154+
136155
}
137156

138157
@FunctionalInterface //接口有此标志,说明其内部只能有一个方法,变量倒是无所谓,静态方法(类方法),默认方法,以及冗余方法不属于函数式接口的范畴
@@ -162,3 +181,19 @@ interface Functional3<T,K>{
162181
abstract T sex(T t,K k);
163182
}
164183

184+
/**
185+
* 类强制转换为函数式接口测试
186+
* @author Administrator
187+
*
188+
* @param <T>
189+
*/
190+
class doubleTest2<T>{
191+
192+
T method1(T t) {
193+
System.out.println("类中的一个普通方法,跟要强制转换的函数式接口的输入输出参数相同!");
194+
System.out.println(t);
195+
return t;
196+
} // 默认是抽象的方法
197+
198+
}
199+
Collapse file

‎src/LambdaLearn/implementsMyFunction.java‎

Copy file name to clipboardExpand all lines: src/LambdaLearn/implementsMyFunction.java
+52-17Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,41 @@ public class implementsMyFunction implements MyFunction<String>{
2020

2121
public static void main(String[] args) {
2222
implementsMyFunction thisClass = new implementsMyFunction();
23-
24-
23+
myfun fun = new myfun();
2524

2625
cache.put(0, 0);
2726
cache.put(1, 1);
2827
implementsMyFunction.fibonacciJava8(8);
2928
System.out.println("获得缓存中8的值:"+cache.get(8));
3029

31-
MyFunction<Integer> myFuc = (Integer a)->a+1;
30+
//实例化一个函数式对象,并且定义了该对象的抽象方法--lamda表达式:(Integer a)->a+1 就代表了一个对象了
31+
MyFunction<Integer> myFuc = (Integer a)->a+1;
3232
int rest = myFuc.oneMethod(4);
3333
System.out.println("使用自定义的函数式接口:"+rest);
3434

35-
// 开始一个线程,使用lambda方法
35+
// 开始一个线程,使用lambda方法 java.lang.Thread.Thread(Runnable target, String name)
36+
//实例化一个Runnable函数式对象,并且定义了该对象的抽象方法
3637
new Thread(()->{System.out.println("开始一个线程,使用lambda方法");}, "lambda线程").start();
3738

3839
// 本类的对象(是一个myFunction对象)调动本类的方法,该方法需要一个myFunction接口的实现对象
3940
//--此处类似匿名内部类的实例
41+
System.out.println("------------------------------");
4042
thisClass.useFunc(a->{
41-
System.out.println("调用对象中含有函数式接口参数的方法");
42-
return "long";
43+
System.out.println("***调用对象中含有函数式接口参数的方法");
44+
return a;
4345
});
46+
//参数 thisClass::oneMethod 和 thisClass 没有什么区别吗
47+
System.out.println("------------------------------");
4448
thisClass.useFunc(thisClass::oneMethod);
45-
//
49+
System.out.println("------------------------------");
4650
thisClass.useFunc(thisClass);
51+
System.out.println("------------------------------");
4752
thisClass.useFunc(a->"mess");
53+
System.out.println("------------------------------");
4854
thisClass.useFunc(a->{return "mess";});
49-
55+
System.out.println("------------------------------");
5056
useFunc2(thisClass);
51-
System.out.println(thisClass.oneMethod("随便返回"));
52-
thisClass.oneMethod("suibian");
53-
57+
// useFunc2(fun); //报错,虽然lambda结构一样
5458
List<String> list = Arrays.asList("ab","lady","amine","count");
5559
List<String> list2 = Stream.of("ab","lady","amine","count").collect(Collectors.toList());
5660

@@ -75,24 +79,27 @@ public static void main(String[] args) {
7579
// });
7680
// 此处有点怪异,System.out::println是一个Consumer对象吗?
7781
list.forEach(System.out::println);
82+
list.forEach((a)->{
83+
System.out.println("打印:"+a);
84+
});
7885
// thisClass.useFunc({implementsMyFunction::testMethod});
7986
// implementsMyFunction ss = implementsMyFunction::thisMethod;
8087

8188
}
8289

8390
public void useFunc(MyFunction<String> mm){
84-
91+
//这是一个空的方法,方法体没有任何操作
8592
}
8693
public static void useFunc2(MyFunction<String> mm){
87-
mm.oneMethod("::使用参数的方法");
94+
String str = mm.oneMethod("::使用参数的方法");
95+
System.out.println(str+"**");
8896
}
8997

9098
@Override
9199
public String oneMethod(String t) {
92100
// TODO Auto-generated method stub
93-
String str = "override oneMethod";
94101
System.out.println("重写函数式接口的抽象方法oneMethod,并返回" + t);
95-
return str;
102+
return t;
96103
}
97104

98105
public static void filter(List<String> str,Predicate pr){
@@ -168,9 +175,37 @@ static int fibonacci(int n) {
168175
public <R> implementsMyFunction change(Function<? super String, ? extends String> mapper){
169176
return null;
170177
}
171-
172-
178+
}
173179

180+
@FunctionalInterface
181+
interface MyFunction<T> {
182+
T oneMethod(T t);
183+
static void testMethod(){System.out.println("接口里的静态方法testMethod");};
184+
185+
default void defaultTest1(){ //默认方法可以被继承的类重写,也可以直接拿来用
186+
System.out.println("This is a default method1");
187+
};
188+
default void defaultTest2(){
189+
System.out.println("This is a default method2");
190+
};
191+
default void defaultTest3(){
192+
System.out.println("This is a default method3");
193+
};
194+
}
195+
196+
@FunctionalInterface
197+
interface MyFunction2<T> {
198+
T accept(T t);
199+
}
200+
201+
class myfun implements MyFunction2<String>{
202+
203+
@Override
204+
public String accept(String t) {
205+
// TODO Auto-generated method stub
206+
return null;
207+
}
208+
174209
}
175210

176211

Collapse file

‎src/LambdaLearn/lambdaTest.java‎

Copy file name to clipboardExpand all lines: src/LambdaLearn/lambdaTest.java
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,10 @@ public static void main(String[] args) {
291291
* lambda表达式 类方法 的引用 1.lambda代码中实现代码只有一行, 2.该行是一个静态方法
292292
* 3.该静态方法的参数与函数式接口的参数一致,且返回值类型相同
293293
*/
294-
MyFunction<String> myFun = lambdaTest::doubleTest;
295-
String doubleTestRes = myFun.oneMethod("123");
296-
System.out.println(doubleTestRes);
294+
MyFunction<String> myFun = lambdaTest::doubleTest; //反正是一个调用方法
295+
MyFunction<String> myFun11 = thisClass::doubleTest2;//反正是一个调用方法
296+
String doubleTestRes = myFun.oneMethod("123"); //执行方法
297+
System.out.println("f:"+doubleTestRes);
297298
/**
298299
* lambda表达式 对象方法 的引用 1.双引号左边是对象 2.双引号右边是对象的方法 3.对象的方法的参数和返回值要和函数式接口的一样
299300
*/
@@ -832,6 +833,7 @@ public static int doubleTesst(String ss) {
832833

833834
public static String doubleTest(String ss) {
834835
ss = "&&&&&&&&&&&&&&&& " + ss + " this is result";
836+
System.out.println("方法组装");
835837
return ss;
836838
}
837839

Collapse file

‎src/my/GenericClassTest.java‎

Copy file name to clipboardExpand all lines: src/my/GenericClassTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static void addItem2(Object[] objects,Collection<?> c){
284284
}
285285

286286
/**
287-
* 泛型方法: 比普通方法对了一个类型形参声明,类型形参声明在 修饰符和返回值 之间,可以有多个类型形参,用逗号隔开
287+
* 泛型方法: 比普通方法多了一个类型形参声明,类型形参声明在 修饰符和返回值 之间,可以有多个类型形参,用逗号隔开
288288
* 与 泛型类等不同的是,调用泛型方法时,无需显示传入实际类型参数,编译器会根据参数推断出类型参数的类型。
289289
* @param t
290290
* @param c
Collapse file

‎src/my/LoggerTest.java‎

Copy file name to clipboardExpand all lines: src/my/LoggerTest.java
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.logging.log4j.Level;
55
import org.apache.logging.log4j.LogManager;
66
import org.apache.logging.log4j.Logger;
7+
import org.apache.logging.log4j.message.ParameterizedMessage;
78
import org.slf4j.LoggerFactory;
89

910
/**
@@ -114,7 +115,18 @@ public static void main(String[] args) {
114115
logger2.debug("debug");
115116
logger2.trace("trace");
116117

117-
118+
/********************************/
119+
String str = "{}个大傻逼";
120+
System.out.println(LoggerTest.StringMax(str, 1));
121+
}
122+
123+
/**
124+
* log4j的字符串组装方法
125+
* @param str
126+
* @param objs
127+
*/
128+
public static String StringMax(String str,Object... objs){
129+
return ParameterizedMessage.format(str, objs);
118130
}
119131

120132
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.