Skip to content

Commit 93adda9

Browse files
committed
源码
1 parent fdcb95e commit 93adda9

21 files changed

+540
-0
lines changed

codes/java8demo/java8demo.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

codes/java8demo/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>java8demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Demo {
2+
public static void main(String[] args) {
3+
StringBuilder sb = new StringBuilder();
4+
for (int i = 1; i < 10; i++) {
5+
String chenmo = "沉默";
6+
String wanger = "王二";
7+
sb.append(chenmo);
8+
sb.append(wanger);
9+
}
10+
System.out.println(sb);
11+
}
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.itwanger.s39;
2+
3+
import sun.reflect.CallerSensitive;
4+
import sun.reflect.MethodAccessor;
5+
import sun.reflect.Reflection;
6+
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.InvocationTargetException;
9+
import java.lang.reflect.Method;
10+
11+
/**
12+
* @author 微信搜「沉默王二」,回复关键字 Java
13+
*/
14+
public class ReflectionDemo1 {
15+
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
16+
Writer writer = new Writer();
17+
writer.setName("沉默王二");
18+
System.out.println(writer.getName());
19+
20+
Class clazz = Class.forName("com.itwanger.s39.Writer");
21+
Constructor constructor = clazz.getConstructor();
22+
Object object = constructor.newInstance();
23+
24+
// Method setNameMethod = clazz.getMethod("setName", String.class);
25+
// setNameMethod.invoke(object, "沉默王二");
26+
// Method getNameMethod = clazz.getMethod("getName");
27+
// System.out.println(getNameMethod.invoke(object));
28+
29+
Method setAgeMethod = clazz.getMethod("setAge", int.class);
30+
for (int i = 0;i < 20; i++) {
31+
setAgeMethod.invoke(object, 18);
32+
}
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.itwanger.s39;
2+
3+
import java.lang.reflect.Method;
4+
5+
/**
6+
* @author 微信搜「沉默王二」,回复关键字 Java
7+
*/
8+
public class ReflectionDemo2 {
9+
public static void target(int i) {
10+
new Exception("#" + i).printStackTrace();
11+
}
12+
13+
public static void main(String[] args) throws Exception {
14+
Class<?> klass = Class.forName("com.itwanger.s39.ReflectionDemo2");
15+
Method method = klass.getMethod("target", int.class);
16+
for (int i = 0; i < 20; i++) {
17+
method.invoke(null, i);
18+
}
19+
}
20+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.itwanger.s39;
2+
3+
import java.io.PrintStream;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
* @author 微信搜「沉默王二」,回复关键字 Java
10+
*/
11+
public class ReflectionDemo3 {
12+
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
13+
//Class c1 = Class.forName("com.itwanger.s39.ReflectionDemo3");
14+
//System.out.println(c1.getCanonicalName());
15+
//
16+
//Class c2 = Class.forName("[D");
17+
//System.out.println(c2.getCanonicalName());
18+
//
19+
//Class c3 = Class.forName("[[Ljava.lang.String;");
20+
//System.out.println(c3.getCanonicalName());
21+
22+
//Class c1 = ReflectionDemo3.class;
23+
//System.out.println(c1.getCanonicalName());
24+
//
25+
//Class c2 = String.class;
26+
//System.out.println(c2.getCanonicalName());
27+
//
28+
//Class c3 = int[][][].class;
29+
//System.out.println(c3.getCanonicalName());
30+
31+
Class c1 = Writer.class;
32+
Writer writer = (Writer) c1.newInstance();
33+
34+
Class c2 = Class.forName("com.itwanger.s39.Writer");
35+
Constructor constructor = c2.getConstructor();
36+
Object object = constructor.newInstance();
37+
38+
Constructor[] constructors1 = String.class.getDeclaredConstructors();
39+
for (Constructor c : constructors1) {
40+
System.out.println(c);
41+
}
42+
43+
Method[] methods1 = System.class.getDeclaredMethods();
44+
for (Method m : methods1) {
45+
System.out.println(m);
46+
}
47+
Method[] methods2 = System.class.getMethods();
48+
for (Method m : methods2) {
49+
System.out.println(m);
50+
}
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.itwanger.s39;
2+
3+
/**
4+
* @author 微信搜「沉默王二」,回复关键字 Java
5+
*/
6+
public class Writer {
7+
private int age;
8+
private String name;
9+
10+
public int getAge() {
11+
return age;
12+
}
13+
14+
public void setAge(int age) {
15+
this.age = age;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.itwanger.s41;
2+
3+
/**
4+
* @author 微信搜「沉默王二」,回复关键字 Java
5+
*/
6+
public class Demo {
7+
public static void main(String[] args) {
8+
System.out.println(10/0);
9+
}
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.itwanger.s41;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author 微信搜「沉默王二」,回复关键字 Java
9+
*/
10+
public class Demo1 {
11+
public static void main(String[] args) throws ClassNotFoundException {
12+
Class clz = Class.forName("com.itwanger.s41.Demo1");
13+
14+
String serverName = args[0];
15+
int port = Integer.parseInt(args[1]);
16+
try {
17+
Socket client = new Socket(serverName, port);
18+
} catch (IOException e) {
19+
20+
}
21+
}
22+
23+
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.itwanger.s41;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author 微信搜「沉默王二」,回复关键字 Java
9+
*/
10+
public class Demo2 {
11+
private String mHost;
12+
private int mPort;
13+
private Socket mSocket;
14+
private final Object mLock = new Object();
15+
16+
public void run() {
17+
if (mSocket == null) {
18+
initSocket();
19+
}
20+
try {
21+
OutputStream out = mSocket.getOutputStream();
22+
byte[] buffer = new byte[1024];
23+
int n;
24+
while ((n = System.in.read(buffer)) > 0) {
25+
out.write(buffer, 0, n);
26+
}
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
initSocket();
30+
}
31+
}
32+
33+
private void initSocket() {
34+
while (true) {
35+
try {
36+
Socket socket = new Socket(mHost, mPort);
37+
synchronized (mLock) {
38+
mSocket = socket;
39+
}
40+
break;
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
}
47+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.itwanger.s42;
2+
3+
/**
4+
* @author 微信搜「沉默王二」,回复关键字 Java
5+
*/
6+
public class Demo1 {
7+
static int num = 1000000;
8+
public static void main(String[] args) {
9+
// tryFun();
10+
// fun();
11+
test1();test2();
12+
}
13+
14+
15+
static void tryFun() {
16+
int sum = 0;
17+
long t1 = System.currentTimeMillis();
18+
for(int i=0;i<num;i++) {
19+
try {
20+
sum = sum + 1;
21+
System.out.println(sum);
22+
} catch (Exception e) {
23+
24+
}
25+
}
26+
long t2 = System.currentTimeMillis();
27+
System.out.println("有 try catch 的时间:" + (t2-t1));
28+
}
29+
30+
static void fun() {
31+
int sum = 0;
32+
long t1 = System.currentTimeMillis();
33+
try {
34+
for(int i=0;i<num;i++) {
35+
sum = sum + 1;
36+
System.out.println(sum);
37+
}
38+
} catch (Exception e) {
39+
40+
}
41+
long t2 = System.currentTimeMillis();
42+
System.out.println("无 try catch 的时间:" + (t2-t1));
43+
}
44+
45+
static void test() {
46+
int num1, num2;
47+
try {
48+
num1 = 0;
49+
num2 = 62 / num1;
50+
System.out.println(num2);
51+
System.out.println("try 块的最后一句");
52+
}
53+
catch (ArithmeticException e) {
54+
// 算术运算发生时跳转到这里
55+
System.out.println("除数不能为零");
56+
}
57+
catch (Exception e) {
58+
// 通用型的异常意味着可以捕获所有的异常,它应该放在最后面,
59+
System.out.println("异常发生了");
60+
}
61+
System.out.println("try-catch 之外的代码.");
62+
}
63+
64+
static void test1 () {
65+
try{
66+
int arr[]=new int[7];
67+
arr[9]=30/1;
68+
System.out.println("try 块的最后");
69+
} catch(ArithmeticException | ArrayIndexOutOfBoundsException e){
70+
System.out.println("除数必须是 0");
71+
}
72+
73+
finally {
74+
75+
}
76+
System.out.println("try-catch 之外");
77+
}
78+
79+
static int test2 () {
80+
try {
81+
System.exit(1);
82+
return 112;
83+
}
84+
finally {
85+
System.out.println("finally 块");
86+
}
87+
}
88+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.itwanger.s43;
2+
3+
/**
4+
* @author 微信搜「沉默王二」,回复关键字 Java
5+
*/
6+
public class ThrowDemo {
7+
static void checkEligibilty(int stuage){
8+
if(stuage<18) {
9+
throw new ArithmeticException("年纪未满 18 岁,禁止观影");
10+
} else {
11+
System.out.println("请认真观影!!");
12+
}
13+
}
14+
15+
public static void main(String args[]){
16+
// checkEligibilty(10);
17+
// System.out.println("愉快地周末..");
18+
19+
try {
20+
myMethod1();
21+
} catch (ArithmeticException e) {
22+
// 算术异常
23+
} catch (NullPointerException e) {
24+
// 空指针异常
25+
}
26+
}
27+
public static void myMethod1() throws ArithmeticException, NullPointerException{
28+
// 方法签名上声明异常
29+
}
30+
31+
public void myMethod() {
32+
try {
33+
// 可能抛出异常
34+
} catch (ArithmeticException e) {
35+
// 算术异常
36+
} catch (NullPointerException e) {
37+
// 空指针异常
38+
}
39+
}
40+
41+
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.itwanger.s45;
2+
3+
/**
4+
* @author 微信搜「沉默王二」,回复关键字 Java
5+
*/
6+
public class Demo {
7+
public static void main(String[] args) {
8+
Runtime runtime = Runtime.getRuntime();
9+
}
10+
}

0 commit comments

Comments
 (0)